Changeset - 0c6c972353fe
[Not reviewed]
default
0 2 0
Laman - 6 years ago 2018-07-07 10:02:02

fixes and tests
2 files changed with 22 insertions and 15 deletions:
blake.js
22
14
0 comments (0 inline, 0 general)
blake.js
Show inline comments
 
@@ -15,7 +15,7 @@ function rrot(x,shift){
 

	
 
function BLAKE2S(outputLen=32,key=[]){
 
	this._buffer=[];
 
	this._dataLen=0;
 
	this._dataLen=[0,0]; // low, high
 
	this._outputLen=outputLen;
 
	
 
	this._state=IV.slice();
 
@@ -26,14 +26,13 @@ function BLAKE2S(outputLen=32,key=[]){
 

	
 
BLAKE2S.prototype.update=function(data){
 
	for(let i=0;i<data.length;i++){
 
		if(this._buffer.length<BLOCK_LEN){
 
			this._buffer.push(data[i]);
 
			this._dataLen++;
 
		}
 
		else{
 
		if(this._buffer.length==BLOCK_LEN){
 
			this._compress(false);
 
			this._buffer=[];
 
		}
 
		this._buffer.push(data[i]);
 
		this._dataLen[0]=(this._dataLen[0]+1)&MASK;
 
		if(this._dataLen[0]<this._buffer.length){this._dataLen[1]++;}
 
	}
 
};
 

	
 
@@ -57,8 +56,8 @@ BLAKE2S.prototype._compress=function(las
 
		[10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0]
 
	];
 
	let v=this._state.concat(IV);
 
	v[12]^=this._dataLen&MASK;
 
	v[13]^=(this._dataLen/0x100000000)&MASK;
 
	v[12]^=this._dataLen[0];
 
	v[13]^=this._dataLen[1];
 
	if(last){v[14]^=MASK;}
 
	let data=bytes2int32s(this._buffer);
 

	
 
@@ -76,7 +75,7 @@ BLAKE2S.prototype._compress=function(las
 
	}
 

	
 
	this._state=this._state.map((x,i)=>x^v[i]^v[i+8]);
 
}
 
};
 

	
 
BLAKE2S.prototype._mix=function(arr,ia,ib,ic,id,x,y){
 
	let a=arr[ia], b=arr[ib], c=arr[ic], d=arr[id];
 
@@ -93,8 +92,17 @@ function blake2s(data,outputLen=32,key=[
 
	return h.digest();
 
}
 

	
 
let msg=[97,98,99];
 
console.log(bytes2hex(blake2s(msg,msg.length,0,16))=="aa4938119b1dc7b87cbad0ffd200d0ae");
 
console.log(bytes2hex(blake2s(msg,msg.length,0,20))=="5ae3b99be29b01834c3b508521ede60438f8de17");
 
console.log(bytes2hex(blake2s(msg,msg.length,0,28))=="0b033fc226df7abde29f67a05d3dc62cf271ef3dfea4d387407fbd55");
 
console.log(bytes2hex(blake2s(msg,msg.length))=="508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982");
 
let msg=str2utf8("abc");
 
let longMsg=str2utf8("0123456789.10.456789.20.456789.30.456789.40.456789.50.456789.60.456789.70.456789.80.456789.90.456789");
 
let key=str2utf8("zoqpiz");
 
let longKey=str2utf8("zoqpizjutyclcmkamzhhmhvchxjtefjy");
 
console.log(bytes2hex(blake2s(msg,16))=="aa4938119b1dc7b87cbad0ffd200d0ae");
 
console.log(bytes2hex(blake2s(msg,20))=="5ae3b99be29b01834c3b508521ede60438f8de17");
 
console.log(bytes2hex(blake2s(msg,28))=="0b033fc226df7abde29f67a05d3dc62cf271ef3dfea4d387407fbd55");
 
console.log(bytes2hex(blake2s(msg))=="508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982");
 
console.log(bytes2hex(blake2s(longMsg))=="59a44e5e417d07fb382505ee7e67c23e0d476d354abc81899960bcab677beee1");
 
console.log(bytes2hex(blake2s(msg,32,key))=="0da0b6a54e8f294b60bb25c572700166ddb9d124257ff36f9f43f18b844adf9f");
 
console.log(bytes2hex(blake2s(msg,32,longKey))=="09ef85c9942bebdeb866c6ade769220fd9b851aead642017f6d59bf7e2a32037");
 
console.log(bytes2hex(blake2s([]))=="69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9");
 
console.log(bytes2hex(blake2s(longMsg.slice(0,64)))=="70484f89974551454d596350dda8af2aa6f0811b527549b9ecfe7adede063753");
 
console.log(bytes2hex(blake2s(longMsg.slice(0,65)))=="af14d4f74947bbde734d0e3015c667cc80676efe4349be235be8046e9e45e0ae");
util.js
Show inline comments
 
@@ -31,7 +31,6 @@ function str2utf8(s){
 
	let res=[];
 
	let c=s.codePointAt(0);
 
	for(let i=0;c!==undefined;i++,c=s.codePointAt(i)){
 
		console.log(c);
 
		if(c<0x80){res.push(c);}
 
		else if(c<0x800){
 
			res.push(0b11000000|(c>>>6));
0 comments (0 inline, 0 general)