-1

I am encoding NSString input using the following code (CC_SHA256). Could someone help me retrieving in decoded format using the same logic?

    -(NSString*) encodeAndGetHashInfo :(NSString *) inStringToHashIt
{
    NSDate *currentDate = [NSDate date];
    NSLog(@"currentDate %@",currentDate);


    NSTimeInterval currTimeMillsecs = ([currentDate timeIntervalSince1970] * 1000);
    NSString *strCurrTimeMilliSecs = [NSString stringWithFormat:@"%.0f", currTimeMillsecs]; 
    NSLog(@"strCurrTimeMilliSecs: %@", strCurrTimeMilliSecs);  //here we are getting millsec in  this way 1328962624994.734131
    //double currentTime=[strCurrTimeMilliSecs doubleValue];

    //Do hashing    
    NSString *withSalt= [NSString stringWithFormat:@"%@%@%@", strCurrTimeMilliSecs, inStringToHashIt,STATIC_HASH];

    NSLog(@"withSalt.%@",withSalt);

    NSString *inputStr = withSalt;

    unsigned char hashedChars[32];
    CC_SHA256([inputStr UTF8String],
              [inputStr lengthOfBytesUsingEncoding:NSUTF8StringEncoding], 
              hashedChars);

    NSData * hashedData = [NSData dataWithBytes:hashedChars length:32];
    NSLog (@"hashedData:%@",hashedData );
    NSString* hashPasswordResponse = NULL;
    hashPasswordResponse = [NSString stringWithFormat:@"%@", hashedData];       

    hashPasswordResponse = [hashPasswordResponse stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSLog(@"hashPasswordResponse.......%@",hashPasswordResponse);//this string is 

    return hashPasswordResponse;
}
Getsy
  • 4,887
  • 16
  • 78
  • 139

2 Answers2

2

You can't SHA is a hashing algoritm, not ment to be decoded.

Like Jonathan Grynspan said, if you could decode sha (any version) would defeat the purpose of such an algorithm.

Community
  • 1
  • 1
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • True, but with rainbowtables, it could would be possible to get a value that would create the same hash, thats why you should al ways add a salt ;) – rckoenes Apr 16 '12 at 14:06
  • Not just possible: certain. Since SHA-256 output is always the same length, the pigeonhole principle applies. – Jonathan Grynspan Apr 16 '12 at 14:07
1

As the others have noted, SHA-1 and the SHA-2 variants are by design one-way hashes. If you can reverse them, the hashes are broken. The hashes are designed to check data integrity, not to provide data encryption.

If you want encryption/decryption as opposed to hashing, you want to use one of CommonCrypto's CCCryptor routines. See:

Any cocoa source code for AES encryption decryption?

Community
  • 1
  • 1
more tension
  • 3,312
  • 17
  • 19