如何在 Rails 中使用 hash_hmac 使用 sha256 加密数据,就像这个 php 函数一样

THp*_*ubs 5 php ruby hash ruby-on-rails hmac

我需要将此 php 函数转换为 Rails。它用于使用特殊密钥对我们提供的数据进行加密。该函数的输出应与 ruby​​ 函数匹配。请帮忙。

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

小智 5

require 'openssl'

def genHash(secret, data)
  OpenSSL::HMAC.hexdigest('sha256', secret, data)
end
Run Code Online (Sandbox Code Playgroud)