I'm new to Crystal. I'd like to try and find the SHA256 hash of a hex string. I've managed to get something working:
sha256 = OpenSSL::Digest.new("sha256")
puts sha256.update("abcd")
But I'm not sure how to put the binary value of "abcd" in to the hash function, or get binary out. I'd basically like to be able to recreate this Ruby function:
def hash256(hex)
# 1. Convert hex string to array, and pack in to binary
binary = [hex].pack("H*")
# 2. Hash the binary value (returns binary)
hash1 = Digest::SHA256.digest(binary)
# 3. Hash it again (returns binary)
hash2 = Digest::SHA256.digest(hash1)
# 4. Convert back to hex (must unpack as array)
result = hash2.unpack("H*")[0]
return result
end
Is it possible to use SHA256 with binary values in Crystal?