MD5 function in Oracle
Recently I was struggling to find a function to get MD5 to work in Oracle, like the MD5() function in MySQL. After some searching I found out that there is no direct way to calculate an MD5 checksum in Oracle. Therefore I created the function below.
CREATE OR REPLACE FUNCTION USERINFO.md5hash (str IN VARCHAR2) RETURN VARCHAR2 IS v_checksum VARCHAR2(32); BEGIN v_checksum := LOWER( RAWTOHEX( UTL_RAW.CAST_TO_RAW( sys.dbms_obfuscation_toolkit.md5(input_string => str) ) ) ); RETURN v_checksum; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; WHEN OTHERS THEN -- Consider logging the error and then re-raise RAISE; END md5hash; /
What this basically does is use the sys.dbms_obfuscation_toolkit.md5 function’s raw byte array, convert it to a hexadecimal string and convert that string to all lowercase characters. This way you can easily convert strings to MD5.
For example:
SELECT md5hash('foo') FROM tbl
This returns acbd18db4cc2f85cedef654fccc4a4d8, the MD5 result we were expecting.
Feel free to use it!

11 comments to “MD5 function in Oracle”
krpalospo
14-03-2012
at 22:14
Thanks man you save my life
Imtyaz
22-05-2012
at 09:30
thank you very much
Michiel
22-05-2012
at 09:33
You’re welcome!
gautham
06-06-2012
at 03:52
thanks a lot for posting it online.
code739
28-06-2012
at 10:37
really helpful tnks for this post
Javialex
26-07-2012
at 16:25
thanks, is really helpful
Ela
07-09-2012
at 08:46
Thanks a lot ….great post
Manuel
29-12-2012
at 02:41
Great solution man!
Eliseo Hernandez
08-01-2013
at 19:33
Thanks a lot! Very useful
Oracle MD5 encryption Method | hiteshgondalia
11-02-2013
at 19:16
[...] MD5 function in Oracle [...]
hosein
03-04-2013
at 07:46
really helpful tnks for this post
Leave a reply