Oracle 中的 MD5 (DBMS_OBFUSCATION_TOOLKIT.MD5)
我正在尝试编写一个函数,从我到处收集的位中获取 MD5 哈希值。我想获得哈希的小写十六进制表示。到目前为止我有这个:
CREATE OR REPLACE FUNCTION MD5 (
CADENA IN VARCHAR2
) RETURN DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM
AS
BEGIN
RETURN LOWER(
RAWTOHEX(
UTL_RAW.CAST_TO_RAW(
DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => CADENA)
)
)
);
END;
我不确定函数的返回类型。 DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM
看起来是合适的选择,据我所知,它按预期工作,但 SQL Developer 显示的 dbms_obfuscation_toolkit
的包定义显示
SUBTYPE varchar2_checksum IS VARCHAR2(16);
: 32个字符所以我一定做错了什么。我的问题:
RETURN
语句的正确类型是什么?- 我是否进行了不必要的转换来计算哈希值?
I'm trying to compose a function to obtain MD5 hashes from bits I've gathered here and there. I want to obtain the lower-case hexadecimal representation of the hash. I have this so far:
CREATE OR REPLACE FUNCTION MD5 (
CADENA IN VARCHAR2
) RETURN DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM
AS
BEGIN
RETURN LOWER(
RAWTOHEX(
UTL_RAW.CAST_TO_RAW(
DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => CADENA)
)
)
);
END;
I'm not sure about the return type of the function. DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM
looks like the appropriate choice and as far as I can tell it works as expected but the package definition for dbms_obfuscation_toolkit
as displayed by SQL Developer shows this:
SUBTYPE varchar2_checksum IS VARCHAR2(16);
The output has 32 characters so I must be doing something wrong. My questions:
- What's the correct type for the
RETURN
statement? - Am I doing unnecessary conversions to calculate the hash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给你:
32 长度是因为它是 raw(16) 值的十六进制表示。或者,修改上面的内容以输出原始版本并将原始数据存储在 RAW 列中(使用的空间较少,但相信我,您将在未来进行 rawtohex 和 hextoraw 转换)。
干杯
Here you go:
The 32 length is because it is a hex representation of the raw(16) value. Or, modify above to output the raw version and store the raw in a RAW column (less space used, but you'll be doing future rawtohex and hextoraw conversions, believe me).
Cheers
Oracle PL/SQL 的一个特点是存储过程参数和函数返回类型不能受到限制。也就是说,我们不能拥有一个具有如下签名的过程:
当然我们可以使用 SUBTYPE 定义过程的参数,但 Oracle 会忽略它。函数返回类型也是如此...
限制参数和返回类型的唯一方法是在存储过程中使用子类型声明变量。使用包内的变量,并将它们分配给 OUT 参数(或返回函数的变量)。
这是一种冗长的说法,您可以在代码中使用
DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM
,确信它不会阻止您的函数返回 32 个字符。但是,这会让查找 SUBTYPE 声明的开发人员感到困惑。在最坏的情况下,这些人将使用子类型来声明自己的工作变量,从而产生以下悲惨结果:
因此,最好不要使用不合适的子类型。相反,声明你自己的。
It's a peculiarity of Oracle PL/SQL that stored procedure parameters and function return types cannot be limited. That is, we cannot have a procedure with a signature like this:
Sure we can define the procedure's parameter using a SUBTYPE but Oracle will ignore it. Same goes for function return types...
The only way of limiting parameters and return types is to declare variables using subtypes within the stored procedure. Use the variables within the package, and assign them to the OUT paramters (or RETURN the variable for functions).
Which is a long-winded way of saying, you can use
DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM
in your code confident that it won't prevent your function returning 32 characters.However, it will confuse developers who will lookup the SUBTYPE declaration. In the worst case these people will use the subtype to declare their own working variables with the following tragic result:
So, it is better not to use an inappropriate subtype. Instead declare your own.