Hector hash-based symmetrical encryption system (JS implementation)


SUBMITTED BY: Guest

DATE: March 22, 2013, 8:09 a.m.

FORMAT: JavaScript

SIZE: 4.2 kB

HITS: 1229

  1. //sha256
  2. function rotateRight(a,b){return b>>>a|b<<32-a}function choice(a,b,c){return a&b^~a&c}function majority(a,b,c){return a&b^a&c^b&c}function sha256_Sigma0(a){return rotateRight(2,a)^rotateRight(13,a)^rotateRight(22,a)}function sha256_Sigma1(a){return rotateRight(6,a)^rotateRight(11,a)^rotateRight(25,a)}function sha256_sigma0(a){return rotateRight(7,a)^rotateRight(18,a)^a>>>3}function sha256_sigma1(a){return rotateRight(17,a)^rotateRight(19,a)^a>>>10}function sha256_expand(a,b){return a[15&b]+=sha256_sigma1(a[15&b+14])+a[15&b+9]+sha256_sigma0(a[15&b+1])}function safe_add(a,b){var c=(65535&a)+(65535&b),d=(a>>16)+(b>>16)+(c>>16);return d<<16|65535&c}function sha256_init(){ihash=Array(8),count=Array(2),buffer=Array(64),count[0]=count[1]=0,ihash[0]=1779033703,ihash[1]=3144134277,ihash[2]=1013904242,ihash[3]=2773480762,ihash[4]=1359893119,ihash[5]=2600822924,ihash[6]=528734635,ihash[7]=1541459225}function sha256_transform(){var a,b,c,d,e,f,g,h,i,j,k=Array(16);a=ihash[0],b=ihash[1],c=ihash[2],d=ihash[3],e=ihash[4],f=ihash[5],g=ihash[6],h=ihash[7];for(var l=0;16>l;l++)k[l]=buffer[(l<<2)+3]|buffer[(l<<2)+2]<<8|buffer[(l<<2)+1]<<16|buffer[l<<2]<<24;for(var m=0;64>m;m++)i=h+sha256_Sigma1(e)+choice(e,f,g)+K256[m],i+=16>m?k[m]:sha256_expand(k,m),j=sha256_Sigma0(a)+majority(a,b,c),h=g,g=f,f=e,e=safe_add(d,i),d=c,c=b,b=a,a=safe_add(i,j);ihash[0]+=a,ihash[1]+=b,ihash[2]+=c,ihash[3]+=d,ihash[4]+=e,ihash[5]+=f,ihash[6]+=g,ihash[7]+=h}function sha256_update(a,b){var c,d,e=0;d=63&count[0]>>3;var f=63&b;for(b<<3>(count[0]+=b<<3)&&count[1]++,count[1]+=b>>29,c=0;b>c+63;c+=64){for(var g=d;64>g;g++)buffer[g]=a.charCodeAt(e++);sha256_transform(),d=0}for(var g=0;f>g;g++)buffer[g]=a.charCodeAt(e++)}function sha256_final(){var a=63&count[0]>>3;if(buffer[a++]=128,56>=a)for(var b=a;56>b;b++)buffer[b]=0;else{for(var b=a;64>b;b++)buffer[b]=0;sha256_transform();for(var b=0;56>b;b++)buffer[b]=0}buffer[56]=255&count[1]>>>24,buffer[57]=255&count[1]>>>16,buffer[58]=255&count[1]>>>8,buffer[59]=255&count[1],buffer[60]=255&count[0]>>>24,buffer[61]=255&count[0]>>>16,buffer[62]=255&count[0]>>>8,buffer[63]=255&count[0],sha256_transform()}function sha256_encode_bytes(){for(var a=0,b=Array(32),c=0;8>c;c++)b[a++]=255&ihash[c]>>>24,b[a++]=255&ihash[c]>>>16,b[a++]=255&ihash[c]>>>8,b[a++]=255&ihash[c];return b}function sha256_encode_hex(){for(var a=new String,b=0;8>b;b++)for(var c=28;c>=0;c-=4)a+=sha256_hex_digits.charAt(15&ihash[b]>>>c);return a}function sha256(a){return sha256_init(),sha256_update(a,a.length),sha256_final(),sha256_encode_hex()}var K256=Array(1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298),ihash,count,buffer,sha256_hex_digits="0123456789abcdef";
  3. //utils
  4. function utf82bin(s){return unescape(encodeURIComponent(s))}
  5. function bin2utf8(d){return decodeURIComponent(escape(d))}
  6. function hex2bin(s){var b="";for(var i=0;i<s.length;i+=2)b+=String.fromCharCode(parseInt(s.substr(i,2),16));return b}
  7. function sha256bin(d){return hex2bin(sha256(d))}
  8. function bloxor(a,b){var r="",i;for(i=0;i<32;i++)r+=String.fromCharCode((a.charCodeAt(i)||0)^(b.charCodeAt(i)||0));return r}
  9. //crypto
  10. function keygen(id, pass){return sha256bin(utf82bin(pass+id))}
  11. function hector(data, key){
  12. var KEF=function(k){return k.split('').reverse().join('')+'XPADDINGXPADDINGXPADDING'+k},
  13. HF=function(g,k){return sha256bin(bloxor(k,sha256bin(bloxor(k,g))))},
  14. gamma=sha256bin(KEF(key)),res="",i;
  15. for(i=0;i<32;i++)gamma=HF(gamma,key); //preseed
  16. for(i=0;i<data.length;i+=32)res+=bloxor(data.substr(i,32),gamma=HF(gamma,key));
  17. return res
  18. }

comments powered by Disqus