让 Oracle 的 MD5 与 PHP 的 MD5 匹配

发布于 2024-07-29 22:09:38 字数 1096 浏览 4 评论 0原文

我正在尝试将 PHP 生成的 MD5 校验和与 Oracle 10g 生成的 MD5 校验和进行比较。 然而,我似乎正在将苹果与橙子进行比较。

以下是我测试比较的方法:

//md5 tests

  //php md5
  print md5('testingthemd5function');

  print '<br/><br/>';

  //oracle md5
  $md5query = "select md5hash('testingthemd5function') from dual";

  $stid = oci_parse($conn, $md5query);
  if (!$stid) {
   $e = oci_error($conn);
   print htmlentities($e['message']);
   exit;
  }

  $r = oci_execute($stid, OCI_DEFAULT);
  if (!$r) {
   $e = oci_error($stid);
   echo htmlentities($e['message']);
   exit;
  }

  $row = oci_fetch_row($stid); 
  print $row[0];

Oracle 中的 md5 函数(在上面的查询中看到)使用“dbms_obfuscation_toolkit.md5”包(?),定义如下:

CREATE OR REPLACE FUNCTION PORTAL.md5hash (v_input_string in varchar2) return varchar2     
is
   v_checksum varchar2(20);
   begin
   v_checksum := dbms_obfuscation_toolkit.md5 (input_string => v_input_string);
   return v_checksum;
end;

我的 PHP 页面上显示的是:

29dbb90ea99a397b946518c84f45e016

)Û¹©š9{”eÈOEà 

任何人都可以帮忙吗?我让两者匹配吗?

I'm trying to compare an MD5 checksum generated by PHP to one generated by Oracle 10g. However it seems I'm comparing apples to oranges.

Here's what I did to test the comparison:

//md5 tests

  //php md5
  print md5('testingthemd5function');

  print '<br/><br/>';

  //oracle md5
  $md5query = "select md5hash('testingthemd5function') from dual";

  $stid = oci_parse($conn, $md5query);
  if (!$stid) {
   $e = oci_error($conn);
   print htmlentities($e['message']);
   exit;
  }

  $r = oci_execute($stid, OCI_DEFAULT);
  if (!$r) {
   $e = oci_error($stid);
   echo htmlentities($e['message']);
   exit;
  }

  $row = oci_fetch_row($stid); 
  print $row[0];

The md5 function (seen in the query above) in Oracle uses the 'dbms_obfuscation_toolkit.md5' package(?) and is defined like this:

CREATE OR REPLACE FUNCTION PORTAL.md5hash (v_input_string in varchar2) return varchar2     
is
   v_checksum varchar2(20);
   begin
   v_checksum := dbms_obfuscation_toolkit.md5 (input_string => v_input_string);
   return v_checksum;
end;

What comes out on my PHP page is:

29dbb90ea99a397b946518c84f45e016

)Û¹©š9{”eÈOEà 

Can anyone help me in getting the two to match?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

下壹個目標 2024-08-05 22:09:42

我得到了相同的“数字或值错误”,并发现两个函数一起工作:

CREATE OR REPLACE FUNCTION MD5RAW( v_input_string in varchar2 )
RETURN varchar2 IS
v_checksum varchar2( 32 );
BEGIN
    v_checksum := SYS.DBMS_OBFUSCATION_TOOLKIT.MD5( input_string => v_input_string );
    return v_checksum;
END;

CREATE OR REPLACE FUNCTION MD5HEX( v_input_string in varchar2 )
RETURN varchar2 IS
v_hex_value varchar2( 32 );
BEGIN
    SELECT  LOWER( RAWTOHEX( MD5RAW( v_input_string ) ) ) 
    INTO    v_hex_value
    FROM    dual;
    return v_hex_value;
END;

然后您可以运行此查询来获取校验和:

SELECT md5hex( 'my string smoked your hash' ) FROM dual;

第二个函数的作用与在您提供的函数上发出 Bazz 提供的 SELECT 语句相同,但我宁愿不必执行 rawToHex --> 每个查询内部的转化率较低。 每次使用查询时,都会留下太多可能出错的事情。 我认为它也可能更快,因为它是在创建时而不是运行时编译的,但我对此可能是错的。

I got the same "numeric or value error" and found that two functions together work:

CREATE OR REPLACE FUNCTION MD5RAW( v_input_string in varchar2 )
RETURN varchar2 IS
v_checksum varchar2( 32 );
BEGIN
    v_checksum := SYS.DBMS_OBFUSCATION_TOOLKIT.MD5( input_string => v_input_string );
    return v_checksum;
END;

CREATE OR REPLACE FUNCTION MD5HEX( v_input_string in varchar2 )
RETURN varchar2 IS
v_hex_value varchar2( 32 );
BEGIN
    SELECT  LOWER( RAWTOHEX( MD5RAW( v_input_string ) ) ) 
    INTO    v_hex_value
    FROM    dual;
    return v_hex_value;
END;

Then you can run this query to get your checksum:

SELECT md5hex( 'my string smoked your hash' ) FROM dual;

That second function does the same thing as issuing the SELECT statement provided by Bazz on the function you provide, but I prefer to not have to do the rawToHex --> lower conversion inside of every query. That leaves too many things to potentially go wrong every time you use the query. I think it may be faster too since it is compiled at creation time instead of at runtime, but I may be wrong about that.

呆° 2024-08-05 22:09:41

如果您想在 Oracle 中拥有 md5,可以使用以下方法:

select lower(rawtohex(md5hash('foobar'))) from dual

In case you would want to have the md5 in Oracle, you can use this method:

select lower(rawtohex(md5hash('foobar'))) from dual
简美 2024-08-05 22:09:41

创建一个如下所示的函数:

create or replace
function md5( input varchar2 ) return sys.dbms_obfuscation_toolkit.varchar2_checksum as
begin
    return lower(rawtohex(utl_raw.cast_to_raw(sys.dbms_obfuscation_toolkit.md5( input_string => input ))));
end;

并像这样调用它:

select md5('foobar') from dual;

似乎“dbms_obfuscation_toolkit.md5”并没有真正以原始格式返回,因此需要调用“utl_raw.cast_to_raw”。 虽然我可能是错的,但应该有更好的解释。

Create a function like the following:

create or replace
function md5( input varchar2 ) return sys.dbms_obfuscation_toolkit.varchar2_checksum as
begin
    return lower(rawtohex(utl_raw.cast_to_raw(sys.dbms_obfuscation_toolkit.md5( input_string => input ))));
end;

and call it like this:

select md5('foobar') from dual;

it seems that "dbms_obfuscation_toolkit.md5" does not really return in raw format, hence the need to call "utl_raw.cast_to_raw". I could be wrong though, there should be a better explanation for this.

喜爱纠缠 2024-08-05 22:09:40

看来从 Oracle 查询中打印的是 md5 校验和的原始字节流,由于大多数八位字节不是 ascii 字符,所以被破坏了。 首先尝试将其转换为十六进制。

It appears that what's being printed from the Oracle query is the raw bytestream of the md5 checksum, mangled because most of those octets won't be ascii characters. Try converting it to hexadecimal first.

吻泪 2024-08-05 22:09:40

它返回原始字节,您需要将其转换为十六进制。

$x = unpack("H*", $row[0]); 
echo $x[1];

It returns raw bytes, you need to convert that into hex.

$x = unpack("H*", $row[0]); 
echo $x[1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文