为什么 checksum() 为不同的字符串返回相同的值

编程入门 行业动态 更新时间:2024-10-25 10:31:59
本文介绍了为什么 checksum() 为不同的字符串返回相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下 SQL 及其结果显示不同的字符串得到相同的 checksum 结果.为什么?

选择 str ,binary_checksum(str) binary_checksum,校验和(str)校验和,hashbytes('md5', str) md5来自(值('2Volvo Director 20'),('3Volvo Director 30'),('4Volvo Director 40'))t ( 字符串 )

str binary_checksum 校验和 md5------------------ --------------- ----------- -------------------------------------2沃尔沃导演20 -1356512636 -383039272 0xB9BD78BCF70FAC36AF14FFF5897672783沃尔沃导向器 30 -1356512636 -383039272 0xF039462F3D15B162FFCDB6125D2908264沃尔沃导向器 40 -1356512636 -383039272 0xFAF315CDA6E453CCC09838CFB129EE74

解决方案

SQL CHECKSUM() 和 MD5 是 散列函数.散列是一种单向算法,它可以采用任意数量的字符/字节并返回固定数量的字符/字节.

这意味着无论您输入的是 1 个字符还是一本书(战争与和平),您都会得到相同长度的回复.所以输入是无限的组合,而输出是有限的.基于此,对于不同的值获得相同的哈希是不可避免的.它被称为哈希冲突.好的 Hash 算法会尝试减轻这种情况,从而很难找到这些冲突值.

但是关于散列的理论已经足够了.这正是您问题的答案.什么是 CHECKSUM() 问题?

The following SQL and its result shows the different string got the same checksum result. Why?

select str , binary_checksum(str) binary_checksum, checksum(str) checksum, hashbytes('md5', str) md5 from ( values ( '2Volvo Director 20'), ( '3Volvo Director 30'), ( '4Volvo Director 40') ) t ( str )

str binary_checksum checksum md5 ------------------ --------------- ----------- -------------------------------------------- 2Volvo Director 20 -1356512636 -383039272 0xB9BD78BCF70FAC36AF14FFF589767278 3Volvo Director 30 -1356512636 -383039272 0xF039462F3D15B162FFCDB6125D290826 4Volvo Director 40 -1356512636 -383039272 0xFAF315CDA6E453CCC09838CFB129EE74

解决方案

SQL CHECKSUM() and MD5 are Hash functions. Hashing is a one way algorithm which can take any number of chars/bytes and return a fixed number of chars/bytes.

It mean no matter if your input is 1 character or a complete book (War and Peace) you will get back the same length of response. So the input is infinite number of combinations meanwhile the output is finite. Based on that it is inevitable to get the same Hash for different values. It is called Hash collision. Good Hash algorithms try to mitigate this to make it hard to find these colliding values.

But enough theory about hashing. Here is exactly the answer to your question. What is the issue with CHECKSUM()?

更多推荐

为什么 checksum() 为不同的字符串返回相同的值

本文发布于:2023-11-06 09:53:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   checksum

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!