Oracle STANDARD_HASH not available in PLSQL?

For text, STANDARD_HASH is the same as DBMS_CRYPTO.HASH with SHA1:

begin
  dbms_output.put_line(dbms_crypto.hash(cast('test' as clob), dbms_crypto.hash_sh1));
end;
/

Output:

A94A8FE5CCB19BA61C4C0873D391E987982FBBD3

For other data types, it's not documented how they are passed to the hash function.


Seems like it isn't yet a part of PL/SQL in 12c.

As a workaround, use SELECT INTO in PL/SQL:

SQL> set serveroutput on
SQL> DECLARE
  2    str VARCHAR2(40);
  3  BEGIN
  4    SELECT STANDARD_HASH('test') INTO str FROM dual;
  5    dbms_output.put_line(str);
  6  END;
  7  /
A94A8FE5CCB19BA61C4C0873D391E987982FBBD3

PL/SQL procedure successfully completed.

SQL>

I would suggest to create a function, and use it whenever you need it in PL/SQL.

For example,

SQL> CREATE OR REPLACE FUNCTION STANDARD_HASH_OUTPUT(str IN VARCHAR2)
  2    RETURN VARCHAR2
  3  AS
  4    op VARCHAR2(40);
  5  BEGIN
  6    SELECT STANDARD_HASH(str) INTO op FROM dual;
  7    RETURN op;
  8  END;
  9  /

Function created.

Call the function directly in PL/SQL block:

SQL> BEGIN
  2     dbms_output.put_line(STANDARD_HASH_OUTPUT('test'));
  3  END;
  4  /
A94A8FE5CCB19BA61C4C0873D391E987982FBBD3

PL/SQL procedure successfully completed.

SQL>

Tags:

Oracle

Hash

Plsql