Swift 中的 CommonHMAC [英] CommonHMAC in Swift
问题描述
我正在尝试在 Swift 中创建一个字符串的 HMAC SHA-1 哈希,但无法弄清楚如何与 API 交互,因为它似乎没有导入 CommonCrypto 框架.我尝试了各种不同形式的导入 CommonCrypto"并创建了一个桥接头文件,但没有任何区别.
I'm trying to create a HMAC SHA-1 hash of a string in Swift but can't figure out how to interact with the APIs as it doesn't seem to be importing the CommonCrypto framework. I've tried various different forms of "import CommonCrypto" and creating a bridging header file but none of it made a difference.
奇怪的是,如果我创建了一个 Objective-C 类,我就可以毫无问题地与 API 交互,所以这似乎是 Swift 独有的.
The odd thing is that if I create an Objective-C class, I'm able to interact with APIs without any problems, so this seems to be unique to Swift.
另外,如果有人能告诉我 uint8_t digest[CC_SHA1_DIGEST_LENGTH]
在 Swift 中的等价物,我将不胜感激
Also if anyone could tell me what the equivalent of uint8_t digest[CC_SHA1_DIGEST_LENGTH]
is in Swift I'd be very grateful
推荐答案
您可以在 Swift 中完成.只需确保将 #import
添加到桥接 Objective-C 桥接标头.
You can do it in Swift. Just make sure you add #import <CommonCrypto/CommonHMAC.h>
to the bridging Objective-C bridging header.
更新:对于 Swift 4,请在此处使用 Swift 包管理器查看更好的解决方案:https://github.com/jernejstrasner/SwiftCrypto
Update: For Swift 4 see a much better solution using the Swift Package Manager here: https://github.com/jernejstrasner/SwiftCrypto
enum CryptoAlgorithm {
case MD5, SHA1, SHA224, SHA256, SHA384, SHA512
var HMACAlgorithm: CCHmacAlgorithm {
var result: Int = 0
switch self {
case .MD5: result = kCCHmacAlgMD5
case .SHA1: result = kCCHmacAlgSHA1
case .SHA224: result = kCCHmacAlgSHA224
case .SHA256: result = kCCHmacAlgSHA256
case .SHA384: result = kCCHmacAlgSHA384
case .SHA512: result = kCCHmacAlgSHA512
}
return CCHmacAlgorithm(result)
}
var digestLength: Int {
var result: Int32 = 0
switch self {
case .MD5: result = CC_MD5_DIGEST_LENGTH
case .SHA1: result = CC_SHA1_DIGEST_LENGTH
case .SHA224: result = CC_SHA224_DIGEST_LENGTH
case .SHA256: result = CC_SHA256_DIGEST_LENGTH
case .SHA384: result = CC_SHA384_DIGEST_LENGTH
case .SHA512: result = CC_SHA512_DIGEST_LENGTH
}
return Int(result)
}
}
extension String {
func hmac(algorithm: CryptoAlgorithm, key: String) -> String {
let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
let strLen = Int(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
let digestLen = algorithm.digestLength
let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
let keyStr = key.cStringUsingEncoding(NSUTF8StringEncoding)
let keyLen = Int(key.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
CCHmac(algorithm.HMACAlgorithm, keyStr!, keyLen, str!, strLen, result)
let digest = stringFromResult(result, length: digestLen)
result.dealloc(digestLen)
return digest
}
private func stringFromResult(result: UnsafeMutablePointer<CUnsignedChar>, length: Int) -> String {
var hash = NSMutableString()
for i in 0..<length {
hash.appendFormat("%02x", result[i])
}
return String(hash)
}
}
这篇关于Swift 中的 CommonHMAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!