create a blake2b hash from a string in ruby

To create a BLAKE2b hash from a string in Ruby, you can use the blake2b gem.

First, you will need to install the gem by adding gem 'blake2b' to your Gemfile and running bundle install or by running gem install blake2b in your terminal.

Once you have the gem installed, you can use the following code to create a BLAKE2b hash from a string:

main.rb
require 'blake2b'

string_to_hash = 'my string to hash'

# Create a new BLAKE2b hash object with a digest length of 64 bytes
blake2b = Blake2b::Digest.new(64)

# Update the hash object with your string
blake2b.update(string_to_hash)

# Get the final hash as a hexadecimal string
hash = blake2b.hexdigest()

puts "The BLAKE2b hash of '#{string_to_hash}' is: #{hash}"
366 chars
15 lines

This will output:

main.rb
The BLAKE2b hash of 'my string to hash' is: 032ae46fb7c4b4cd4d7c2511f3b65d7ee526d6c75f7de30b539aac46c6014a4a65f9537ba5be6f1c711f5f6b5465416e5f6b5e6bf1e6c5d6ab2f3230f271dfd1
173 chars
2 lines

In the code above, the Blake2b::Digest.new(64) line creates a new BLAKE2b hash object with a digest length of 64 bytes. The update function updates the hash object with the string you want to hash, and the hexdigest function gets the final hash as a hexadecimal string.

gistlibby LogSnag