0

I'm trying to get the same hashing result between Oracle PL/SQL and .Net using SHA256 MAC with a key. However I'm getting different results. What do i need to modify? Here are the two functions:

Considering inputs: Text="Testing12345" and Key="1234567890123456"

Oracle Result: 88435052AFA0D89F017183A9AC2BEA1A4C1DA6384481435698DBC4602BFB835F2F0D99A2BA4252C1258EBEE4F439DAB8

.Net Result: PSLL/JXAD3JMYN1WJC1XDYOZJCFDZE4BWVI2+CGHJMK

Oracle PL/SQL Function:

CREATE OR REPLACE FUNCTION Hash_Val(p_txt IN VARCHAR2, 
                                        p_key IN VARCHAR2)

    RETURN VARCHAR2
    IS
         raw_txt RAW(4000) := utl_raw.cast_to_raw(p_txt);
         raw_key RAW(128) := utl_raw.cast_to_raw(p_key);
         v_hashed RAW(4000);
    BEGIN

    v_hashed := dbms_crypto.mac(src => raw_txt, 
                                        key => raw_key,
                                        typ => dbms_crypto.hash_sh256);

    RETURN v_hashed;

END;

.Net function:

public static string HashMACSHA256(string text, string key)
        {
            var encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(key);
            byte[] messageBytes = encoding.GetBytes(text);
            using (var hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                return Convert.ToBase64String(hashmessage);
            }
        }
wolφi
  • 8,091
  • 2
  • 35
  • 64
ZiggY
  • 1
  • 1
  • Your Oracle result appears to be in hex, your .NET result appears to be in Base64. You need to compare them in the same format. – rossum Jun 10 '20 at 16:07
  • @rossum thanks for the reply. You'r right... so i modified the .Net code to convert the hashmessage variable (a byte[]) to a hex string like this BitConverter.ToString(hashmessage).Replace("-",""). I got the following result which is still different from the one in Oracle: 3D2965FC9C5A0F726660DD568C2D57772A338DC143644E015AF236F828072669 – ZiggY Jun 11 '20 at 05:36

0 Answers0