-1

我正在使用以下代码(CC_SHA256)对 NSString 输入进行编码。有人可以帮助我使用相同的逻辑以解码格式检索吗?

    -(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;
}
4

2 回答 2

2

你不能 SHA 是一种散列算法,而不是要解码。

就像Jonathan Grynspan所说,如果你可以解码 sha(任何版本),就会违背这种算法的目的。

于 2012-04-16T13:56:49.783 回答
1

正如其他人所指出的,SHA-1 和 SHA-2 变体在设计上是单向哈希。如果您可以反转它们,则散列被破坏。哈希旨在检查数据完整性,而不是提供数据加密。

如果你想要加密/解密而不是散列,你想要使用 CommonCrypto 的CCCryptor例程之一。看:

任何用于AES加密解密的可可源代码?

于 2012-04-16T14:32:50.857 回答