create a sha256 hash from a string in ruby

To create a SHA256 hash from a string in Ruby, you can use the OpenSSL library that comes with Ruby by default. Here is an example code snippet:

main.rb
require 'openssl'

string_to_hash = "Hello World!"
sha256_hash = OpenSSL::Digest::SHA256.hexdigest(string_to_hash)

puts sha256_hash
133 chars
7 lines

In this code, we first require the openssl library. Then we define the string that we want to hash. Finally, we use the OpenSSL::Digest::SHA256 class to create a SHA256 hash object, and then call the hexdigest method on it, passing in the string we want to hash. The hexdigest method returns the hexadecimal representation of the hash value as a string.

When you run this code, it will output the SHA256 hash of the string "Hello World!". You can replace this string with any other string that you want to hash.

gistlibby LogSnag