Search code examples
crystal-lang

Crystal: How can I find the SHA256 hash of a binary value?


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?


Solution

  • Decoding a string of hex characters into a binary slice isn't currently part of the crystal standard library, so I wrote a decoding function myself:

    def hex_decode(hex)
      return unless hex.size % 2 == 0
    
      slice = Slice(UInt8).new(hex.size / 2)
      0.step(to: hex.size - 1, by: 2) do |i|
        high_nibble = hex.to_unsafe[i].unsafe_chr.to_u8?(16)
        low_nibble = hex.to_unsafe[i + 1].unsafe_chr.to_u8?(16)
        return unless high_nibble && low_nibble
    
        slice[i / 2] = (high_nibble << 4) | low_nibble
      end
    
      slice
    end
    

    This function takes a String containing hexadecimal characters, then decodes it into a Slice(UInt8) (or returns nil if the hex is invalid).

    Then the full code equivalent to the ruby code you pasted above would be:

    def hash256(hex_string)
      data = hex_decode(hex_string)
      raise "Invalid hexadecimal" unless data
    
      hash = OpenSSL::Digest.new("SHA256")
      hash.update(data)
      hash1 = hash.digest
    
      hash = OpenSSL::Digest.new("SHA256")
      hash.update(hash1)
    
      hash.hexdigest
    end
    

    Although I have to question why you would want to use SHA256 twice. I would recommend changing the hash function like so:

    def hash256(hex_string)
      data = hex_decode(hex_string)
      raise "Invalid hexadecimal" unless data
    
      hash = OpenSSL::Digest.new("SHA256")
      hash.update(data)
    
      hash.hexdigest
    end