Decode sha1 string to normal string

2020-03-31 05:50发布

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 help me to solve this issue.

Thanks in advance

标签: iphone
5条回答
我欲成王,谁敢阻挡
2楼-- · 2020-03-31 06:34

You can also use a rainbow table used for reversing cryptographic hash functions.

查看更多
做自己的国王
3楼-- · 2020-03-31 06:37

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.

查看更多
Evening l夕情丶
4楼-- · 2020-03-31 06:43

SHA1 is a one-way hash: it cannot be "decoded".

Hashing is used to turn a value into another probably-unique value. For example, you can fingerprint files by computing their hashes. Then, for example, you can know that you've successfully downloaded the right gigabyte of data by computing its hash and comparing it to the hash you expect.

Passwords are often hashed: I can check if you've entered the right password without having a copy of your password: I hash the password when you set it, and store that. Then when you enter your password, if it hashes to the same value, it's the right password.

查看更多
家丑人穷心不美
5楼-- · 2020-03-31 06:47

SHA1 digests are one way digests and original string cannot be retrieved back from the digest.

查看更多
闹够了就滚
6楼-- · 2020-03-31 06:47

You can't decode it. But if you are too desperate, Brute-force it!

查看更多
登录 后发表回答