Home > Software engineering >  In Rails, what is the equivalent of hash() PHP?
In Rails, what is the equivalent of hash() PHP?

Time:12-12

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

ref

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..."
  • Related