I am trying to write a function to create a sha512 string in an object from a .net function that
public static string GetSHA512(string strPlain) { UnicodeEncoding UE = new UnicodeEncoding(); byte[] HashValue = null; byte[] MessageBytes = UE.GetBytes(strPlain); System.Security.Cryptography.SHA512Managed SHhash = new System.Security.Cryptography.SHA512Managed(); string strHex = string.Empty; HashValue = SHhash.ComputeHash(MessageBytes); foreach (byte b in HashValue) { strHex += String.Format("{0:x2}", b); } return strHex; }
This gives the result as
input : pass123 output: 2a6353744cc2914c602265f50d2e413d0561368775756392517abb340ef75d52ee0c5d3623ddd1826fd768a13dca8961f5957c75df0d793b9d7537aabe050705
What I tried looks like this
-(NSString *)createSHA512:(NSString *)string { const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding]; NSData *data = [NSData dataWithBytes:cstr length:string.length]; uint8_t digest[CC_SHA512_DIGEST_LENGTH]; CC_SHA512(data.bytes, data.length, digest); NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH]; for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) [output appendFormat:@"%02x", digest[i]]; return output; }
which gives the result below
input : pass123 output: fd37ca5ca8763ae077a5e9740212319591603c42a08a60dcc91d12e7e457b024f6bdfdc10cdc1383e1602ff2092b4bc1bb8cac9306a9965eb352435f5dfe8bb0
Can anyone suggest what I am doing wrong?
Why are these two meanings different?
Please correct my mistakes.
EDIT
Medium While I tried to change the encoding to NSUTF16StringEncoding and NSUnicodeStringEncoding , the results of which are still different and as it should
input : pass123 output: 514331e3f7ca0a295539347ebccc4e4f095fe5f3c1df10d43b4d550144c7b30ba9507831893ea63ea22e62e993be529b0d14be7800a90aa0de199d6be62a5f1b
source share