我正在使用下面的代碼將字符串轉換爲sha1字符串,但我無法找到任何解決方案,即從sha1字符串中返回正常字符串。將sha1字符串解碼爲普通字符串
+(NSString *)stringToSha1:(NSString *)str{
const char *s = [str cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
// This is the destination
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA1(keyData.bytes, keyData.length, digest);
// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
NSLog(@"Hash is %@ for string %@", hash, str);
NSData *dtt = [hash dataUsingEncoding:NSUTF8StringEncoding];
//dtt = [nsda]
NSString *unhash = [dtt description];
return hash;
}
plz幫我解決了這個問題。
在此先感謝
+1 - 散列無法解碼的原因是「可能唯一」位 - 如果結果散列的宇宙小於可能值的宇宙,散列必然是多對一的過程。多對一算法只能在「這*可能*是原始值」的可信度下反轉, – 2010-08-16 10:41:51