Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Rails, what is the equivalent of hash() PHP?

In PHP there are hash() function that accept ‘sha256’ and string as arguments

Docs: https://www.php.net/manual/en/function.hash.php

Example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  $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 ?

>Solution :

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..."
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading