Search notes:

Oracle: DBMS_CRYPTO.HASH

The dbms_crypto.hash function takes two parameters:
The function creates the hash value for the first parameter as per the desired hash and returns a raw.

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;
/
Github repository Oracle-patterns, path: /Installed/dbms/crypto/hash/sha-256.sql
This simple example prints
hash: 33B28259443FA31A0052030C1E5482E2145219AD02B6E226CD5D3F096FC7E897

Calculate an MD5 hash from a string (varchar2)

In order to calculate a hash from a varchar2, the value of the string might be converted to a raw (otherwise an ORA-06502: PL/SQL: numeric or value error: hex to raw conversion error is thrown).
Such a conversion is possible with utl_i18n.string_to_raw.
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;
/
Github repository Oracle-patterns, path: /Installed/dbms/crypto/hash/md5.sql

See also

The
dbms_sqlhash
dbms_crypto

Index