PHP - 计算两个字符串之间相似度的最佳哈希算法是什么?
PHP - What is the best hash algorithm to calculate the similarity between two string?
我想用hash()来calculate/find两个字符串之间的相似度。
在PHP中有许多受支持的散列算法。列表可以通过hash_algos().
获得
推荐的最佳算法是什么?
你的问题太含糊了。
首先,你说你想计算两个字符串之间的相似度。这根本不需要散列。您可以为此使用 compare
、equality
、levenshtein distance
、edit distance
等。
Why do we use hashing
如果有一些我们不能以明文形式存储的敏感数据和我们不需要在任何处理、计算或修改数据中使用这些数据,而只需要将其与完全相等进行比较,我们使用哈希。
eg. storing user passwords, which would need only comparison with the password string once he tries logging in
Parameters
速度、安全(也许还有受欢迎程度)
一些最流行的哈希值包括 md5, SHA-1, SHA-256 and SHA-512
。这是它们安全且相对较慢的顺序。
fast, less secure | md5 < SHA-1 < SHA-256 < SHA-512 | relatively slow, more secure
我建议使用 SHA-1 或 SHA-256,它们足够快也足够安全。
此外,使用秘密 salt 可以将安全性提高很多倍(使用 salt 时散列可以显着提高安全性,原因很明显)。
哈希与相似度无关。此外,大多数哈希算法对于极其相似的字符串产生的结果几乎完全不同。例如 SHA1 的:
The quick brown fox jumps over the lazy dog
= 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
和一个符号的区别是完全不同的字符串:
The quick brown fox jumps over the lazy cog
= de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3
有一些简单且广为人知的算法可用于查找字符串相似性。 Hamming distance for strings of the same length, Levenshtein distance for different length string are just a few examples of string metrics
P.S。如果你真的真的想使用散列 - 这种类型的散列的正确术语是 locality preserving hash. You can read about it here.
我建议查看 Nilsimsa 算法以获得介绍。
"The goal of Nilsimsa is to generate a hash digest of an email message
such that the digests of two similar messages are similar to each
other"
我想用hash()来calculate/find两个字符串之间的相似度。
在PHP中有许多受支持的散列算法。列表可以通过hash_algos().
获得推荐的最佳算法是什么?
你的问题太含糊了。
首先,你说你想计算两个字符串之间的相似度。这根本不需要散列。您可以为此使用 compare
、equality
、levenshtein distance
、edit distance
等。
Why do we use hashing
如果有一些我们不能以明文形式存储的敏感数据和我们不需要在任何处理、计算或修改数据中使用这些数据,而只需要将其与完全相等进行比较,我们使用哈希。
eg. storing user passwords, which would need only comparison with the password string once he tries logging in
Parameters
速度、安全(也许还有受欢迎程度)
一些最流行的哈希值包括 md5, SHA-1, SHA-256 and SHA-512
。这是它们安全且相对较慢的顺序。
fast, less secure | md5 < SHA-1 < SHA-256 < SHA-512 | relatively slow, more secure
我建议使用 SHA-1 或 SHA-256,它们足够快也足够安全。
此外,使用秘密 salt 可以将安全性提高很多倍(使用 salt 时散列可以显着提高安全性,原因很明显)。
哈希与相似度无关。此外,大多数哈希算法对于极其相似的字符串产生的结果几乎完全不同。例如 SHA1 的:
The quick brown fox jumps over the lazy dog
= 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
和一个符号的区别是完全不同的字符串:
The quick brown fox jumps over the lazy cog
= de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3
有一些简单且广为人知的算法可用于查找字符串相似性。 Hamming distance for strings of the same length, Levenshtein distance for different length string are just a few examples of string metrics
P.S。如果你真的真的想使用散列 - 这种类型的散列的正确术语是 locality preserving hash. You can read about it here.
我建议查看 Nilsimsa 算法以获得介绍。
"The goal of Nilsimsa is to generate a hash digest of an email message such that the digests of two similar messages are similar to each other"