c# - Generating SHA256 in Swift (iOS) -


i trying generate sha256 using following function:-

 func generatehmac(key: string, data: string) -> string {     let keydata = key.datafromhexadecimalstring()! nsdata     let datain = data.datausingencoding(nsutf16stringencoding)     var result: [cunsignedchar]     result = array(count: int(cc_sha256_digest_length), repeatedvalue: 0)     cchmac(cchmacalgorithm(kcchmacalgsha256), keydata.bytes, keydata.length, datain!.bytes, datain!.length, &result)      let hash = nsmutablestring()     val in result {         hash.appendformat("%02hhx", val)     }      return hash string } 

input is

accountnumber: 100195 amount: 10  billerid: 59  channelid: 2  context: 11|test countryid: 1  customerid: 34 emailid: ankur.arya@me.com returnurl: https://uat.myfatoora.com/receiptpoc.aspx  securitytoken: 6b4a47a6-40a0-4c9d-a925-5ceca2910881  txnrefnum: 991107844408242  username: usp 

and output

4cd1acc736a9702c8cdb1a546d1c274a67cb285dbdbb972aab39ee51c2a2‌​26c8 

however, doesn’t match output of backend uses following algo

private string createsha256poc(bool userequest)     {         // hex decode secure secret use in using hmacsha256 hasher         // hex decoding eliminates source of error independent of character encoding         // hex decoding precise in converting byte array , preferred form representing binary values hex strings.          securehash = "";         byte[] convertedhash = new byte[_securesecret.length / 2];         (int = 0; < _securesecret.length / 2; i++)         {             convertedhash[i] = (byte)int32.parse(_securesecret.substring(i * 2, 2), system.globalization.numberstyles.hexnumber);         }          // build string collection in preperation hashed         stringbuilder sb = new stringbuilder();         sortedlist<string, string> list = (userequest ? requestfields : responsefields);         foreach (keyvaluepair<string, string> kvp in list)         {             // if (kvp.key.startswith("vpc_") || kvp.key.startswith("user_"))             sb.append(kvp.key + "=" + kvp.value + "&");         }         // remove trailing & string         if (sb.length > 0)             sb.remove(sb.length - 1, 1);          // create securehash on string         string hexhash = "";         using (hmacsha256 hasher = new hmacsha256(convertedhash))         {             byte[] hashvalue = hasher.computehash(encoding.utf8.getbytes(sb.tostring()));             foreach (byte b in hashvalue)             {                 hexhash += b.tostring("x2");                 securehash = hexhash;             }         }         return hexhash;     } 

and output

41d8e81c128100a76185f24ce00bc6a4fea30839e6de3dffbc3b5814e4fd0c4e 

secret key

71dd0f73affbb47825ff9864dde95f3b 

can please me update method in swift same result backend.

thanks.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -