Lawrence D'Anna
Lawrence D'Anna

Reputation: 3124

How can I create a SHA256Digest from a hex string or bytes?

How can I create a SHA256Digest, or any of the other digest types in CryptoKit, using a Data, or a hexadecimal String as the input?

In other words, how do I write the inverse of this function?

func toHex(hash: SHA256Digest) -> String {
    return String(hash.flatMap { byte in
        String(format:"%02x", byte)
    })
}

I can't find a constructor or another reasonable way to do it.

edit:

It can be done with unsafe pointers, but I still wish there were a safe way to do it.

extension Digest {
    
    func toHex() -> String {
        return String(self.flatMap { byte in
            String(format:"%02x", byte)
        })
    }
    
    static func fromHex(_ string : String) -> Self? {
        let data = UnsafeMutableRawBufferPointer.allocate(byteCount: Self.byteCount, alignment: 8)
        defer { data.deallocate() }
        let utf8 = string.utf8
        if utf8.count != 2 * Self.byteCount { return nil }
        func digit(_ i : Int) -> Int? {
            let byte = utf8[utf8.index(utf8.startIndex, offsetBy: i)]
            return Character(UnicodeScalar(byte)).hexDigitValue
        }
        for i in stride(from: 0, to: 2*Self.byteCount, by: 2) {
            guard
                let high = digit(i),
                let low = digit(i+1)
            else { return nil }
            data[i/2] = UInt8(high * 16 + low)
        }
        return data.bindMemory(to: Self.self)[0]
    }
}

Upvotes: 0

Views: 625

Answers (0)

Related Questions