Swift SHA256 hash doedn't match PHP SHA256 hash

Paul K.

Has anyone had difficulty getting the hash results from swift to match the same input hash result in PHP? Here's my swift code:

let longPassword = myTextField.text!+email
let pword: Array<UInt8> = Array(longPassword.utf8)
let keyString = self.runHashOnPassword(text: pword, salt: Array(salt.utf8))

func runHashOnPassword(text: Array<UInt8>, salt: Array<UInt8>) -> String {
    do {
        let key = try PKCS5.PBKDF2(password: text,salt: salt, iterations: 4096, variant: .sha256).calculate()
        let jEncoder = JSONEncoder.init()
        let byteString = try jEncoder.encode(key)
        return byteString.base64EncodedString()
    } catch {
        return ""
    }
}

And here is my PHP code:

$longPassword = $pword . $email . $salt;
$newHash = hash('sha256',$longPassword,false);

I have tried altering the boolean flag to true in the PHP hash function but it didn't help. Any ideas? Thank you.

Marcin

PBKDF2 is not a Digest. You can't compare those two values.

Anyway, this is how you calculate hash: https://github.com/krzyzanowskim/CryptoSwift#calculate-digest, basically:

let hash = bytes.sha256()

in PHP hash('sha256',$longPassword,false); outputs lowercase hexits, so you need something like this:

let hash = bytes.sha256().toHexString()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related