Skip to content

Instantly share code, notes, and snippets.

@bubudrc
Last active February 3, 2023 19:07
Show Gist options
  • Save bubudrc/b572fa64dc4d18dd803f2ee652ac01e7 to your computer and use it in GitHub Desktop.
Save bubudrc/b572fa64dc4d18dd803f2ee652ac01e7 to your computer and use it in GitHub Desktop.
HMAC Using SHA1 with Secret Key (Works for other HMAC algorithms) for CryptoKit
import Foundation
import CryptoKit
extension String {
func hmacUsingSHA1(_ secretString: String) -> String {
let key = SymmetricKey(data: secretString.data(using: .utf8)!)
let signature = HMAC<Insecure.SHA1>.authenticationCode(for: self.data(using: .utf8)!, using: key)
return Data(signature).map { String(format: "%02hhx", $0) }.joined()
}
}
print("my_string_to_hash".hmacUsingSHA1("SECRET_KEY"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment