在Swift 3中的ansi字符串上使用MD5哈希值

问题描述 投票:0回答:3

我有一个小函数,它接受一个字符串并返回其MD5哈希值。问题是,它需要一个UTF8字符串,我需要它来计算用iso-8859-1(~ansi)编码的字节数组的哈希值。

如何更改以下代码以接受字节数组,然后返回其哈希值?

static func md5(_ string: String) -> String {
    let context = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
    var digest = Array<UInt8>(repeating:0, count:Int(CC_MD5_DIGEST_LENGTH))
    CC_MD5_Init(context)
    CC_MD5_Update(context, string, CC_LONG(string.lengthOfBytes(using: String.Encoding.utf8)))
    CC_MD5_Final(&digest, context)
    context.deallocate(capacity: 1)
    var hexString = ""
    for byte in digest {
        hexString += String(format:"%02x", byte)
    }

    return hexString
}

棘手的部分是CC_MD5_Update调用。谢谢。

swift hash md5
3个回答
1
投票

您可以轻松修改函数以将任意字节数组作为参数。 CC_MD5_Update映射到Swift as

func CC_MD5_Update(_ c: UnsafeMutablePointer<CC_MD5_CTX>!, _ data: UnsafeRawPointer!, _ len: CC_LONG) -> Int32

你可以传递一个数组作为UnsafeRawPointer参数:

func md5(bytes: [UInt8]) -> String {
    var context = CC_MD5_CTX()
    var digest = Array<UInt8>(repeating:0, count:Int(CC_MD5_DIGEST_LENGTH))
    CC_MD5_Init(&context)
    CC_MD5_Update(&context, bytes, CC_LONG(bytes.count))
    CC_MD5_Final(&digest, &context)
    return digest.map { String(format: "%02hhx", $0) }.joined()
}

(我也简化了一下。)

或者,传递一个Data参数:

func md5(data: Data) -> String {
    var context = CC_MD5_CTX()
    var digest = Array<UInt8>(repeating:0, count:Int(CC_MD5_DIGEST_LENGTH))
    CC_MD5_Init(&context)
    data.withUnsafeBytes {
        _ = CC_MD5_Update(&context, $0, CC_LONG(data.count))
    }
    CC_MD5_Final(&digest, &context)
    return digest.map { String(format: "%02hhx", $0) }.joined()
}

然后可以用作

let s = "foo"
if let data = s.data(using: .isoLatin1) {
    let hash = md5(data: data)
    print(hash)
}

Swift 5的更新:

import CommonCrypto

func md5(data: Data) -> String {
    var context = CC_MD5_CTX()
    var digest = Array<UInt8>(repeating:0, count:Int(CC_MD5_DIGEST_LENGTH))
    CC_MD5_Init(&context)
    data.withUnsafeBytes {
        _ = CC_MD5_Update(&context, $0.baseAddress, CC_LONG(data.count))
    }
    CC_MD5_Final(&digest, &context)
    return digest.map { String(format: "%02hhx", $0) }.joined()
}

0
投票

如果您确定'string'只包含utf8个字符,请使用string.utf8调用CC_MD5_Update,这样:

CC_MD5_Update(context, string.utf8, CC_LONG(string.lengthOfBytes(using: String.Encoding.utf8)))

swift中的字符串是“有趣的”,这是一个很好的阅读主题:https://oleb.net/blog/2016/08/swift-3-strings/


0
投票
// requires a bridging header with this:
// #import <CommonCrypto/CommonCrypto.h>
func MD5(_ string: String) -> String? {
    let length = Int(CC_MD5_DIGEST_LENGTH)
    var digest = [UInt8](repeating: 0, count: length)
    if let d = string.data(using: String.Encoding.utf8) {
        d.withUnsafeBytes { (body: UnsafePointer<UInt8>) in
            CC_MD5(body, CC_LONG(d.count), &digest)
        }
    }
    return (0..<length).reduce("") {
        $0 + String(format: "%02x", digest[$1])
    }
}

贾斯汀答案:https://gist.github.com/jstn/787da74ab4be9d4cf3cb

© www.soinside.com 2019 - 2024. All rights reserved.