xcode warning: cannot involke ‘CCHmac’ with an argument List of type (UInt32,[CChar]?,UInt,inout[(CUnsignedChar)]
func generateHMAC(key: String,data: String) -> String { let cKey = key.cStringUsingEnCoding(NSUTF8StringEnCoding) let cdata = data.cStringUsingEnCoding(NSUTF8StringEnCoding) var result = [CUnsignedChar](count: Int(CC_SHA512_DIGEST_LENGTH),repeatedValue: 0) CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA512),cKey,strlen(cKey!),cdata,strlen(cdata!),&result) let hash = NSMutableString() for var i = 0; i < result.count; i++ { hash.appendFormat("%02hhx",result[i]) } return hash as String}解决方法 问题是strlen返回一个UInt,而CCHmac的长度参数是Ints.
虽然你可以做一些强制,你也可以只使用两个数组的count属性而不是调用strlen.
func generateHMAC(key: String,data: String) -> String { var result: [CUnsignedChar] if let cKey = key.cStringUsingEnCoding(NSUTF8StringEnCoding),cdata = data.cStringUsingEnCoding(NSUTF8StringEnCoding) { let algo = CCHmacAlgorithm(kCCHmacAlgSHA512) result = Array(count: Int(CC_SHA512_DIGEST_LENGTH),repeatedValue: 0) CCHmac(algo,cKey.count-1,cdata.count-1,&result) } else { // as @MartinR points out,this is in theory impossible // but personally,I prefer doing this to using `!` fatalError("Nil returned when processing input strings as UTF8") } let hash = NSMutableString() for val in result { hash.appendFormat("%02hhx",val) } return hash as String}总结
以上是内存溢出为你收集整理的ios – 使用CCHmac()生成HMAC swift sdk8.3全部内容,希望文章能够帮你解决ios – 使用CCHmac()生成HMAC swift sdk8.3所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)