Changeset - 5c8fceb4e291
[Not reviewed]
default
0 2 0
Laman - 5 years ago 2019-07-05 23:58:20

I/O, user interface
2 files changed with 31 insertions and 4 deletions:
0 comments (0 inline, 0 general)
dist/crypto.html
Show inline comments
 
<!DOCTYPE html>
 
<html>
 
<head>
 
	<meta charset="utf-8" />
 
	<title>Chacha20</title>
 
	<style type="text/css">
 
		textarea{width:400px;height:160px;}
 
		form[name="chacha"] > *, form[name="chacha"] label > * {display:block;margin-bottom:8px;}
 
	</style>
 
	<script type="text/javascript" src="main.js"></script>
 
</head>
 
<body>
 
	<form name="chacha">
 
		<label>Vstup:
 
		<textarea name="input"></textarea>
 
		</label>
 
		<label>Heslo:
 
		<input type="password" name="password" />
 
		</label>
 
		<div>
 
		<button type="button" name="encrypt">zašifrovat</button>
 
		<button type="button" name="decrypt">dešifrovat</button>
 
		<textarea name="output"></textarea>
 
		</div>
 
		<label>Výstup:
 
			<textarea name="output" readonly></textarea>
 
		</label>
 
	</form>
 
	
 
	<script type="text/javascript">
 
		let form=document.querySelector("form[name='chacha']");
 
		let input=form.querySelector("textarea[name='input']");
 
		let output=form.querySelector("textarea[name='output']");
 
		let password=form.querySelector("input[name='password']");
 
		
 
		form.querySelector("button[name='encrypt']").addEventListener("click",function(){
 
			output.value=cryptoJS.encrypt(input.value,password.value);
 
		});
 
		
 
		form.querySelector("button[name='decrypt']").addEventListener("click",function(){
 
			let res=cryptoJS.decrypt(input.value,password.value);
 
			if(res===1){res="! Nepodařilo se ověřit pravost zprávy. !";}
 
			else if(res===2){res="! Nekompatibilní verze programu. !";}
 
			output.value=res;
 
		});
 
	</script>
 
</body>
 
</html>
src/main.js
Show inline comments
 
@@ -17,35 +17,35 @@ function encrypt(s,password){
 
	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 iters=arr[17];
 
	let salt=arr.slice(18,30);
 
	let ciphertext=arr.slice(30);
 
	let check=blake2s([version,iters].concat(salt,ciphertext),16,pass);
 
	if(!signature.every((b,i)=>b===check[i])){return false;}
 
	if(version>VERSION){return false;}
 
	if(!signature.every((b,i)=>b===check[i])){return 1;}
 
	if(version>VERSION){return 2;}
 
	let key=pbkdf2(pass,salt,1<<iters,32);
 
	let plainbytes=_decrypt(ciphertext,key,salt);
 
	return util.utf82str(plainbytes);
 
}
 

	
 
function stretchKey(password,salt){
 
	let start=Date.now(); // ms
 
	let i,key;
 
	for(i=0;i<256;i++){
 
	for(i=0;i<52;i++){
 
		key=pbkdf2(password,salt,1<<i,32);
 
		if(Date.now()-start>=500){break;}
 
	}
 
	return [i,key];
 
}
 

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

	
 
// export for tests running on Node
 
if(typeof module!=='undefined'&&module.hasOwnProperty('exports')){
 
	module.exports.util=util;
 
	module.exports.blake2s=blake2s;
0 comments (0 inline, 0 general)