Calculate a 256-bit SHA-2 hash from a CLOB
Calling dbms_crypto.hash
with the dbms_crypto.hash_sh256
creates a 256-bit SHA-2 hash.
declare
h raw(32); -- 32 * 8-bit = 256-bit
c clob := q'[foo
bar
baz]';
begin
h := dbms_crypto.HASH(c, dbms_crypto.hash_sh256);
dbms_output.put_line('hash: ' || rawtohex(h));
end;
/
This simple example prints
hash: 33B28259443FA31A0052030C1E5482E2145219AD02B6E226CD5D3F096FC7E897
Calculate an MD5 hash from a string (varchar2)
declare
txt varchar2(100) := 'foo, bar and baz.';
md5 raw(16); -- MD5 is 128 = 16 * 8 bits
begin
md5 := dbms_crypto.hash(
utl_i18n.string_to_raw(txt, 'AL32UTF8'),
dbms_crypto.hash_md5
);
dbms_output.put_line('MD5 is: ' || md5);
end;
/