MySQL 函数 md5 的输入是如何处理的?

发布于 2024-08-08 19:09:47 字数 1068 浏览 3 评论 0原文

我在理解如何处理 MySQL 4.1.22 中 md5 函数的输入时遇到问题。基本上我无法重新创建特定值组合的 md5sum 进行比较。我猜这与输入数据的格式有关。

我设置了一个表,其中有两列 double 类型(方向和高程)+ 第三列用于存储 md5 和。

使用设置脚本,我将数据添加到方向和高程字段+使用以下语法创建校验和:

insert into polygons (
  direction, 
  elevation, 
  md5sum
) 
values ( 
  (select radians(20.0)), 
  (select radians(30.0)), 
  ( md5( (select radians(20.0)) + (select radians(20.0)) ) )
)

最终为: 0.349065850398866, 0.523598775598299, '0c2cd2c2a9fe40305c4e3bd991812df5'

后来我将存储的 md5sum 与新的进行比较计算后,新计算的值是使用 md5('0.349065850398866' + '0.523598775598299') 创建的,我得到以下校验和: '8c958bcf912664d6d27c50f1034bdf34'

如果我将传递的“字符串”中的最后一个小数从 9 修改为 8 0.523598775598298 我会得到与之前存储的相同的校验和,'0c2cd2c2a9fe40305c4e3bd991812 df5 '。从 8 到 0 最后一位小数的任何值都会给出相同的校验和。

在设置脚本中使用 BINARY, (md5( (select BINARY(radians(20.0))) + (select BINARY(radians(20.0))) ) 创建与我原来的“运行时计算”相同的校验和“

值得一提的是,原始方法适用于我拥有的所有其他行(55)

我想我以一种有点奇怪的方式使用该函数,但我不确定更好的方法,所以为了找到更好的方法我觉得我需要理解为什么电流会失败。

I'm having problems understanding how input to the md5 function in MySQL 4.1.22 is handled. Basically I'm not able to recreate the md5sum of a specific value combination for comparison. I guess it has something to do with the format of the input data.

I have set up a table with two columns of type double(direction and elevation) + a third for storing a md5 sum.

With a setup script i add data to the direction and elevation fields + create a checksum using the following syntax:

insert into polygons (
  direction, 
  elevation, 
  md5sum
) 
values ( 
  (select radians(20.0)), 
  (select radians(30.0)), 
  ( md5( (select radians(20.0)) + (select radians(20.0)) ) )
)

which ends up as: 0.349065850398866, 0.523598775598299, '0c2cd2c2a9fe40305c4e3bd991812df5'

Later I compare the stored md5sum with a newly calculated, the newly calculated is created using md5('0.349065850398866' + '0.523598775598299') and I get the following checksum:
'8c958bcf912664d6d27c50f1034bdf34'

If I modify the last decimal in the passed "string" from a 9 to a 8 0.523598775598298 i get the same checksum as previously stored, '0c2cd2c2a9fe40305c4e3bd991812df5'. Any value of the last decimal from 8 down to 0 gives the same checksum.

Using BINARY, (md5( (select BINARY(radians(20.0))) + (select BINARY(radians(20.0))) ) in the setup script creates the same checksum as the my original "runtime calculation"

Worth mentioning is that the original method works for all other rows I have (55)

I guess I'm using the function in a somewhat strange way, but I'm not sure of a better way, so in order to find a better way I feel I need to understand why the current is failing.

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

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

发布评论

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

评论(2

放肆 2024-08-15 19:09:48

弧度(20.0)的输出是用比打印输出中显示的更多的数字来计算的。当结果传递给 md5 函数时,将使用完整的未截断值,而打印值仅显示有限的位数。因此,在这两种情况下传递给 md5 函数的值不同。

The output of radians(20.0) is computed with many more digits than are shown in the printed output. When the result is passed to the md5 function, the full non-truncated value is used, whereas the printed value only will show a limited number of digits. Thus, it is not the same value being passed into the md5 function in the two cases.

梦里泪两行 2024-08-15 19:09:47

您相加的两个数字以二进制形式存储,但以十进制形式显示。如果您将十进制形式返回给机器,则不能保证您会得到完全相同的数字。

在这种情况下,这会导致加法给出稍微不同的结果,从而给出完全不同的 MD5 和:

mysql> select radians(20.0) + radians(30.0), '0.349065850398866' + '0.523598775598299';
+-------------------------------+-------------------------------------------+
| radians(20.0) + radians(30.0) | '0.349065850398866' + '0.523598775598299' |
+-------------------------------+-------------------------------------------+
|              0.87266462599716 |                          0.87266462599717 | 
+-------------------------------+-------------------------------------------+
1 row in set (0.00 sec)

如果您想始终获得相同的结果,则需要存储 radians(20.0)radians(30.0) 在变量中的某处,从不依赖于它们的打印表示。

The two numbers you are adding are stored in binary form, but displayed in decimal form. There is no guarantee that you will get exactly the same number back if you give the decimal form back to the machine.

In this case, this causes the addition to give a slightly different result, which gives an entirely different MD5 sum:

mysql> select radians(20.0) + radians(30.0), '0.349065850398866' + '0.523598775598299';
+-------------------------------+-------------------------------------------+
| radians(20.0) + radians(30.0) | '0.349065850398866' + '0.523598775598299' |
+-------------------------------+-------------------------------------------+
|              0.87266462599716 |                          0.87266462599717 | 
+-------------------------------+-------------------------------------------+
1 row in set (0.00 sec)

If you want to consistently get the same result, you need to store the results of radians(20.0) and radians(30.0) in variables somewhere, never relying on their printed representations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文