小字符串的快速开源校验和
我需要一个小字符串(20-500 个字符)的快速校验和(尽可能快)。
我需要源代码,而且它一定很小! (最多大约 100 LOC)
如果它可以生成 Base32/64 的字符串。 (或类似的东西)那就完美了。 基本上校验和不能使用任何“坏”字符..你知道..通常的 (){}[].,;:/+-\| 等等
澄清
它可能是强/弱,这实际上并不重要,因为它仅用于幕后目的。
它不需要包含原始字符串的所有数据,因为我只会与生成的校验和进行比较,我不期望任何类型的“解密”。
I need a quick checksum (as fast as possilbe) for small strings (20-500 chars).
I need the source code and that must be small! (about 100 LOC max)
If it could generate strings in Base32/64. (or something similar) it would be perfect. Basically the checksums cannot use any "bad" chars.. you know.. the usual (){}[].,;:/+-\| etc
Clarifications
It could be strong/weak, that really doesn't matter since it is only for behind-the-scenes purposes.
It need not contain all the data of the original string since I will be only doing comparison with generated checksums, I don't expect any sort of "decryption".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在C中快速实现,我这边没有版权,所以你可以随意使用它。 但请注意,这是一个非常弱的“校验和”,所以不要将它用于严肃的事情:) - 但这就是您想要的,不是吗?
这将返回一个 32 位整数校验和,编码为包含其十六进制值的字符串。
如果校验和函数不能满足您的需求,您可以将
chk += ((int)(str[i]) * (i + 1));
行更改为更好的值(fe 乘法) ,加法和按位旋转会好得多)。编辑:根据hughdbrown的建议和他链接的答案之一,我将
更改为
循环,因此它不会在每次迭代时调用strlen
。Quick implementation in C, no copyrights from my side, so use it as you wish. But please note that this is a very weak "checksum", so don't use it for serious things :) - but that's what you wanted, isn't it?
This returns an 32-bit integer checksum encoded as an string containing its hex value.
If the checksum function doesn't satisfy your needs, you can change the
chk += ((int)(str[i]) * (i + 1));
line to something better (f.e. multiplication, addition and bitwise rotating would be much better).EDIT: Following hughdbrown's advice and one of the answers he linked, I changed the
for
loop so it doesn't callstrlen
with every iteration.几乎您能想到的任何算法都可以满足您的标准。 例如,
使其成为“坏字符安全”
尝试通过增加输出域来减少冲突次数
通过计算不同排列的不同值来进一步减少冲突次数的解决方案
一般来说,所有这些变体都表现良好如果输入足够随机并且足够稀疏(当然,“足够”在每种情况下都不同)
Pretty much any algorithm you could come up with would satisfy your criteria. E.g.
to make it "bad-char-safe"
An attempt that tries to reduce the number of collisions by increasing the output domain
A solution that further reduces the number of collisions by calculating different values for different permuations
In general, all these variations perform pretty well if the input is random enough and sparse enough (of course, "enough" differs in each case)
它有 207 行,但它是 md5 的 javascript 实现:
http://www.webtoolkit。 info/javascript-md5.html
将其与 javascript base64 结合起来:
http:// www.webtoolkit.info/javascript-base64.html
这些脚本是完全独立的,因此它们有一些冗余(例如 UTF-8 编码/解码),可以轻松地对它们进行通用。
编辑:在同一站点上,您可以找到 javascript crc32:
http: //www.webtoolkit.info/javascript-crc32.html
It is 207 lines, but it is a javascript implementation of md5:
http://www.webtoolkit.info/javascript-md5.html
Couple that with a javascript base64:
http://www.webtoolkit.info/javascript-base64.html
These scripts are totally self contained, so they have some redundancies (such as the UTF-8 encode/decode) that could easily be made common to them.
EDIT: On the same site you can find a javascript crc32:
http://www.webtoolkit.info/javascript-crc32.html
要仅使用 javascript,您可以使用此 crc 函数: http://www.webtoolkit.info/ 在 php 中的javascript-crc32.html
中,您可以使用带有 md5 哈希的一行来完成,对于长度为 20 - 500 的字符串,它应该相当快,
这里有一些更多信息: http://us.php.net/md5
另外,如果你想选择哈希算法,你可以使用 hash php 函数: http://us.php.net/manual/en/function.hash.php< /a>
to use just javascript you could possibly use this crc function: http://www.webtoolkit.info/javascript-crc32.html
in php you could do it with one line with an md5 hash, on a string of length 20 - 500 it should be pretty fast
here's some more info: http://us.php.net/md5
also, if you wanted to choose your hashing algorithm you could use the hash php function: http://us.php.net/manual/en/function.hash.php
适用于现代(非 IE)浏览器,必须使用 HTTPS:
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
上面大约需要4.5ms,短句和这长句没有什么区别。 没有尝试过很长的字符串。
Works in modern (non IE) browsers, has to be on HTTPS:
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
The above takes about 4.5ms, a short sentence and this long one made no difference. Didn't try it for a very long string.
您正在寻找的是一种为字符串创建哈希码的算法。 在 C# 中:
What you are looking for is an algorithm to create a hash code for a string. In C#:
md5sum、sha1sum、sha224sum、sha256sum、sha384sum、sha512sum 适用于大多数 *nix 发行版
md5sum, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum are available to most *nix distributions
这可能看起来有点晚了,而且部分偏离主题...我使用了 joelpt 的 JS 实现 ://stackoverflow.com/a/811277/336311">schnaader 在基于 JS 和 PHP 的应用程序中的解决方案。 而且,我认为 PHP 实现也可能对其他人有帮助。
This may seem a bit late and partly off topic... I used joelpt's JS-implementation of schnaader's solution in an application based on JS and PHP. And, I assume that the PHP implementation might be helpful for others, as well.
schnaader的执行速度确实非常快。 这是 JavaScript 代码:
使用 Google Chrome,该函数只需 5 毫秒即可运行 1 兆字节的字符串,而使用 crc32 函数则需要 330 毫秒。
schnaader's implementation is indeed very fast. Here it is in Javascript:
Using Google Chrome, this function takes just 5ms to run for 1-megabyte strings, versus 330ms using a crc32 function.
以下是 CRC32 的 Javascript 实现:
位于 kevin.vanzonneveld.net
Here's a Javascript implementation of CRC32:
Found at kevin.vanzonneveld.net