File tree Expand file tree Collapse file tree 1 file changed +5
-26
lines changed Expand file tree Collapse file tree 1 file changed +5
-26
lines changed Original file line number Diff line number Diff line change 1
- import CommonCrypto
1
+ import CryptoKit
2
2
import Foundation
3
3
import TweetNacl
4
4
@@ -17,33 +17,12 @@ struct Crypto {
17
17
/// - message: The message.
18
18
/// - Returns: The hex-encoded MAC string.
19
19
static func generateSHA256HMAC( secret: String , message: String ) -> String {
20
- let secretData = Data ( secret. utf8)
21
- let messageData = Data ( message. utf8)
22
-
23
- let algorithm = CCHmacAlgorithm ( kCCHmacAlgSHA256)
24
- let digestLength = Int ( CC_SHA256_DIGEST_LENGTH)
25
-
26
- var digest = Data ( count: digestLength)
27
-
28
- digest. withUnsafeMutableBytes { ( digestBytes: UnsafeMutableRawBufferPointer ) in
29
- secretData. withUnsafeBytes { ( secretBytes: UnsafeRawBufferPointer ) in
30
- messageData. withUnsafeBytes { ( messageBytes: UnsafeRawBufferPointer ) in
31
- CCHmac ( algorithm,
32
- secretBytes. baseAddress,
33
- secretData. count,
34
- messageBytes. baseAddress,
35
- messageData. count,
36
- digestBytes. baseAddress)
37
- }
38
- }
39
- }
40
-
41
- // Data to hex string
42
- let signature = digest
43
- . map { String ( format: " %02x " , $0) }
44
- . joined ( )
20
+ let key = SymmetricKey ( data: Data ( secret. utf8) )
21
+ let signature = HMAC< SHA256> . authenticationCode( for: Data ( message. utf8) , using: key)
45
22
46
23
return signature
24
+ . map { String ( format: " %02hhx " , $0) }
25
+ . joined ( )
47
26
}
48
27
49
28
/// Decrypts some data `String` using a key, according to the NaCl secret box algorithm.
You can’t perform that action at this time.
0 commit comments