将sha1字符串解码为普通字符串

将sha1字符串解码为普通字符串

本文介绍了将sha1字符串解码为普通字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将字符串转换为sha1字符串,但我无法找到任何反向的解决方案,即来自sha1字符串的正常字符串。

I am using following code to convert string to sha1 string but i am not able to find any solution for reverse of this i.e normal string from sha1 string.

+(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帮我解决这个问题。

plz help me to solve this issue.

提前致谢

推荐答案

SHA-1是一种单向哈希算法,所以没有'用于从SHA-1字符串转换为普通字符串的算法。

SHA-1 is a one-way hash algorithm, so there isn't an algorithm to go from a SHA-1 string to a normal string.

这篇关于将sha1字符串解码为普通字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:01