Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hash a string to SHA512 in Swift?

I am building a social media application and I would like some help encoding a password string to SHA512 in Swift. I found the CryptoSwift library on GitHub but I am having a hard time loading it into my Swift project and linking it to my project files. Does anyone know how to accomplish this relatively easily? Thanks in advance, Kyle

like image 899
TechnologyGuy Avatar asked May 17 '15 05:05

TechnologyGuy


People also ask

Is SHA512 a hash?

SHA-512 is a hashing algorithm that performs a hashing function on some data given to it. Hashing algorithms are used in many things such as internet security, digital certificates and even blockchains.

Is SHA512 one way hash?

The Secure Hash Standard specifies five SHAs: SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512. All five of the algorithms are iterative, one-way hash functions that can process a message to produce a condensed representation called a message digest.

Can you crack SHA512?

Technically speaking SHA512 password hashes are not cracked or decrypted . They are matched using a list of possible passwords, it is more akin to reversing than breaking.

What format is SHA512?

SHA-512, or Secure Hash Algorithm 512, is a hashing algorithm used to convert text of any length into a fixed-size string. Each output produces a SHA-512 length of 512 bits (64 bytes). This algorithm is commonly used for email addresses hashing, password hashing, and digital record verification.


2 Answers

Solution for Swift 3:

extension String {

    func sha512() -> String {
        let data = self.data(using: .utf8)!
        var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
        data.withUnsafeBytes({
            _ = CC_SHA512($0, CC_LONG(data.count), &digest)
        })
        return digest.map({ String(format: "%02hhx", $0) }).joined(separator: "")
    }

}
like image 98
sundance Avatar answered Sep 29 '22 16:09

sundance


I find all of the answer ok, but if we should have a true universal solution I think we need to step it up a level.

CC_LONG is just an UInt32 and will not support really large data structures.

This is my solution in Swift 3:

First we create a foundation:

struct Sha512 {
    let context = UnsafeMutablePointer<CC_SHA512_CTX>.allocate(capacity:1)

    init() {
        CC_SHA512_Init(context)
    }

    func update(data: Data) {
        data.withUnsafeBytes { (bytes: UnsafePointer<Int8>) -> Void in
            let end = bytes.advanced(by: data.count)
            for f in sequence(first: bytes, next: { $0.advanced(by: Int(CC_LONG.max)) }).prefix(while: { (current) -> Bool in current < end})  {
                _ = CC_SHA512_Update(context, f, CC_LONG(Swift.min(f.distance(to: end), Int(CC_LONG.max))))
            }
        }
    }

    func final() -> Data {
        var digest = [UInt8](repeating: 0, count:Int(CC_SHA512_DIGEST_LENGTH))
        CC_SHA512_Final(&digest, context)

        return Data(bytes: digest)
    }
}

For convenience we do an extension for Data:

extension Data {
    func sha512() -> Data {
        let s = Sha512()
        s.update(data: self)
        return s.final()
    }
}

And last an extension for String:

extension String {
    func sha512() -> Data {
        return self.data(using: .utf8)!.sha512()
    }
}

This solution can be used for Sha256, MD5 etc. to get a good true universal solutions with Apple's CommonCrypto.

like image 35
Martin Carlberg Avatar answered Sep 29 '22 15:09

Martin Carlberg