PHP 上的 md5()、crc32() 和 sha1() 加密之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15676575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What’s the difference between md5(), crc32() and sha1() crypto on PHP?
提问by Mike D.
The difference is in the length they generate.
不同之处在于它们生成的长度。
crc32() gives 32 bit code
crc32() 给出 32 位代码
sha1() gives 128 bit code
sha1() 给出 128 位代码
md5() gives 160 bit code
md5() 给出 160 位代码
is it right?? or Is there any more differences among them?
这样对吗??或者它们之间还有什么区别吗?
回答by Mike D.
They each implement a different cryptographic hash function, and each hash function does generate a different sized hash. The main difference between the three functions you've shown here is that sha1and md5are actually meant to be cryptographically secure. crc32 (crc stands for cyclic redundancy check) function is not a crypto function and is meant to generate a hash that will be used to check the integrity of a file (mostly to determine if it was corrupted during download).
它们各自实现不同的加密散列函数,并且每个散列函数确实生成不同大小的散列。您在此处显示的三个函数之间的主要区别在于sha1和md5实际上是加密安全的。crc32(crc 代表循环冗余校验)函数不是加密函数,它旨在生成一个哈希值,用于检查文件的完整性(主要用于确定文件是否在下载过程中损坏)。
Just a side note: Please don't use md5or sha1for any real crypto work (such as hashing passwords). These are both terribly broken (just ask evernote or any of the other companies burned by using this old algorithm). Instead use the php crypt() functionand use the SHA-256 or SHA-512 (better than 256), or blowfish. And always salt your hashes...
旁注:请不要将md5或sha1用于任何真正的加密工作(例如散列密码)。这些都非常糟糕(只要问问印象笔记或使用这种旧算法烧毁的任何其他公司)。而是使用php crypt() 函数并使用 SHA-256 或 SHA-512(优于 256)或河豚。并且总是给你的哈希加盐......
回答by Dipesh Parmar
I think not only the length
differs, also they use different algorithm to encrypt
the data.
我认为不仅length
不同,而且他们encrypt
对数据使用不同的算法。
Crypto usage in PHP is simple, but that doesn't mean it's free. First off, depending on the data that you're encrypting, you might have reasons to store a 32-bit value in the database instead of the 160-bit value to save on space. Second, the more secure the crypto is, the longer is the computation time to deliver the hash value. A high volume site might be significantly slowed down, if frequent md5() generation is required.
PHP 中的加密使用很简单,但这并不意味着它是免费的。首先,根据您要加密的数据,您可能有理由在数据库中存储 32 位值而不是 160 位值以节省空间。其次,加密越安全,传递哈希值的计算时间就越长。如果需要频繁的 md5() 生成,高容量站点可能会显着减慢速度。
回答by AD7six
crc32 is not a hashing algorithm
crc32 不是散列算法
crc32generates "the crc32 checksum of str as an integer." - it's intended use is to quickly verify the integrity of something and widely used to detect accidental changes such as network transmission errors.
crc32生成“str 作为整数的 crc32 校验和”。- 它的预期用途是快速验证某物的完整性并广泛用于检测意外更改,例如网络传输错误。
md5 and sha1 are hash algorithms
md5 和 sha1 是哈希算法
You are better off reading php's docsthat cover examples:
您最好阅读涵盖示例的php 文档:
Why are common hashing functions such as md5() and sha1() unsuitable for passwords?Hashing algorithms such as MD5, SHA1 and SHA256 are designed to be very fast and efficient. With modern techniques and computer equipment, it has become trivial to "brute force" the output of these algorithms, in order to determine the original input.
为什么 md5() 和 sha1() 等常用散列函数不适用于密码?MD5、SHA1 和 SHA256 等散列算法旨在非常快速和高效。借助现代技术和计算机设备,“蛮力”这些算法的输出以确定原始输入已变得微不足道。
An md5
is 128-bit, a sha1
is 160-bit. In the question this is reversed.
Anmd5
是 128 位, asha1
是 160 位。在问题中,这是相反的。
A major difference between md5
and sha1
is that an example of a sha1
collisionhas yet to be found. I.e. if you use md5
for two different inputs, it's possible to get the same hash; with sha1
it's not. Other than that they implement different algorithms but, e.g., are both unsuitable for storing passwordsdespite being commonly used to do so.
之间的主要区别md5
,并sha1
是一个示例sha1
碰撞尚未被发现。即,如果您md5
用于两个不同的输入,则有可能获得相同的哈希值;与sha1
它不是。除此之外,它们实现了不同的算法,但是,例如,尽管通常用于存储密码,但它们都不适合存储密码。
回答by Patashu
crc32() is NOT intended to be used for cryptographic purposes. Its purpose is that it produces a hash very very quickly, so you want to use it when that is your primary concern - you want a usually useful hash right away. It's used in networking equipment for this reason.
crc32() 不打算用于加密目的。它的目的是非常快速地产生一个散列,所以你想在你最关心的时候使用它——你想要一个通常有用的散列。出于这个原因,它被用于网络设备。
md5() and sha1() are both cryptographic hashing, meaning they intend to have very nice properties, such as it being very very hard to find a message that produces a certain hash given only what the hash value is. However, sha1 is more secure and more modern than md5. In fact, the use of md5 should be considered deprecated if you care about security.
md5() 和 sha1() 都是加密散列,这意味着它们打算具有非常好的属性,例如很难找到仅给定散列值就产生特定散列的消息。但是,sha1 比 md5 更安全、更现代。事实上,如果您关心安全性,应该考虑弃用 md5。
Also remember to salt hashes of passwords by concatenating something extra with them, or something called a 'rainbow table' which is a big list of pre-hashed common passwords can be used to figure out what the hashes correspond to and hack user accounts.
还要记住通过连接一些额外的东西来对密码的散列进行加盐,或者称为“彩虹表”的东西,这是一个预先散列的常用密码的大列表,可用于找出散列对应的内容并破解用户帐户。
回答by Nicholas Pickering
Those are called hash functions.
这些被称为哈希函数。
http://en.wikipedia.org/wiki/Hash_function- explains the different types of hash functions
http://en.wikipedia.org/wiki/Hash_function- 解释不同类型的哈希函数
They work completely differently internally.
它们在内部的工作方式完全不同。
Note: Don't use crc32
for encryption purposes. Its use is for quick hashing, it is not nearly as secure as sha1
or md5
.
注意:请勿crc32
用于加密目的。它用于快速散列,它不像sha1
或那样安全md5
。
回答by Nirav Ranpara
The major difference is the length of the hash generated.
主要区别在于生成的散列的长度。
CRC32 is, evidently, 32 bits,
sha1() returns a 128 bit value,
and md5() returns a 160 bit value.
It is important when avoiding collisions.
这在避免碰撞时很重要。