Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

javascript - Fast open source checksum for small strings

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".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

schnaader's implementation is indeed very fast. Here it is in Javascript:

function checksum(s)
{
  var chk = 0x12345678;
  var len = s.length;
  for (var i = 0; i < len; i++) {
      chk += (s.charCodeAt(i) * (i + 1));
  }

  return (chk & 0xffffffff).toString(16);
}

Using Google Chrome, this function takes just 5ms to run for 1-megabyte strings, versus 330ms using a crc32 function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...