Skip to content

Instantly share code, notes, and snippets.

@devi
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save devi/ef05a3f04b84b3c6c159 to your computer and use it in GitHub Desktop.

Select an option

Save devi/ef05a3f04b84b3c6c159 to your computer and use it in GitHub Desktop.
(uglified) blake2s implementation for javascript
!function(n){"use strict";function e(n,e){var r=255&n[e];return r|=(255&n[e+1])<<8,r|=(255&n[e+2])<<16,r|=(255&n[e+3])<<24}function r(n,e,r){n[e]=255&r,r>>>=8,n[e+1]=255&r,r>>>=8,n[e+2]=255&r,r>>>=8,n[e+3]=255&r}function f(){for(var n=0,e=0;e<arguments.length;e++)n=n+arguments[e]>>>0;return n}function t(n,e){return(n>>>e|n<<32-e)>>>0}function o(n,e,r,o,u,i){g[r]=f(g[r],g[o],p[d[n][2*e]]),g[i]=t(g[i]^g[r],16),g[u]=f(g[u],g[i]),g[o]=t(g[o]^g[u],12),g[r]=f(g[r],g[o],p[d[n][2*e+1]]),g[i]=t(g[i]^g[r],8),g[u]=f(g[u],g[i]),g[o]=t(g[o]^g[u],7)}function u(n,r){n.t[0]+=r,n.t[1]+=n.t[0]<r?1:0;var f=0;for(f=16;f--;)p[f]=e(n.buf,4*f);for(f=8;f--;)g[f]=n.h[f];for(g[8]=b[0],g[9]=b[1],g[10]=b[2],g[11]=b[3],g[12]=(b[4]^n.t[0])>>>0,g[13]=(b[5]^n.t[1])>>>0,g[14]=(b[6]^n.f[0])>>>0,g[15]=(b[7]^n.f[1])>>>0,f=0;10>f;f++)o(f,0,0,4,8,12),o(f,1,1,5,9,13),o(f,2,2,6,10,14),o(f,3,3,7,11,15),o(f,4,0,5,10,15),o(f,5,1,6,11,12),o(f,6,2,7,8,13),o(f,7,3,4,9,14);for(f=8;f--;)n.h[f]^=g[f]^g[f+8]}function i(n,e,r){r=0|r;for(var f=0,t=0;r>0;){var o=n.buflen,i=128-o;if(r>i){for(f=i;f--;)n.buf[f+o]=e[f+t];for(n.buflen+=i,u(n,64),f=64;f--;)n.buf[f]=n.buf[f+64];n.buflen-=64,t+=i,r-=i}else{for(f=r;f--;)n.buf[f+o]=e[f+t];n.buflen+=r,t+=r,r-=r}}}function a(n){var e=0;if(n.buflen>64)for(u(n,64),n.buflen-=64,e=n.buflen;e--;)n.buf[e]=n.buf[e+64];for(n.f[0]=4294967295,e=n.buflen;128>e;e++)n.buf[e]=0;u(n,n.buflen);var f=new Array(32);for(e=8;e--;)r(f,4*e,n.h[e]);return f}function h(n){var e=[];n=unescape(encodeURIComponent(n));for(var r=n.length;r--;)e[r]=n.charCodeAt(r);return e}function c(n){var r="string"==typeof n?h(n):n,f="undefined"!=typeof n?n.length:0;f>32&&(f=32);var t=new v;if(t.h[0]^=e([32,f,1,1],0),f>0){var o,u=[];for(o=64;o--;)u[o]=0;for(o=f;o--;)u[o]=r[o];i(t,u,64)}return t}function l(n){for(var e="0123456789abcdef",r="",f=0;f<n.length;f++)r+=e[n[f]>>4&15],r+=e[15&n[f]];return r}function s(n){n.replace(/([^0-9a-f])/g,"");var e=[],r=n.length,f="";if(r%2===1)throw new Error("Odd length hex string");for(var t=0;r>t;t+=2)f=n[t],f+=t+1>=r||"undefined"==typeof n[t+1]?"0":n[t+1],e.push(parseInt(f,16));return e}var b=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],d=[[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],[14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3],[11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4],[7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8],[9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13],[2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9],[12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11],[13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10],[6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5],[10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0]],v=function(){this.h=b.slice(0),this.t=[0,0],this.f=[0,0],this.buf=new Array(128),this.buflen=0},g=[],p=[],w=64,y=32,E=32;n.BLOCKBYTES=w,n.OUTBYTES=y,n.KEYBYTES=E,n.init=c,n.update=i,n.finish=a,n.decodeString=h,n.encodeHex=l,n.decodeHex=s,n.hash=function(n,e){if("undefined"!=typeof e&&e.length!==E)throw new Error("Invalid key size.");var r="string"==typeof n?h(n):n,f=c(e);return i(f,r,r.length),l(a(f))}}("undefined"!=typeof exports?exports:window.blake2s=window.blake2s||{});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment