Create hash in swift using key and message

前端 未结 2 906
感动是毒
感动是毒 2021-01-16 22:17

I want to create an SHA1 hmac hash of a string using a key in swift. In obj-c I used this and it worked great:

+(NSString *)sha1FromMessage:(NSString *)messa         


        
相关标签:
2条回答
  • 2021-01-16 23:02

    The last parameter of the CCHmac() function has the type UnsafeMutablePointer<Void> because that's where the result is written to. You have to declare cHMAC as variable and pass it as an in-out expression with &. In addition, some type conversions are necessary:

    var cHMAC = [CUnsignedChar](count: Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0)
    CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), cKey, UInt(cKey.count), cData, UInt(cData.count), &cHMAC)
    
    0 讨论(0)
  • 2021-01-16 23:05

    Apple enum values are defined differently in Swift. Instead of kCCHmacAlgSHA1, it's probably defined like CCHmacAlgorithm.SHA1.

    This is answered here: https://stackoverflow.com/a/24411522/2708650

    0 讨论(0)
提交回复
热议问题