-4

I create an SHA512 Hash with following code:

func createSHA512(source:String) -> String {
    let rawstr:NSString = NSString(format: "%@%@", source, "xxxxxxxxxxxxxx");
    let data = rawstr.dataUsingEncoding(NSUTF8StringEncoding)!;
    var digest = [UInt8](count:Int(CC_SHA512_DIGEST_LENGTH), repeatedValue: 0);
    CC_SHA512(data.bytes, CC_LONG(data.length), &digest);
    let output = NSMutableString(capacity: Int(CC_SHA512_DIGEST_LENGTH));
    for byte in digest {
        output.appendFormat("%02x", byte);
    }
    return output as String;
}

Is it possible to decrypt this Hash with Swift 2 to the original String?

Wain
  • 118,658
  • 15
  • 128
  • 151
  • 2
    The very purpose of a hash is that it cannot be undone (unhashed, decrypted). It's not a method of encryption. – Codo Aug 24 '16 at 08:09
  • This is only possible through brute force or if you have a rainbow table that contains hashes and their known "originals". You can't decrypt a hash. – donnywals Aug 24 '16 at 08:10
  • 2
    A SO search for `decrypt sha hash` has (currently) 351 hits. I cannot imagine that none of them answered your question. – Martin R Aug 24 '16 at 08:17

1 Answers1

3

SHA is a hash, not an encryption. A hash does not include all of the information in the original and it's a one-way function. It's a signature of the original data, not a version of it.

So no, it can not be undone to retrieve the original.

The only option to 'reverse' would be to create guesses of the original and try to hash them and see if the result matches. Note that hashes aren't unique, so even if you got a match you couldn't 100% guarantee that it matched the original, only that it hashes to the same value.

Wain
  • 118,658
  • 15
  • 128
  • 151