简体   繁体   中英

SHA256 Base64 Hash in Ruby

We are trying to implement the SHA256 Base64 hash in ruby which is not returning the expected results as in C#.

Below is our C# sample code.

    public static string HashSHA256ToBase64(string phrase)
    {
        if (phrase == null)
            return null;
        var encoder = new UTF8Encoding();
        var sha256Hasher = new SHA256CryptoServiceProvider();
        var hashedDataBytes = sha256Hasher.ComputeHash(encoder.GetBytes(phrase));

        return Convert.ToBase64String(hashedDataBytes);
    }

For this, we need to write equivalent code in ruby. For which we are trying as follows.

Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), "", phrase))

It is not providing the same results as C#.

Test phrase : V2dcZBpzzglD1ynW5ZAyFocs9wtpR624wlla9gujw0I=RquZ/QzazPM=

Expected Result : utXwt733s9FmiSM69o2zGOm0IT42FjthbB0oquIuPak=

Can someone help me with the equivalent ruby code to resolve this

下面的代码解决了这个问题

Digest::SHA256.base64digest(phrase)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM