Skip to content

Instantly share code, notes, and snippets.

@bryc
Created March 29, 2018 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryc/2e933c20ff71bb599e97d795c8e7ec3e to your computer and use it in GitHub Desktop.
Save bryc/2e933c20ff71bb599e97d795c8e7ec3e to your computer and use it in GitHub Desktop.
/*
a mockup of a fileformat with a 8 byte header. 56-bit magic, 8-bit version code, and 64-bit hash.
*/
var data = [250,222,222,175,98,83,80,0,149,227,98,114,223,2,65,139,212,54,64,54,169,172,160,90,58,126,203,87,217,240,53,186,160,226,221,133,70,56,95,24,49,66,201,41,182,75,120,164,236,222,80,145,147,117,15,88,219,136,158,2,20,70,140,78,163,103,32,199,173,219,175,24,97,78,33,93,215,188,212,50,218,155,63,171,40,68,17,134,244,173,170,70,180,159,32,245,135,233,57,203,196,84,18,249,120,73,72,71,223,101,226,87,42,132,104,173,25,242,197,218,55,191,232,94,192,124,90,38,130,209,31,191,73,175,162,149,144,23,144,228,157,185,200,43,59,215,220,70,173,60,188,142,168,51,132,136,82,197,155,28,157,175,122,82,138,217,234,9,24,64,128,54,248,145,159,140,63,244,94,168,188,196,252,32,159,36,223,9,154,195,52,215,181,53,168,184,107,167,233,180,210,240,211,64,230,41,117,26,64,97,101,137,182,14,0,84,201,78,142,38,219,37,110,27,125,58,187,170,101,208,21,58,211,245,119,11,118,33,74,61,23,197,29,34,155,132,182,48,37,20,89,44,106,253,131,111,120,184,121,98,131,239,85,134,135,197,144,166,201,126,19,29,117,237,102,50,1,116,127,254,9,123,60,104,181,192,163,30,1,207,90,162,100,131,6,194,173,118,114,32,18,167,186,168,220,203,211,251,19,13,51,77,28,46,105,46,56,74,240,57,68,193,141,173,62,240,247,95,85,197,170,143,156,105,27,142,164,115,172,66,143,102,26,238,91,120,67,212,134,216,130,145,68,245,243,75,216,124,108,19,231,16,246,12,194,244,8,71,248,29,237,82,197,120,243,250,22,78,100,183,72,14,99,192,213,245,34,200,140,83,30,212,29,93,71,180,245,221,70,10,216,39,69,249,156,32,241,201,242,67,153,52,55,240,147,31,161,113,99,245,77,132,57,225,5,82,198,167,188,27,141,214,23,17,23,135,143,31,185,251,231,147,224,174,35,233,6,208,36,58,180,36,156,2,92,185,1,59,204,10,173,10,140,108,93,128,123,213,169,27,101,114,65,26,113,141,237,199,161,181,178,72,199,46,147,165,157,32,201,8,232,8,119,108,227,81,36,60,62,59,143,156,132,84,161,90,178,60,142,189,64,117,110,182,1,111,220,40,130,32,102,77,221,204,217,100,38,208];
function micromush(t){for(var h,a=1,e=0,c=-4&t.length;e<c;e+=4)a+=t[e+3]<<24|t[e+2]<<16|t[e+1]<<8|t[e],a=Math.imul(a,1540483507),a^=a>>>24;switch(3&t.length){case 3:a^=t[e+2]<<16;case 2:a^=t[e+1]<<8;case 1:a^=t[e],a=Math.imul(a,3432918353)}return h=3127224669^(a^=t.length),a=Math.imul(a,3432918353),[(a^=a>>>15)>>>0,h>>>0]}
function headerCheck(data) {
var sum = [], hdr = [250,222,222,175,98,83,80], i = hdr.length;
// check for 56-bit file magic in header. terminate if bytes don't match.
while(i--) { if(data[i] !== hdr[i]) return false; }
// extract the 64-bit stored hash, overwrite with 0xFF (initialize)
for(i = 0; i < 8; i++) sum[i] = data[i+8], data[i+8] = 255;
// compute new hash and compare with stored hash.
var hash = micromush(data), hash2 = new DataView(new Uint8Array(sum).buffer);
if(hash[0] === hash2.getUint32(0, false) && hash[1] === hash2.getUint32(4, false)) {
return true;
} else {
return false;
}
}
headerCheck(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment