Skip to content

Instantly share code, notes, and snippets.

@nenadvulic
Last active February 22, 2017 09:53
Show Gist options
  • Save nenadvulic/1e83516a68fc78a04075b909b69c7669 to your computer and use it in GitHub Desktop.
Save nenadvulic/1e83516a68fc78a04075b909b69c7669 to your computer and use it in GitHub Desktop.
SHA1 in Swift 3
func hmacsha1(_ data:Data!, secret key:Data!) -> Data! {
let cHMAC = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity:Int(CC_SHA1_DIGEST_LENGTH))
key.withUnsafeBytes {(keyBytes: UnsafePointer<UInt8>)->Void in
data.withUnsafeBytes {(dataBytes: UnsafePointer<UInt8>)->Void in
CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1),keyBytes, key.count, dataBytes, data.count, cHMAC)
}
}
return Data(bytes:cHMAC, count:Int(CC_SHA1_DIGEST_LENGTH))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment