问题
DECLARE
l_string NVARCHAR2(600) := '123456';
checksum NVARCHAR2(600);
BEGIN
DBMS_OUTPUT.DISABLE;
DBMS_OUTPUT.ENABLE(1000000);
DBMS_OBFUSCATION_TOOLKIT.md5 (input_string => l_string, checksum_string => checksum);
DBMS_OUTPUT.PUT_LINE(RAWTONHEX(utl_raw.cast_to_raw(checksum)));
END;
Expected value: e10adc3949ba59abbe56e057f20f883e
But it returns: FFFD00390049FFFD0059FFFDFFFD0056FFFD000FFFFD003E
Note I want to maintain nvarchar2 datatype. The value from checksum variable gets stored in column that is of type nvarchar2.
I am aware that md5 accepts and returns data in varchar2. But if someone can help me figure this out using nvarchar2 data type, that would be great.
The NLS_CHARACTERSET = AL32UTF8
回答1:
The following should work via dbms_crypto using hash()
declare
l_src nvarchar2(100) := '123456';
l_raw_hash raw(100);
begin
l_raw_hash := dbms_crypto.hash(to_clob(l_src), dbms_crypto.HASH_MD5);
dbms_output.put_line(l_raw_hash);
end;
Result: E10ADC3949BA59ABBE56E057F20F883E
l_raw_hash will be in raw format. You can use UTL_RAW to convert it to another datat type. Just make sure your display is showing the proper character set or thing will look interesting.
来源:https://stackoverflow.com/questions/10827888/convert-nvarchar2-to-md5-hash-in-oracle-dbms-obfuscation-toolkit-md5