Changeset - 7eec13103f80
[Not reviewed]
default
0 3 0
Laman - 5 years ago 2019-07-03 16:49:43

encrypt/decrypt wrappers
3 files changed with 37 insertions and 5 deletions:
0 comments (0 inline, 0 general)
dist/crypto.html
Show inline comments
 
@@ -2,11 +2,19 @@
 
<html>
 
<head>
 
	<meta charset="utf-8" />
 
	<title>Chacha20</title>
 
	<script type="text/javascript" src="main.js"></script>
 
</head>
 
<body>
 
	<form name="chacha">
 
		<textarea name="input"></textarea>
 
		<input type="password" name="password" />
 
		<button type="button" name="encrypt">zašifrovat</button>
 
		<button type="button" name="decrypt">dešifrovat</button>
 
		<textarea name="output"></textarea>
 
	</form>
 
	<script type="text/javascript">
 
		console.log(cryptoJS.util.str2utf8("abc"));
 
		></i>
 
	</script>
 
</body>
 
</html>
src/chacha.js
Show inline comments
 
@@ -8,7 +8,7 @@ function lrot(x,shift){
 
function createNonce(){
 
	let nonce=new Uint8Array(12);
 
	window.crypto.getRandomValues(nonce);
 
	return nonce;
 
	return Array.from(nonce);
 
}
 

	
 
/**
src/main.js
Show inline comments
 
import * as util from "./util.js";
 
import {blake2s} from "./blake.js";
 
import {pbkdf2} from "./pbkdf2.js";
 
import {Chacha20,encrypt,decrypt} from "./chacha.js";
 
import {Chacha20,encrypt as _encrypt,decrypt as _decrypt} from "./chacha.js";
 

	
 
const VERSION=1;
 

	
 
function encrypt(s,password){
 
	let bs=util.str2utf8(s);
 
	let pass=util.str2utf8(password);
 
	let noncedCiphertext=_encrypt(bs,pass);
 
	let signature=blake2s([VERSION].concat(noncedCiphertext),16,pass);
 
	let arr=[VERSION].concat(signature,noncedCiphertext);
 
	return util.bytes2base64(arr);
 
}
 

	
 
function decrypt(s,password){
 
	let pass=util.str2utf8(password);
 
	let arr=util.base642bytes(s);
 
	let version=arr[0];
 
	let signature=arr.slice(1,17);
 
	let noncedCiphertext=arr.slice(17);
 
	let check=blake2s([version].concat(noncedCiphertext),16,pass);
 
	if(!signature.every((b,i)=>b===check[i])){return false;}
 
	if(version>VERSION){return false;}
 
	let plainbytes=_decrypt(noncedCiphertext,pass);
 
	return util.utf82str(plainbytes);
 
}
 

	
 
export default {util,blake2s,pbkdf2,Chacha20,encrypt,decrypt};
 

	
 
@@ -11,6 +35,6 @@ if(typeof module!=='undefined'&&module.h
 
	module.exports.blake2s=blake2s;
 
	module.exports.pbkdf2=pbkdf2;
 
	module.exports.Chacha20=Chacha20;
 
	module.exports.encrypt=encrypt;
 
	module.exports.decrypt=decrypt;
 
	module.exports.encrypt=_encrypt;
 
	module.exports.decrypt=_decrypt;
 
}
0 comments (0 inline, 0 general)