简体   繁体   中英

SHA1 digest compare with String fingerprint in Swift

I have a string representation of web-site's fingerprint:

5D A8 E2 8A F7 D7 82 65 40 14 8C 8A 3C 4B 37 49 CE A4 B2 42

Then I need to match it with the certificate's hash, which I'm using CryptoKit for:

static func sha1FingerprintsForTrust(_ trust: SecTrust) -> [String] {
    var sha1s: [String] = []
    for index in 0..<SecTrustGetCertificateCount(trust) {
        if let certificate = SecTrustGetCertificateAtIndex(trust, index) {
            let der = SecCertificateCopyData(certificate) as NSData
            let sha1 = Insecure.SHA1.hash(data: der)
            print(sha1) //prints **SHA1 digest: 76e27ec14fdb82c1c0a675b505be3d29b4eddbbb**
        }
    }
    return sha1s
}

how can I convert any of those to match it with the other? Either way: Insecure.SHA1.Digest into String or String into Digest to then compare them?

Side note: I am aware that SHA1 is deprecated and should not be used anymore.

As usual the answer is pretty simple:

sha1.hexString 

That's the missing thing I was looking for. With that I get a String and the only thing left is to make format of those same (remove spaces and turn uppercased() ) and then compare them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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