Skip to content

Commit 605f027

Browse files
committed
refactor: use crypto-kit for HMAC on non Apple platforms
1 parent 45e5d4a commit 605f027

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ let dependencies = [
1111
Package.Dependency.package(url: "https://github.com/kylef-archive/CommonCrypto.git", from: "1.0.0"),
1212
]
1313
#endif
14-
let excludes = ["HMAC/HMACCryptoSwift.swift"]
14+
let excludes = ["HMAC/HMACCrypto.swift"]
1515
let targetDependencies: [Target.Dependency] = []
1616
#else
1717
let dependencies = [
18-
Package.Dependency.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "0.10.0"),
18+
Package.Dependency.package(url: "https://github.com/apple/swift-crypto", from: "1.1.3"),
1919
]
2020
let excludes = ["HMAC/HMACCommonCrypto.swift"]
21-
let targetDependencies: [Target.Dependency] = ["CryptoSwift"]
21+
let targetDependencies: [Target.Dependency] = ["Crypto"]
2222
#endif
2323

2424

Sources/JWA/HMAC/HMACCrypto.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Foundation
2+
import Crypto
3+
4+
5+
extension HMACAlgorithm: SignAlgorithm, VerifyAlgorithm {
6+
var symmetricKey: SymmetricKey {
7+
return SymmetricKey(data: key)
8+
}
9+
10+
public func sign(_ message: Data) -> Data {
11+
switch hash {
12+
case .sha256:
13+
let code = HMAC<SHA256>.authenticationCode(for: message, using: symmetricKey)
14+
return Data(code)
15+
case .sha384:
16+
let code = HMAC<SHA384>.authenticationCode(for: message, using: symmetricKey)
17+
return Data(code)
18+
case .sha512:
19+
let code = HMAC<SHA512>.authenticationCode(for: message, using: symmetricKey)
20+
return Data(code)
21+
}
22+
}
23+
24+
public func verify(_ message: Data, signature: Data) -> Bool {
25+
switch hash {
26+
case .sha256:
27+
return HMAC<SHA256>.isValidAuthenticationCode(signature, authenticating: message, using: symmetricKey)
28+
case .sha384:
29+
return HMAC<SHA384>.isValidAuthenticationCode(signature, authenticating: message, using: symmetricKey)
30+
case .sha512:
31+
return HMAC<SHA512>.isValidAuthenticationCode(signature, authenticating: message, using: symmetricKey)
32+
}
33+
}
34+
}

Sources/JWA/HMAC/HMACCryptoSwift.swift

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)