Oracle PLSQL 与 Node.js 中的 SHA512 哈希
我需要在 Ora PL-SQL 中获取与使用加密模块在 Node.js 中相同的哈希值。 在 PLSQL 中,我使用集成的 dbms_crypto.hash 函数。还有 dbms_crypto.mac 函数,但我无法使用它,因为没有额外的秘密可用于散列。
我尝试过的所有 3 种方法都从节点加密模块返回了不同的结果。
declare
v_body varchar2(1000):= 'myteststring';
v_hash varchar2(1000);
begin
--1 method
SELECT sys.dbms_crypto.hash( utl_raw.cast_to_raw(v_body), DBMS_CRYPTO.HASH_SH512 )
INTO v_hash
FROM dual;
dbms_output.put_line(lower((v_hash)));
--result:516e7933c5ecd0721cd91cdbe341834ffe775bb32a223faa5c564d3ddf0b3069bdebfde2f07b642bad5f1351ec913247c737855ba8adf03cb56adc76ca3c8722
--2 method
SELECT standard_hash (utl_raw.cast_to_raw(v_body), 'SHA512')
INTO v_hash
FROM dual;
dbms_output.put_line(lower(v_hash));
--result:516e7933c5ecd0721cd91cdbe341834ffe775bb32a223faa5c564d3ddf0b3069bdebfde2f07b642bad5f1351ec913247c737855ba8adf03cb56adc76ca3c8722
--3 method
SELECT standard_hash (rawtohex(v_body), 'SHA512')
INTO v_hash
FROM dual;
dbms_output.put_line(lower((v_hash)));
--result:7dcaa1fd7e41a572158ec8a3742599f9e34ddd33d649e1952223780863ee66a6722ad93091cc6e42f53e16f896e130ac4e28794affde34819e32e77873c2ed46
end;
在 Node.js 中,代码很简单,看起来像这样(这是另一面,无法更改):
const crypto = require('crypto');
var mystring = 'myteststring';
var hash = crypto.createHmac('sha512',mystring).digest('hex');
console.log(hash);
//22e6584b717cff0f6180bf988c5ebf0bbb0bc1959bbb911a203f9188971fc2de93b6c83465501747d1cde6f6efaf0b37d4ac278bcd3bb6fe662d3e9f7e0ae50c
我不确定我做错了什么,或者这个 Ora 函数的工作方式不同。我还尝试过更改参数类型(clob/varchar)。 Ora 结果是十六进制的,就像它在 Node 中一样。 提前致谢!
I need to get a hash in Ora PL-SQL thats the same as it is in Node.js using crypto module.
In PLSQL im using integrated dbms_crypto.hash function. There is also dbms_crypto.mac function but i cant use it since there is no additional secret to use for hashing.
All 3 methods i've tried returned different results from Node crypto module.
declare
v_body varchar2(1000):= 'myteststring';
v_hash varchar2(1000);
begin
--1 method
SELECT sys.dbms_crypto.hash( utl_raw.cast_to_raw(v_body), DBMS_CRYPTO.HASH_SH512 )
INTO v_hash
FROM dual;
dbms_output.put_line(lower((v_hash)));
--result:516e7933c5ecd0721cd91cdbe341834ffe775bb32a223faa5c564d3ddf0b3069bdebfde2f07b642bad5f1351ec913247c737855ba8adf03cb56adc76ca3c8722
--2 method
SELECT standard_hash (utl_raw.cast_to_raw(v_body), 'SHA512')
INTO v_hash
FROM dual;
dbms_output.put_line(lower(v_hash));
--result:516e7933c5ecd0721cd91cdbe341834ffe775bb32a223faa5c564d3ddf0b3069bdebfde2f07b642bad5f1351ec913247c737855ba8adf03cb56adc76ca3c8722
--3 method
SELECT standard_hash (rawtohex(v_body), 'SHA512')
INTO v_hash
FROM dual;
dbms_output.put_line(lower((v_hash)));
--result:7dcaa1fd7e41a572158ec8a3742599f9e34ddd33d649e1952223780863ee66a6722ad93091cc6e42f53e16f896e130ac4e28794affde34819e32e77873c2ed46
end;
In Node.js code is simple and looks like this (this is other side and cant change this):
const crypto = require('crypto');
var mystring = 'myteststring';
var hash = crypto.createHmac('sha512',mystring).digest('hex');
console.log(hash);
//22e6584b717cff0f6180bf988c5ebf0bbb0bc1959bbb911a203f9188971fc2de93b6c83465501747d1cde6f6efaf0b37d4ac278bcd3bb6fe662d3e9f7e0ae50c
Im not sure what im doing wrong, or this Ora function works different. I've also tried changing type of parameters (clob/varchar). Ora result is in hex as its in Node.
Thanks in any advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您调用dbms_crypto.mac的clob(或blob)过载,则似乎有效
Seems to work if you call the CLOB (or BLOB) overload of dbms_crypto.mac and pass an empty clob (or blob), e.g.
在node.js中,您使用
createHmac
而不是createHash
。如果使用:
那么输出为:
与前两个Oracle查询的输出相匹配。
如果要生成HMAC,则在Node.js中您想要:
在Oracle中,您需要(未经测试):
对于您的代码,您需要
null
message(再次,未测试):In Node.js, you are using
createHmac
rather thancreateHash
.If you use:
Then the output is:
Which matches the output of your first two Oracle queries.
If you want to generate a HMAC then in Node.js you want:
and in Oracle, you want (untested):
For your code you would want a
NULL
message (again, untested):