首批通过分布式安全可靠测评,为关键业务系统打造
OceanBase 数据库 Oracle 模式如何实现 md5 函数
更新时间:2026-05-19 08:21
本文介绍 OceanBase 数据库 Oracle 模式如何实现 md5 函数。
适用版本
OceanBase 数据库 V3.2.x、V4.x 版本
详细内容
OceanBase 数据库 V3.x 和 V4.x 兼容 dbms_crypto 但暂未兼容较早版本的 dbms_obfuscation_toolkit。
dbms_obfuscation_toolkit 是在 Oracle 8i 中引入的。
dbms_crypto 是 Oracle 10g 中引入的,旨在替换 dbms_obfuscation_toolkit。
与 dbms_crypto 相比,dbms_obfuscation_toolkit 功能有限,Oracle 官方建议使用 dbms_crypto。
OceanBase 数据库 Oracle 中实现 md5 方式如下。
dbms_crypto
obclient> SELECT rawtohex(dbms_crypto.hash(utl_raw.cast_to_raw('hello world'),2 /*dbms_crypto.HASH_MD5*/)) md5 FROM dual;
+----------------------------------+
| MD5 |
+----------------------------------+
| 5EB63BBBE01EEED093CB22BB8F5ACDC3 |
+----------------------------------+
1 row in set (0.16 sec)
对比 Oracle 11g 中 md5 结果,两者一致。
dbms_crypto
SQL> SELECT rawtohex(dbms_crypto.hash(utl_raw.cast_to_raw('hello world'),2 /*dbms_crypto.HASH_MD5*/)) md5 FROM dual;
MD5
--------------------------------------------------------------------------------
5EB63BBBE01EEED093CB22BB8F5ACDC3
dbms_obfuscation_toolkit (Oracle 官方已不推荐使用)
SQL> SELECT utl_raw.cast_to_raw(dbms_obfuscation_toolkit.md5(input_string => 'hello world')) md5 FROM dual;
MD5
--------------------------------------------------------------------------------
5EB63BBBE01EEED093CB22BB8F5ACDC3
对比 MySQL 中 md5 结果,两者一致。
Server version: 5.7.32-log MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select md5('hello world');
+----------------------------------+
| md5('hello world') |
+----------------------------------+
| 5eb63bbbe01eeed093cb22bb8f5acdc3 |
+----------------------------------+
1 row in set (0.03 sec)
对比 OceanBase MySQL 中 md5 结果,两者一致。
Server version: 5.7.25 OceanBase 3.2.4.7 (r107000012023113010-30dacb758186c789832a51a3556e3408074b39d3) (Built Nov 30 2023 10:31:16)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [oceanbase]> select md5('hello world');
+----------------------------------+
| md5('hello world') |
+----------------------------------+
| 5eb63bbbe01eeed093cb22bb8f5acdc3 |
+----------------------------------+
1 row in set (0.00 sec)
快速让 OceanBase 数据库支持 dbms_obfuscation_toolkit.md5
## 创建包为 DBMS_OBFUSCATION_TOOLKIT
CREATE OR REPLACE PACKAGE DBMS_OBFUSCATION_TOOLKIT
IS
FUNCTION MD5(input_string VARCHAR2)
RETURN varchar2;
END DBMS_OBFUSCATION_TOOLKIT;
/
## 创建包体为 DBMS_OBFUSCATION_TOOLKIT
CREATE OR REPLACE PACKAGE BODY DBMS_OBFUSCATION_TOOLKIT
IS
FUNCTION MD5(input_string VARCHAR2)
RETURN varchar2
IS
BEGIN
RETURN UTL_RAW.CAST_TO_VARCHAR2(dbms_crypto.hash(utl_raw.cast_to_raw(input_string), 2));
END MD5;
END DBMS_OBFUSCATION_TOOLKIT;
/