THpubs
THpubs

Reputation: 8172

How to encrypt data with sha256 with hash_hmac in Rails like in this php function

I need to convert this php function to Rails. It is used to encrypt the data we supply with a special key. The output of this function should match the ruby function. Please help.

public static function genHash($secret, $data) {
    $ourhash = hash_hmac('sha256', utf8_decode($data), utf8_decode($secret), FALSE);
    return $Hmac;
}

Upvotes: 5

Views: 2888

Answers (1)

greg
greg

Reputation: 66

require 'openssl'

def genHash(secret, data)
  OpenSSL::HMAC.hexdigest('sha256', secret, data)
end

Upvotes: 5

Related Questions