Skip to content

Instantly share code, notes, and snippets.

@ecmadao
Created February 24, 2018 07:42
Show Gist options
  • Save ecmadao/c1fd642cbff9bd7202f7782ebf2b5fd9 to your computer and use it in GitHub Desktop.
Save ecmadao/c1fd642cbff9bd7202f7782ebf2b5fd9 to your computer and use it in GitHub Desktop.
Swift4 HMAC-SHA1-BASE64
import CommonCrypto
extension String {
func hmac(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(bytes: digest)
return data.base64EncodedString()
}
}
@ecmadao
Copy link
Author

ecmadao commented Feb 24, 2018

@kingkangyu
Copy link

Thanks,helps me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment