简体   繁体   English

Swift SHA256哈希与PHP SHA256哈希不匹配

[英]Swift SHA256 hash doedn't match PHP SHA256 hash

Has anyone had difficulty getting the hash results from swift to match the same input hash result in PHP? 有没有人很难从swift获得哈希结果以匹配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: 这是我的PHP代码:

$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. 我尝试在PHP哈希函数中将boolean标志更改为true,但这没有帮助。 Any ideas? 有任何想法吗? Thank you. 谢谢。

PBKDF2 is not a Digest. PBKDF2不是摘要。 You can't compare those two values. 您无法比较这两个值。

Anyway, this is how you calculate hash: https://github.com/krzyzanowskim/CryptoSwift#calculate-digest , basically: 无论如何,这是您计算哈希的方式: https : //github.com/krzyzanowskim/CryptoSwift#calculate-digest ,基本上是:

let hash = bytes.sha256()

in PHP hash('sha256',$longPassword,false); 在PHP hash('sha256',$longPassword,false); outputs lowercase hexits, so you need something like this: 输出小写的hexits,因此您需要这样的内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM