Changeset - f425e00a94c6
[Not reviewed]
default
4 1 6
Laman - 5 years ago 2019-06-29 16:42:59

bundling with Rollup
7 files changed with 30 insertions and 50 deletions:
0 comments (0 inline, 0 general)
dist/crypto.html
Show inline comments
 
file renamed from crypto.html to dist/crypto.html
 
<!DOCTYPE html>
 
<html>
 
<head>
 
	<script type="text/javascript" src="util.js"></script>
 
	<script type="text/javascript" src="blake.js"></script>
 
	<meta charset="utf-8" />
 
	<script type="text/javascript" src="main.js"></script>
 
</head>
 
<body>
 
	<script type="text/javascript">
 
		console.log(str2utf8("abc"));
 
		console.log(cryptoJS.util.str2utf8("abc"));
 
	</script>
 
</body>
 
</html>
rollup.config.js
Show inline comments
 
new file 100644
 
module.exports = {
 
  input: 'src/main.js',
 
  output: {
 
    file: 'dist/main.js',
 
    format: 'iife',
 
		name: "cryptoJS"
 
  }
 
};
spec/test/utilSpec.js
Show inline comments
 
/* global expect */
 

	
 
describe("Util",function(){
 
  var util = require("../../util.js");
 
	var utf=[ // https://tools.ietf.org/html/rfc3629#page-8
 
  let util=require("../../src/util.js");
 
	let utf=[ // https://tools.ietf.org/html/rfc3629#page-8
 
		["",[]],
 
		["abc",[97,98,99]],
 
		["å",[195,165]],
 
		["🚅",[240,159,154,133]],
 
		["žír",[0xc5,0xbe,0xc3,0xad,114]],
 
		["A\u2262\u0391.",[0x41,0xE2,0x89,0xA2,0xCE,0x91,0x2E]],
src/blake.js
Show inline comments
 
file renamed from blake.js to src/blake.js
 
@@ -9,13 +9,13 @@ function padEnd(arr,length,val=0){
 
}
 

	
 
function rrot(x,shift){
 
	return ((x>>>shift)|(x<<(32-shift)))&MASK;
 
}
 

	
 
function BLAKE2S(outputLen=32,key=[]){
 
export function BLAKE2S(outputLen=32,key=[]){
 
	this._buffer=[];
 
	this._dataLen=[0,0]; // low, high
 
	this._outputLen=outputLen;
 
	
 
	this._state=IV.slice();
 
	this._state[0]^=0x01010000^(key.length<<8)^this._outputLen;
 
@@ -82,28 +82,11 @@ BLAKE2S.prototype._mix=function(arr,ia,i
 
	c=(c+d)&MASK; b=rrot(b^c,12);
 
	a=(a+b+y)&MASK; d=rrot(d^a,8);
 
	c=(c+d)&MASK; b=rrot(b^c,7);
 
	arr[ia]=a; arr[ib]=b; arr[ic]=c; arr[id]=d;
 
};
 

	
 
function blake2s(data,outputLen=32,key=[]){
 
export function blake2s(data,outputLen=32,key=[]){
 
	let h=new BLAKE2S(outputLen,key);
 
	for(let i=0;i<data.length;i+=BLOCK_LEN){h.update(data.slice(i,i+BLOCK_LEN));}
 
	return h.digest();
 
}
 

	
 
function testBlake(){
 
	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");
 
}
src/chacha.js
Show inline comments
 
file renamed from chacha.js to src/chacha.js
 
@@ -7,13 +7,13 @@ function lrot(x,shift){
 
function createNonce(){
 
	let nonce=new Uint8Array(12);
 
	window.crypto.getRandomValues(nonce);
 
	return nonce;
 
}
 

	
 
function Chacha20(key,nonce){
 
export function Chacha20(key,nonce){
 
	if(nonce===undefined){
 
		let nonce=createNonce();
 
	}
 
	nonce=bytes2int32s(nonce);
 
	key=bytes2int32s(key);
 
	
 
@@ -67,19 +67,12 @@ Chacha20.prototype._computeBlock=functio
 
};
 

	
 
Chacha20.prototype._incrementPos=function(){
 
	this._state[12]=(this._state[12]+1)&MASK;
 
};
 

	
 
function encrypt(message,key){
 
	let nonce=Date.now();
 
	let nonce0=nonce;
 
	let hashedKey=blake2s(str2bytes(key));
 
	message=str2bytes(message);
 
}
 

	
 
function testChacha(){
 
	let key=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
 
	let nonce=[0,0,0,9,0,0,0,74,0,0,0,0];
 
	let cipher=new Chacha20(key,nonce);
 
	cipher.setPos(1);
 
	let output=cipher._computeBlock().map(x=>(x>>>0).toString(16).padStart(8,"0"));
src/main.js
Show inline comments
 
new file 100644
 
import * as util from "./util.js";
 
import {blake2s} from "./blake.js";
 
import {Chacha20} from "./chacha.js";
 

	
 
export default {util,blake2s,Chacha20};
 
	
 
\ No newline at end of file
src/util.js
Show inline comments
 
file renamed from util.js to src/util.js
 
const MASK=0xffffffff;
 
export const MASK=0xffffffff;
 

	
 
function bytes2int32(arr){
 
export function bytes2int32(arr){
 
	return arr.reduce((acc,b,i)=>acc|b<<(i*8));
 
}
 

	
 
function bytes2int32s(arr){
 
export function bytes2int32s(arr){
 
	let res=[];
 
	for(let i=0;i<arr.length;i+=4){
 
		res.push(bytes2int32(arr.slice(i,i+4)));
 
	}
 
	return res;
 
}
 

	
 
function int322bytes(x){
 
export function int322bytes(x){
 
	let res=[];
 
	for(let i=0;i<4;i++){
 
		res.push(x&0xff);
 
		x>>>=8;
 
	}
 
	return res;
 
}
 

	
 
function int32s2bytes(arr){
 
export function int32s2bytes(arr){
 
	return arr.map(int322bytes).reduce((acc,bytes)=>acc.concat(bytes));
 
}
 

	
 
function bytes2hex(arr){
 
export function bytes2hex(arr){
 
	return arr.map(x=>x.toString(16).padStart(2,"0")).join("");
 
}
 

	
 
function str2utf8(s){
 
export function str2utf8(s){
 
	let res=[];
 
	let c=s.codePointAt(0);
 
	for(let i=0; c!==undefined; i++,c=s.codePointAt(i)){
 
		if(c<0x80){res.push(c);}
 
		else if(c<0x800){
 
			res.push(0b11000000|(c>>>6));
 
@@ -51,13 +51,13 @@ function str2utf8(s){
 
		}
 
		if(c>0xffff){i++;} // skip surrogate
 
	}
 
	return res;
 
}
 

	
 
function utf82str(arr){
 
export function utf82str(arr){
 
	let res=[];
 
	for(let i=0;i<arr.length;i++){
 
		let x=arr[i];
 
		if(x<=0b1111111){res.push(x);}
 
		else if(x<=0b11011111){
 
			let a=x&0b11111;
 
@@ -77,16 +77,6 @@ function utf82str(arr){
 
			let d=arr[++i]&0b111111;
 
			res.push(a<<18|b<<12|c<<6|d);
 
		}
 
	}
 
	return res.map(x=>String.fromCodePoint(x)).join("");
 
}
 

	
 
if(typeof module!=='undefined'&&module.hasOwnProperty('exports')){
 
	module.exports.bytes2int32=bytes2int32;
 
	module.exports.bytes2int32s=bytes2int32s;
 
	module.exports.int322bytes=int322bytes;
 
	module.exports.int32s2bytes=int32s2bytes;
 
	module.exports.bytes2hex=bytes2hex;
 
	module.exports.str2utf8=str2utf8;
 
	module.exports.utf82str=utf82str;
 
}
0 comments (0 inline, 0 general)