SQL 如何调用Oracle MD5 哈希函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22533037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call Oracle MD5 hash function?
提问by user755806
I have below code. I am using Oracle 11g.
我有以下代码。我正在使用 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'
How can i call this code? Can i directly execute this code in sqldeveloper?
我怎样才能调用这个代码?我可以直接在 sqldeveloper 中执行这段代码吗?
回答by tbone
In Oracle 12c you can use the function STANDARD_HASH. It does not require any additional privileges.
在 Oracle 12c 中,您可以使用函数STANDARD_HASH。它不需要任何额外的特权。
select standard_hash('foo', 'MD5') from dual;
The dbms_obfuscation_toolkit is deprecated (see Note here). You can use DBMS_CRYPTO directly:
dbms_obfuscation_toolkit 已弃用(请参阅此处的注释)。您可以直接使用 DBMS_CRYPTO:
select rawtohex(
DBMS_CRYPTO.Hash (
UTL_I18N.STRING_TO_RAW ('foo', 'AL32UTF8'),
2)
) from dual;
Output:
输出:
ACBD18DB4CC2F85CEDEF654FCCC4A4D8
Add a lower function call if needed. More on DBMS_CRYPTO.
如果需要,添加一个较低的函数调用。更多关于DBMS_CRYPTO。
回答by James Ding
I would do:
我会做:
select DBMS_CRYPTO.HASH(rawtohex('foo') ,2) from dual;
output:
输出:
DBMS_CRYPTO.HASH(RAWTOHEX('FOO'),2)
--------------------------------------------------------------------------------
ACBD18DB4CC2F85CEDEF654FCCC4A4D8
回答by Greg
@user755806 I do not believe that your question was answered. I took your code but used the 'foo' example string, added a lower function and also found the length of the hash returned. In sqlplus or Oracle's sql developer Java database client you can use this to call the md5sum of a value. The column formats clean up the presentation.
@ 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;
The result set
结果集
HASH_KEY HASH_KEY_LEN
---------------------------------- ------------
acbd18db4cc2f85cedef654fccc4a4d8 32
is the same value that is returned from a Linux md5sum command.
与从 Linux md5sum 命令返回的值相同。
echo -n foo | md5sum
acbd18db4cc2f85cedef654fccc4a4d8 -
- Yes you can call or execute the sql statement directly in sqlplus or sql developer. I tested the sql statement in both clients against 11g.
- You can use any C, C#, Java or other programming language that can send a statement to the database. It is the database on the other end of the call that needs to be able to understand the sql statement. In the case of 11 g, the code will work.
- @tbone provides an excellent warning about the deprecation of the dbms_obfuscation_toolkit. However, that does not mean your code is unusable in 12c. It will work but you will want to eventually switch to dbms_crypto package. dbms_crypto is not available in my version of 11g.
- 是的,您可以直接在 sqlplus 或 sql developer 中调用或执行 sql 语句。我在两个客户端中针对 11g 测试了 sql 语句。
- 您可以使用任何可以向数据库发送语句的 C、C#、Java 或其他编程语言。需要能够理解sql语句的是调用另一端的数据库。在 11 g 的情况下,代码将起作用。
- @tbone 提供了关于 dbms_obfuscation_toolkit 弃用的极好警告。但是,这并不意味着您的代码在 12c 中无法使用。它会起作用,但您最终会希望切换到 dbms_crypto 包。dbms_crypto 在我的 11g 版本中不可用。
回答by Nashev
To calculate MD5 hash of CLOB content field with my desired encoding without implicitly recoding content to AL32UTF8, I've used this code:
为了使用我想要的编码计算 CLOB 内容字段的 MD5 哈希,而不将内容隐式重新编码为 AL32UTF8,我使用了以下代码:
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)));