In PHP there are hash() function that accept 'sha256' and string as arguments
Docs: https://www.php.net/manual/en/function.hash.php
Example:
$token = hash('sha256', $str);
I tried to find an equivalent function in rails but I can only find the
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, content)
that need a key
to hash.
Is there any function like $token = hash('sha256', $str);
in Rails that dont need the key ?
CodePudding user response:
You can try this way
require 'digest'
# Compute a complete digest
Digest::SHA256.digest 'message' #=> "\xABS\n\x13\xE4Y..."
sha256 = Digest::SHA256.new
sha256.digest 'message' #=> "\xABS\n\x13\xE4Y..."
# Other encoding formats
Digest::SHA256.hexdigest 'message' #=> "ab530a13e459..."
Digest::SHA256.base64digest 'message' #=> "q1MKE RZFJgr..."