c# - Getting HMACSHA1 correctly on iPhone using openssl -
argh, head hurts!
context
i need write c/objective-c code decrypt files stored on remote server. encryption open-source , written in c#
i'm using libssl.a , libcrypto.a project;
https://github.com/x2on/openssl-for-iphone
my first hurdle getting hmacsha1 of string correct :( sha1 seems correct according reference site here - http://hash.online-convert.com/sha1-generator hmac not. ok, code;
nsstring *input = @"wombats"; // define strings etc need unsigned char *instrg = malloc(256); instrg = (unsigned char *)[input utf8string]; unsigned long lngth = [input length]; unsigned char result [evp_max_md_size]; unsigned char hmacresult [evp_max_md_size]; unsigned char newpassphrase[25]; memset(newpassphrase, 0, 25); unsigned char salt[] = "\x01\x02\x03\x04\x05\x06\x07\x08"; // real code requires re-hash input 1000 times. lets // check 10 (hey first wrong). (int i=0; i<10; i++) { // use openssl sha_ctx shacontext; sha1_init(&shacontext); sha1_update(&shacontext, instrg, lngth ); sha1_final(result, &shacontext); unsigned int len; // if @ result now, matches reference, think next stage goes wrong. hmac(evp_sha1(),salt,8,result,20,hmacresult,&len); printf("hmac ----------------- : "); hexprint(hmacresult,len); // copy result input can re-hash; lngth = len; memcpy((char *)instrg,(char *)hmacresult,len); }
the code trying match following c# fragment. code generates correct output , extract production server.
byte[] salt={0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}; utf8encoding utf8 = new utf8encoding(); byte[] data = utf8.getbytes("wombats"); hmacsha1 sha1= new hmacsha1(); sha1.key = salt; for(int i=0; i<10; i++) { sha1.computehash(data); data = sha1.hash; console.writeline(system.bitconverter.tostring(data)); }
if compile , run c# code, output following sequence;
dc-f3-1c-c1-2f-8a-86-16-cc-74-1e-da-25-7b-ff-16-6e-eb-2a-0f e7-6a-f4-c6-73-07-db-e8-52-82-9e-d1-96-bf-06-95-85-c6-94-f4 1f-e2-61-bc-f0-08-92-3c-14-70-2b-89-eb-46-c9-20-a5-90-65-9b etc
if @ output iphone version, following;
44-35-5b-bb-41-0e-bc-c3-d3-58-62-e6-5c-a0-51-6b-bc-97-11-f6 4c-3b-61-bd-1f-f7-72-13-af-1b-5e-5e-e3-70-8a-31-08-11-67-f2 41-56-8d-41-c1-3e-02-d0-d2-76-ad-d3-49-ba-71-ff-34-d7-3e-8b etc
so there wrong.
if check sha1 part, seems correct, i'm guessing either i'm using hmac wrong parameters, or c# hmacsha1 different expectations.
i should add if use suitable ascii salt, result of online generator here http://hash.online-convert.com/sha1-generator matches c# code leads me suspect c on iphone wrong.
can assist me working out problem is, i'm running out of ideas @ moment!
removing duplicate sha1 fixes it. doh!
sha_ctx shacontext; sha1_init(&shacontext); sha1_update(&shacontext, instrg, lngth ); sha1_final(result, &shacontext);
delete of this.
Comments
Post a Comment