i've been trying generate authentication api request follows: base64(sha256(payload+secret))
i've been using following code base64 string that's been generated isn't correct.
- (void)viewdidload { [super viewdidload]; nsstring *data = @"{" @"\"testing\":{" @"\"uri\":\"https://example.com/something.php\"," @"\"id\":\"0\"" @"}" @"}"; nsstring *key = @"secret"; nsstring *hashstring = [nsstring stringwithformat:@"%@%@",data,key]; const char *ckey = [hashstring cstringusingencoding:nsutf8stringencoding]; nsdata *sdata = [nsdata datawithbytes:ckey length:hashstring.length]; unsigned char shmac[64]; cc_sha256((__bridge const void *)(sdata), sdata.length, shmac); nsdata *hash = [[nsdata alloc] initwithbytes:shmac length:sizeof(shmac)]; nsstring *s = [self base64fordata:hash]; nslog(@"authentication: %@",s); } //base64 encoding - (nsstring*)base64fordata:(nsdata*)thedata { const uint8_t* input = (const uint8_t*)[thedata bytes]; nsinteger length = [thedata length]; static char table[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; nsmutabledata* data = [nsmutabledata datawithlength:((length + 2) / 3) * 4]; uint8_t* output = (uint8_t*)data.mutablebytes; nsinteger i; (i=0; < length; += 3) { nsinteger value = 0; nsinteger j; (j = i; j < (i + 3); j++) { value <<= 8; if (j < length) { value |= (0xff & input[j]); } } nsinteger theindex = (i / 3) * 4; output[theindex + 0] = table[(value >> 18) & 0x3f]; output[theindex + 1] = table[(value >> 12) & 0x3f]; output[theindex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3f] : '='; output[theindex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3f] : '='; } return [[nsstring alloc] initwithdata:data encoding:nsasciistringencoding]; }
i've been struggling 1 couple of weeks already. thank kind assistance!
you have comma in json dictionary. maybe messing up?
also, notice hashstring
looks this:
{"testing":{"uri":"https://example.com/something.php","id":"0",}}secret
is intended?
also, traditional c-code, has been done before. see e.g. this answer.
Comments
Post a Comment