轻量级校验和算法的好选择

轻量级校验和算法的好选择

本文介绍了轻量级校验和算法的好选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了一致性的目的,我发现自己需要为一串数据生成一个校验和。广泛的想法是,客户端可以根据它收到的有效载荷重新生成校验和,从而检测传输过程中发生的任何损坏。我隐约地意识到这种事情背后有各种数学原理,如果你自己试着滚动它,那么微妙的错误很容易使整个算法失效。



所以我正在寻找哈希/校验和算法的建议,使用以下标准:


  • 它将生成Javascript,所以需要相对较轻的计算。

  • 验证将由Java完成(尽管我看不到这实际上是一个问题)。 >这将需要文本输入(URL编码的Unicode,我相信是ASCII)的中等长度;通常大约200-300个字符,并且在所有情况下低于2000.

  • 输出也应该是ASCII文本,越短越好。



我主要对轻量级的东西感兴趣,而不是尽可能减少碰撞的绝对最小可能。我会天真地想象八字符散列会适合这个吗?我还应该澄清,如果在验证阶段没有发现腐败现象(并且我意识到这不会100%可靠),那么这并不是世界末日,尽管我的其他代码对于每个人来说效率明显较低腐败的条目滑过。



编辑 - 感谢所有贡献。我选择了Adler32选项,并且考虑到它本身在Java中得到了支持,在Javascript中实现起来非常简单,可以快速计算两端并具有8字节输出,这完全符合我的要求。



(请注意,我意识到网络传输不太可能对任何损坏错误负责,并且不会在这个问题上折腾我的手臂;但是添加校验和验证可以消除一点失败,并意味着我们可以专注于其他领域。)

解决方案

CRC32在任何语言中都不难实现,它足以检测简单的数据损坏情况,并且以良好的方式实施时,速度非常快。但是,您也可以尝试Adler32,它几乎与CRC32一样好,但实现起来更容易(并且速度相当快)。





这两个(或者甚至是两者)均可在Java中使用。


I find myself needing to generate a checksum for a string of data, for consistency purposes. The broad idea is that the client can regenerate the checksum based on the payload it recieves and thus detect any corruption that took place in transit. I am vaguely aware that there are all kinds of mathematical principles behind this kind of thing, and that it's very easy for subtle errors to make the whole algorithm ineffective if you try to roll it yourself.

So I'm looking for advice on a hashing/checksum algorithm with the following criteria:

  • It will be generated by Javascript, so needs to be relatively light computationally.
  • The validation will be done by Java (though I cannot see this actually being an issue).
  • It will take textual input (URL-encoded Unicode, which I believe is ASCII) of a moderate length; typically around 200-300 characters and in all cases below 2000.
  • The output should be ASCII text as well, and the shorter it can be the better.

I'm primarily interested in something lightweight rather than getting the absolute smallest potential for collisions possible. Would I be naive to imagine that an eight-character hash would be suitable for this? I should also clarify that it's not the end of the world if corruption isn't picked up at the validation stage (and I do realise that this will not be 100% reliable), though the rest of my code is markedly less efficient for every corrupt entry that slips through.

Edit - thanks to all that contributed. I went with the Adler32 option and given that it was natively supported in Java, extremely easy to implement in Javascript, fast to calculate at both ends and have an 8-byte output it was exactly right for my requirements.

(Note that I realise that the network transport is unlikely to be responsible for any corruption errors and won't be folding my arms on this issue just yet; however adding the checksum validation removes one point of failure and means we can focus on other areas should this reoccur.)

解决方案

CRC32 is not too hard to implement in any language, it is good enough to detect simple data corruption and when implemted in a good fashion, it is very fast. However you can also try Adler32, which is almost equally good as CRC32, but it's even easier to implement (and about equally fast).

Adler32 in the Wikipedia

CRC32 JavaScript implementation sample

Either of these two (or maybe even both) are available in Java right out of the box.

这篇关于轻量级校验和算法的好选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 16:34