mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-02 02:22:25 +00:00
Use CryptoKit instead of CommonCrypto with SHA1. Add tests.
This commit is contained in:
1 parent
2c323b5e92
commit
fdf8325b55
2 files changed
+76
-7
No files matched your search
@@ -7,7 +7,6 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CommonCrypto
|
||||
import CryptoKit
|
||||
|
||||
private let hexDigits: [UInt8] = Array("0123456789abcdef".utf8)
|
||||
@@ -25,12 +24,20 @@ public extension String {
|
||||
return link.htmlByAddingLink(link)
|
||||
}
|
||||
|
||||
func hmacUsingSHA1(key: String) -> String {
|
||||
var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
|
||||
CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), key, key.count, self, self.count, &digest)
|
||||
let data = Data(digest)
|
||||
return data.map { String(format: "%02hhx", $0) }.joined()
|
||||
}
|
||||
/// HMAC-SHA1 of `self` (as UTF-8 bytes) using `key` (as UTF-8 bytes),
|
||||
/// formatted as 40 lowercase hex characters.
|
||||
func hmacUsingSHA1(key: String) -> String {
|
||||
let symmetricKey = SymmetricKey(data: Data(key.utf8))
|
||||
let mac = HMAC<Insecure.SHA1>.authenticationCode(for: Data(utf8), using: symmetricKey)
|
||||
var hex = [UInt8](repeating: 0, count: mac.byteCount * 2)
|
||||
var i = 0
|
||||
for byte in mac {
|
||||
hex[i] = hexDigits[Int(byte >> 4)]
|
||||
hex[i + 1] = hexDigits[Int(byte & 0x0F)]
|
||||
i += 2
|
||||
}
|
||||
return String(decoding: hex, as: UTF8.self)
|
||||
}
|
||||
}
|
||||
|
||||
public extension String {
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// HMACTests.swift
|
||||
// RSCoreTests
|
||||
//
|
||||
// Created by Brent Simmons on 2026-04-24.
|
||||
//
|
||||
|
||||
import Testing
|
||||
@testable import RSCore
|
||||
|
||||
@Suite struct HMACTests {
|
||||
|
||||
@Test("Pinned HMAC-SHA1: key=Jefe, data=\"what do you want for nothing?\"")
|
||||
func pinnedVectorJefe() {
|
||||
// Cross-verified with Python hashlib and `openssl dgst -sha1 -hmac`.
|
||||
let signed = "what do you want for nothing?".hmacUsingSHA1(key: "Jefe")
|
||||
#expect(signed == "548329b620689206afea507f732f80f41a4c1a75")
|
||||
}
|
||||
|
||||
@Test("Empty key and empty message produce HMAC-SHA1 of empty/empty")
|
||||
func emptyKeyEmptyMessage() {
|
||||
// HMAC-SHA1("", "") is a well-known value confirmable against any
|
||||
// HMAC-SHA1 reference implementation (Python, openssl).
|
||||
#expect("".hmacUsingSHA1(key: "") == "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d")
|
||||
}
|
||||
|
||||
@Test("Emoji message (non-ASCII UTF-8) produces pinned HMAC-SHA1")
|
||||
func nonASCIIEmojiMessage() {
|
||||
// 🙂 is U+1F642, 4 UTF-8 bytes. HMAC operates on bytes, so the
|
||||
// Swift Character count (1) must not be used — this pins the
|
||||
// byte-correct result computed via Python hashlib.
|
||||
let signed = "🙂".hmacUsingSHA1(key: "k")
|
||||
#expect(signed == "442de3a50b8dc6ba238e6049059c4d069d0f2e38")
|
||||
}
|
||||
|
||||
@Test("Output is 40 lowercase hex characters")
|
||||
func outputFormat() {
|
||||
let signed = "arbitrary input".hmacUsingSHA1(key: "arbitrary key")
|
||||
#expect(signed.count == 40)
|
||||
#expect(signed.allSatisfy { c in
|
||||
("0"..."9").contains(c) || ("a"..."f").contains(c)
|
||||
})
|
||||
}
|
||||
|
||||
@Test("Different keys produce different signatures for the same message")
|
||||
func differentKeysDiffer() {
|
||||
let message = "The quick brown fox"
|
||||
let a = message.hmacUsingSHA1(key: "key-one")
|
||||
let b = message.hmacUsingSHA1(key: "key-two")
|
||||
#expect(a != b)
|
||||
}
|
||||
|
||||
@Test("Same key and message produce deterministic output")
|
||||
func deterministic() {
|
||||
let message = "https://example.com/article/42"
|
||||
let key = "client-secret"
|
||||
let a = message.hmacUsingSHA1(key: key)
|
||||
let b = message.hmacUsingSHA1(key: key)
|
||||
#expect(a == b)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user