Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created September 7, 2011 13:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jed/1200559 to your computer and use it in GitHub Desktop.
Save jed/1200559 to your computer and use it in GitHub Desktop.
adler32 checksum
function(a){for(var b=65521,c=1,d=0,e=0,f;f=a.charCodeAt(e++);d=(d+c)%b)c=(c+f)%b;return d<<16|c}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "adler32",
"description": "a javascript port of wikipedia's example C implementation of Adler32",
"keywords": [
"checksum",
"hash",
"adler32"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>1802963080</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var adler32 = function(a){for(var b=65521,c=1,d=0,e=0,f;f=a.charCodeAt(e++);d=(d+c)%b)c=(c+f)%b;return d<<16|c}
document.getElementById( "ret" ).innerHTML = adler32("140byt.es is totally rad!")
</script>
@subzey
Copy link

subzey commented Sep 7, 2011

@jed, Nice one!

Placeholder arguments aren't panacea for js minification, straightforward var declaration is 2 bytes shorter:

function(a){for(var b=65521,c=1,d=0,e=0,f;f=a.charCodeAt(e++);d=(d+c)%b)c=(c+f)%b;return(d<<16)|c}

@jed
Copy link
Author

jed commented Sep 7, 2011

great point, @subzey, and very easy to forget. i'll fix this on the fletcher too.

EDIT: actually, it looks like fletcher doesn't need it.

@p01
Copy link

p01 commented Sep 7, 2011

Also, return c|d<<16 is 1 byte shorter

@jed
Copy link
Author

jed commented Sep 7, 2011

thanks again, @p01!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment