Browse Source

store initialization vector in file

encrypt 7 years ago
parent
commit
50d94d8c8a
2 changed files with 20 additions and 30 deletions
  1. 12 21
      client/crypto.js
  2. 8 9
      client/main.js

+ 12 - 21
client/crypto.js

@@ -1,18 +1,13 @@
 var CryptoUtils = {
     cypher: "AES-GCM",
     key: null,
-    iv: null,
     urlSafeKey: null,
-    urlSafeIV: null,
-    
-    initialize: function(key = null, iv = null) {
-	if(key === null || iv === null) {
-	    this.generateIV();
+  
+    initialize: function(key = null) {
+	if(key === null) {
 	    return this.generateKey();
 	}
 	else {
-	    this.iv = this.base64ToArrayBuffer(iv)
-	    this.urlSafeIV = iv;
 	    this.urlSafeKey = key;
 	    return this.importKey(this.base64ToArrayBuffer(key));
 	}	    
@@ -38,30 +33,30 @@ var CryptoUtils = {
 	});	    
     },
     
-    generateIV: function() {
-	this.iv = window.crypto.getRandomValues(new Uint8Array(12));
-	this.urlSafeIV = this.arrayBufferToBase64(this.iv);
-    },
-
     encrypt: function(data) {
+	var self = this;
+	var iv = window.crypto.getRandomValues(new Uint8Array(12));
 	return window.crypto.subtle.encrypt(
 	    {		
 		name: this.cypher,
-		iv: this.iv,
+		iv: iv,
 	    },
 	    this.key,
 	    data
-	);
+	).then(function(encrypted){
+	    return new Blob([iv, encrypted], {type: 'application/octet-binary'});  
+    	});
     },
     
     decrypt: function(data) {
+	var self = this;
 	return window.crypto.subtle.decrypt(
 	    {
 		name: this.cypher,
-		iv: this.iv,
+		iv: data.slice(0,12),
 	    },
 	    this.key,
-	    data
+	    data.slice(12)
 	);
     },
     
@@ -80,10 +75,6 @@ var CryptoUtils = {
 	});
     },
 
-    importIV: function(iv) {
-	
-    },
-    
     arrayBufferToBase64: function(a) {
 	return btoa(String.fromCharCode(...new Uint8Array(a)))
     },

+ 8 - 9
client/main.js

@@ -55,12 +55,13 @@ var CryptoUploader = {
  
     createRoom: function() {
 	Uploader.createRoom();
-	CryptoUtils.initialize().then(function(){
-	    window.location = window.location.href+"#"+Uploader.roomId+","+CryptoUtils.urlSafeKey+","+CryptoUtils.urlSafeIV;
-	    window.location.reload();
-	})
+	CryptoUtils.initialize()
+	    .then(function(){
+		window.location = window.location.href+"#"+Uploader.roomId+","+CryptoUtils.urlSafeKey;
+		window.location.reload();
+	    })
     },
-
+    
     uploadFile: function() {
 	var fileReader = new FileReader();
 	var data;
@@ -70,7 +71,7 @@ var CryptoUploader = {
 	    this.data = fileReader.result;
 	    CryptoUtils.encrypt(this.data)
 		.then(function(encrypted){
-		    Uploader.uploadFile(file.name, new Blob([encrypted], { type: 'application/octet-binary' }));
+		    Uploader.uploadFile(file.name, encrypted);
 		    self.showRoomContent();
 		}).catch(function(err){
 		    console.error(err);
@@ -104,7 +105,6 @@ var CryptoUploader = {
 	$("#file-list > ul").empty();
 	Uploader.getRoom(function(data){
 	    for(let f of data.entries()) {
-		//var a = document.createElement("a");
 		$("<li><a>"+f[1]+"</a></li>")
 		    .appendTo("#file-list > ul")
 		    .on('click', function(e){CryptoUploader.downloadFile(e.target.text)});
@@ -118,12 +118,11 @@ $(function(){
 	var params = window.location.hash.substr(1).split(',');
 	var roomId = params.shift();
 	var key = params.shift();
-	var iv = params.shift();
 	var file = params.shift();
 	$("#create-room").hide();
 	$("#room").show();
 	Uploader.roomId = roomId;
-	CryptoUtils.initialize(key, iv).then(function(){
+	CryptoUtils.initialize(key).then(function(){
 	    $("#woot").on('click', function(){
 		CryptoUploader.uploadFile();
 	    });