views:

50

answers:

3

Hi,when i use this code for generate an hash256 in my iPhone app:

 unsigned char hashedChars[32];
  NSString *inputString;
  inputString = [NSString stringWithFormat:@"hello"];
  CC_SHA256([inputString UTF8String],
      [inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding ], 
      hashedChars);
  NSData * hashedData = [NSData dataWithBytes:hashedChars length:32];

The hash256 of inputString, is created correctly, but if i use a string like this @"\x00\x25\x53\b4", the hash256 is different from the real string with "\x" characters. I think that the problem is in encoding "UTF8" instead of ascii. Thanks!

+1  A: 

I would be suspicious of the first character, "\x00" - thats going to terminate anything that thinks its dealing with "regular C strings".

Not sure whether lengthOfBytesUsingEncoding: takes that stuff into account, but its something I'd experiment with.

Jeff
The problem is:If i try to insert "\x" characters, the resulting sha256 is incorrect and if i log the string with these characters, is different from equal string written in python for example. Excuse my crappy english.
pakizip
A: 

You're getting the bytes with [inputString UTF8String] but the length with [inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding]. This is obviously wrong. Moreover (assuming you mean "\xB4" and that it turns into something not in ASCII), "\xB4" is not likely to be in ASCII. The docs for NSString say

Returns 0 if the specified encoding cannot be used to convert the receiver

So you're calculating the hash of the empty string. Of course it's wrong.

You're less likely to have problems if you only generate the data once:

NSData * inputData = [inputString dataUsingEncoding:NSUTF8StringEncoding];
CC_SHA256(inputData.bytes, inputData.length, hashedChars);
tc.
Anyway the hash is incorrect. Any ideas?
pakizip
I posted my idea.
tc.
A: 

Nothing, the hash256 is incorrect, anyway. A solution? Thanks

pakizip