本文介绍了如何在Swift中将字符串哈希到SHA512?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个社交媒体应用程序,我希望获得一些帮助,以在Swift中将密码字符串编码为SHA512.我在GitHub上找到了CryptoSwift库,但是我很难将其加载到我的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

推荐答案

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: "")
    }

}

这篇关于如何在Swift中将字符串哈希到SHA512?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:58