ios - Generate a HMAC swift sdk8.3 using CCHmac() -
before sdk8.3 generating hmac way. error on cchmac() function. since i'm beginner can't figure out how fix it. in advance help!
xcode warning: cannot involke 'cchmac' argument list of type (uint32, [cchar]?, uint, [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() var = 0; < result.count; i++ { hash.appendformat("%02hhx", result[i]) } return hash string }
the problem strlen
returns uint
, while cchmac
’s length arguments int
s.
while coercion, may use count
property of 2 arrays rather calling 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, ckey.count-1, cdata, cdata.count-1, &result) } else { // @martinr points out, in theory impossible // personally, prefer doing using `!` fatalerror("nil returned when processing input strings utf8") } let hash = nsmutablestring() val in result { hash.appendformat("%02hhx", val) } return hash string }
Comments
Post a Comment