Close

Using java to extend dbms_crypto

I don’t normally write a lot of java stored procedures.  They simply aren’t necessary most of the time; but today somebody asked me a question that just seemed a perfect fit.  They wanted to know how to generate a SHA-256 hash for a given string.  For most hashing tasks I’d recommend Oracle’s built-in package DBMS_CRYPTO; but it can only support the SHA1 algorithm, for 256 bits we need to go to SHA2.  Fortunately, implementing such a thing in java is quite simple.

Using java.security.MessageDigest we can implement 256, 384 and 512 bit algorithms.

Here’s a simple function that supports all three.

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED sha2 AS
import java.security.MessageDigest;
import oracle.sql.*;

public class sha2
{
public static oracle.sql.RAW get_digest( String p_string, int p_bits ) throws Exception
{
MessageDigest v_md = MessageDigest.getInstance( “SHA-” + p_bits );
byte[] v_digest;
v_digest = v_md.digest( p_string.getBytes( “UTF-8” ) );
return RAW.newRAW(v_digest);
}
}
/

CREATE OR REPLACE FUNCTION sha2(p_string in VARCHAR2, p_bits in number)
RETURN RAW
AS
LANGUAGE JAVA
NAME ‘sha2.get_digest( java.lang.String, int ) return oracle.sql.RAW’;
/

Using the examples in http://en.wikipedia.org/wiki/SHA-2 we can verify the algorithms return the expected results.
SELECT sha2(‘The quick brown fox jumps over the lazy dog’,256) FROM DUAL
union all
SELECT sha2(‘The quick brown fox jumps over the lazy dog’,384) FROM DUAL
union all
SELECT sha2(‘The quick brown fox jumps over the lazy dog’,512) FROM DUAL

2 thoughts on “Using java to extend dbms_crypto

  1. 12c update – The SHA2 haching algorithm is now supported in dbms_crypto

    —- dbms_crypto.HASH_SH256 = 4;
    —- dbms_crypto.HASH_SH384 = 5;
    —- dbms_crypto.HASH_SH512 = 6;
    SQL> SELECT ‘dbms_crypto 256’ name,
    2 sys.DBMS_CRYPTO.hash(UTL_RAW.cast_to_raw(‘The quick brown fox jumps over the lazy dog’), 4)
    3 FROM DUAL
    4 UNION ALL
    5 SELECT ‘java 256’, sha2(‘The quick brown fox jumps over the lazy dog’, 256) s FROM DUAL
    6 UNION ALL
    7 SELECT ‘dbms_crypto 384’,
    8 sys.DBMS_CRYPTO.hash(UTL_RAW.cast_to_raw(‘The quick brown fox jumps over the lazy dog’), 5)
    9 FROM DUAL
    10 UNION ALL
    11 SELECT ‘java 384’, sha2(‘The quick brown fox jumps over the lazy dog’, 384) s FROM DUAL
    12 UNION ALL
    13 SELECT ‘dbms_crypto 512’,
    14 sys.DBMS_CRYPTO.hash(UTL_RAW.cast_to_raw(‘The quick brown fox jumps over the lazy dog’), 6)
    15 FROM DUAL
    16 UNION ALL
    17 SELECT ‘java 512’, sha2(‘The quick brown fox jumps over the lazy dog’, 512) s FROM DUAL;

    NAME SYS.DBMS_CRYPTO.HASH(UTL_RAW.CAST_TO_RAW(‘THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG’),4)
    ————— ———————————————————————————————————————————–
    dbms_crypto 256 D7A8FBB307D7809469CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592
    java 256 D7A8FBB307D7809469CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592
    dbms_crypto 384 CA737F1014A48F4C0B6DD43CB177B0AFD9E5169367544C494011E3317DBF9A509CB1E5DC1E85A941BBEE3D7F2AFBC9B1
    java 384 CA737F1014A48F4C0B6DD43CB177B0AFD9E5169367544C494011E3317DBF9A509CB1E5DC1E85A941BBEE3D7F2AFBC9B1
    dbms_crypto 512 07E547D9586F6A73F73FBAC0435ED76951218FB7D0C8D788A309D785436BBB642E93A252A954F23912547D1E8A3B5ED6E1BFD7097821233FA0538F3DB854FEE6
    java 512 07E547D9586F6A73F73FBAC0435ED76951218FB7D0C8D788A309D785436BBB642E93A252A954F23912547D1E8A3B5ED6E1BFD7097821233FA0538F3DB854FEE6

    6 rows selected.

Leave a Reply