我必须具有生成随机元素的哈希(sha512)的功能。第一个是oracle sql查询:
select RAND, DBMS_CRYPTO.HASH(RAND, 6 /*SHA512*/) as sha512 from
(select DBMS_CRYPTO.RANDOMBYTES(5) as RAND from DUAL);
返回
RAND
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SHA512
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C1BEC41854
E4BD639D4726D294CB63B6DDC651C6B6F5708ED3FC9B2E08A71DD7D36958B7B13BD31ECA28039565121F3067167D719292A86B6CAD052EFC9A56923594946084
当我尝试在python中为C1BEC41854
生成哈希时,我使用以下脚本
from hashlib import sha512
h = 'C1BEC41854'
b = bytes.fromhex(h)
print(sha512(b).hexdigest())
返回
a63f4d25b5f0fc51fb27ae1e1c5f4ff19edc7b790d2373071ae8f454e63766a19b69a200690a32a65dd57be5b47fec29ee15c354f52ad5916127bb4cf674ab37
您能帮我弄清楚为什么两个哈希值都不相同吗?
答案 0 :(得分:3)
在Oracle查询中,您正在生成多个随机字节字符串。
尝试以下方法进行演示:
select RAND, RAND, RAND, DBMS_CRYPTO.HASH(RAND, 6 /*SHA512*/) as sha512 from
(select DBMS_CRYPTO.RANDOMBYTES(5) as RAND from DUAL);
请注意RAND的三个不同值。因此,您生成的哈希实际上是与您认为的不同的字节序列。
要解决此问题,您可以使用AskTom提供的一些骗术
select RAND,DBMS_CRYPTO.HASH(RAND, 6 /*SHA512*/) as sha512 from
(select rownum, DBMS_CRYPTO.RANDOMBYTES(5) as RAND from DUAL);
在子查询中包含rownum会使每次将RAND用作顶级SELECT字段时使RAND保持一致。
答案 1 :(得分:1)
这将为您提供相同的结果(大写):
SELECT sys.dbms_crypto.hash(
hextoraw('C1BEC41854'),
6--HASH_SH512
) from dual;
看起来您的字符串已经是一个十六进制值,所以我只做了一个hextoraw。如果它是像“ Hello”这样的基本字符串,那么我将使用utl_raw.cast_to_raw
。
输出:
A63F4D25B5F0FC51FB27AE1E1C5F4FF19EDC7B790D2373071AE8F454E63766A19B69A200690A32A65DD57BE5B47FEC29EE15C354F52AD5916127BB4CF674AB37