Operating System - HP-UX
1747986 Members
4712 Online
108756 Solutions
New Discussion юеВ

Hash created using DBMS_CRYPTO.Hash and md5sum, sha1sum not maching

 
SANTOSH S. MHASKAR
Trusted Contributor

Hash created using DBMS_CRYPTO.Hash and md5sum, sha1sum not maching

Hi,

I have PL/SQL script MD5SH1.sql to generate md5 and sha1 hash, but hash generated by this script and
hash generated by md5sum and sha1sum utility not matching.
Pl. tell me what is wrong,

------------------MD5SH1.sql----------------

set serveroutput on
DECLARE
input_string VARCHAR2 (200) := 'Lord of the ring'';
output_str_md5 VARCHAR2 (200);
output_str_sh1 VARCHAR2 (200);
hash_raw_md5 RAW(2000);
hash_raw_sh1 RAW(2000);
hash_algo_type1 PLS_INTEGER := DBMS_CRYPTO.HASH_MD5;
hash_algo_type2 PLS_INTEGER := DBMS_CRYPTO.HASH_SH1;

BEGIN
DBMS_OUTPUT.PUT_LINE ( 'Original string: ' || input_string);
hash_raw_md5 := DBMS_CRYPTO.Hash
(
src=> UTL_I18N.STRING_TO_RAW (input_string, NULL),
--src=> input_string,
typ => hash_algo_type1
);
hash_raw_sh1 := DBMS_CRYPTO.Hash
(
src=> UTL_I18N.STRING_TO_RAW (input_string, NULL),
--src=> input_string,
typ => hash_algo_type2
);
output_str_md5 := UTL_I18N.RAW_TO_CHAR (hash_raw_md5,'US7ASCII');
output_str_sh1 := UTL_I18N.RAW_TO_CHAR (hash_raw_sh1,'US7ASCII');
--output_str_md5 := hash_raw_md5;
--output_str_sh1 := hash_raw_sh1;
--dbms_output.put_line(hash_raw_md5);
--dbms_output.put_line(rawtohex(hash_raw_sh1));
DBMS_OUTPUT.PUT_LINE ('MD5 Hash: ' || output_str_md5);
DBMS_OUTPUT.PUT_LINE ('SH1 Hash: ' || output_str_sh1);
END;
/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The O/p of this script is

SYS@mydb> @MD5SH1.sql
Original string: Lord of the ring
MD5 Hash: я┐╜я┐╜я┐╜я┐╜я┐╜+.vя┐╜`я┐╜a_A
SH1 Hash:
я┐╜я┐╜ я┐╜я┐╜Eя┐╜k я┐╜[я┐╜F[c я┐╜2я┐╜

PL/SQL procedure successfully completed.

SYS@mydb>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The o/p of md5sum and sha1sum is
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

san@sde4 ~$ echo "Lord of the ring" |md5sum
f3fbb6dfd8a2d6f8f6aeabc4d6e17c57 -
san@sde4 ~$ echo "Lord of the ring" |sha1sum
856f6132e23c7e335ca4188bd45c7bc9515f6905 -
san@sde4 ~$

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Why these 2 hashes are not matching?

=============================================
-------OR It can be put like this -----------
=============================================


a] md5 hash generated by DBMS_CRYPTO.Hash function

SYS@mydb> @MD5SH1.sql
Original string: Lord of the ring
MD5 Hash: я┐╜я┐╜я┐╜я┐╜я┐╜+.vя┐╜`я┐╜a_A

is not matching with the hash generated by md5sum utility.

san@sde4 ~$ echo "Lord of the ring" |md5sum
f3fbb6dfd8a2d6f8f6aeabc4d6e17c57 -

and

b] sha1 hash generated by DBMS_CRYPTO.Hash function

SYS@mydb> @MD5SH1.sql
Original string: Lord of the ring
MD5 Hash: я┐╜я┐╜я┐╜я┐╜я┐╜+.vя┐╜`я┐╜a_A
*SH1 Hash:
я┐╜я┐╜ я┐╜я┐╜Eя┐╜k я┐╜[я┐╜F[c я┐╜2я┐╜*

is not matching with the hash generated by sha1sum utility.

san@sde4 ~$ echo "Lord of the ring" |sha1sum
856f6132e23c7e335ca4188bd45c7bc9515f6905 -

why is this mismatch if hash value is expected to be same for same algorithm.

-Santosh Mhaskar
2 REPLIES 2
Matti_Kurkela
Honored Contributor

Re: Hash created using DBMS_CRYPTO.Hash and md5sum, sha1sum not maching

The output of the hash function is a long string of bits. If you wish to display it like md1sum or sha1sum do, you must convert it to a hexadecimal form first.

If you just convert it to raw ASCII characters, you will get control characters and other random, maybe un-displayable nonsense that can confuse your display.

The commented-out line:
"--dbms_output.put_line(rawtohex(hash_raw_sh1));"
would have the right idea. I don't know enough PL/SQL to know offhand if the syntax of rawtohex() is correct or not.

MK
MK
SANTOSH S. MHASKAR
Trusted Contributor

Re: Hash created using DBMS_CRYPTO.Hash and md5sum, sha1sum not maching

Thanks Matti,

I'll convert it to hexadecimal and then see.


-Santosh