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);
}
}