我有以下代码。我正在使用Oracle 11g。
SELECT DBMS_OBFUSCATION_TOOLKIT.md5 (input => UTL_RAW.cast_to_raw(
FIRST_NAME
||LAST_NAME
)) md5_key ,
FIRST_NAME ,
LAST_NAME
FROM C_NAME_TAB
WHERE PKEY='1234'
我该如何调用这段代码?我能直接在SQL开发者中执行这段代码吗?
在Oracle 12c中,您可以使用函数STANDARD_HASH。它不需要任何额外的权限。
select standard_hash('foo', 'MD5') from dual;
dbms_obfuscation_toolkit已被弃用(请参见此处的说明)。您可以直接使用DBMS_CRYPTO:
select rawtohex(
DBMS_CRYPTO.Hash (
UTL_I18N.STRING_TO_RAW ('foo', 'AL32UTF8'),
2)
) from dual;
输出:
ACBD18DB4CC2F85CEDEF654FCCC4A4D8
如有需要,请添加一个低级函数调用。更多内容请参见DBMS_CRYPTO。
我会做:
select DBMS_CRYPTO.HASH(rawtohex('foo') ,2) from dual;
输出:
DBMS_CRYPTO.HASH(RAWTOHEX('FOO'),2)
--------------------------------------------------------------------------------
ACBD18DB4CC2F85CEDEF654FCCC4A4D8
@user755806,我认为你的问题还没有得到答复。我使用了你的代码并添加了“foo”样例字符串,加入了小写函数,并找到了哈希返回值的长度。在sqlplus或Oracle的sql developer Java数据库客户端中,你可以使用此方法调用一个值的md5sum。列格式化可以使呈现更清晰。
column hash_key format a34;
column hash_key_len format 999999;
select dbms_obfuscation_toolkit.md5(
input => UTL_RAW.cast_to_raw('foo')) as hash_key,
length(dbms_obfuscation_toolkit.md5(
input => UTL_RAW.cast_to_raw('foo'))) as hash_key_len
from dual;
结果集
HASH_KEY HASH_KEY_LEN
---------------------------------- ------------
acbd18db4cc2f85cedef654fccc4a4d8 32
这个值和Linux md5sum命令返回的值是一样的。
echo -n foo | md5sum
acbd18db4cc2f85cedef654fccc4a4d8 -
为了在不将内容隐式重新编码为AL32UTF8的情况下,使用我所需的编码计算CLOB内容字段的MD5哈希值,我使用了以下代码:
create or replace function clob2blob(AClob CLOB) return BLOB is
Result BLOB;
o1 integer;
o2 integer;
c integer;
w integer;
begin
o1 := 1;
o2 := 1;
c := 0;
w := 0;
DBMS_LOB.CreateTemporary(Result, true);
DBMS_LOB.ConvertToBlob(Result, AClob, length(AClob), o1, o2, 0, c, w);
return(Result);
end clob2blob;
/
update my_table t set t.hash = (rawtohex(DBMS_CRYPTO.Hash(clob2blob(t.content),2)));