1
0
Ответвление 0
silent-drive/client/crypto.js
2019-08-28 16:19:30 +02:00

137 строки
2,8 КиБ
JavaScript

var CryptoUtils = {
cypher: "AES-GCM",
key: null,
urlSafeKey: null,
initialize: function(key = null) {
if(key === null) {
return this.generateKey();
}
else {
this.urlSafeKey = key;
return this.importKey(this.base64ToArrayBuffer(key));
}
},
generateKey: function() {
var self = this;
return window.crypto.subtle.generateKey(
{
name: this.cypher,
length: 256,
},
true,
["encrypt", "decrypt"]
).then(function(k){
self.key = k;
return window.crypto.subtle.exportKey("raw", k)
}).then(function(keydata) {
console.log(keydata);
self.urlSafeKey = self.arrayBufferToBase64(keydata);
}).catch(function(err){
console.error(err)
});
},
encrypt: function(data) {
var self = this;
var iv = window.crypto.getRandomValues(new Uint8Array(12));
return window.crypto.subtle.encrypt(
{
name: this.cypher,
iv: iv,
},
this.key,
data
).then(function(encrypted){
return new Blob([iv, encrypted], {type: 'application/octet-binary'});
});
},
encryptFilename: function(data, iv) {
var self = this;
iv = this.stringToArrayBuffer(iv);
data = this.stringToArrayBuffer(data);
return window.crypto.subtle.encrypt(
{
name: this.cypher,
iv: iv,
},
this.key,
data
).then(function(encrypted){
return base32.encode(encrypted);
});
},
decrypt: function(data) {
var self = this;
return window.crypto.subtle.decrypt(
{
name: this.cypher,
iv: data.slice(0,12),
},
this.key,
data.slice(12)
);
},
decryptFileName: function(data, iv) {
var self = this;
var data = new Uint8Array(base32.decode.asBytes(data)).buffer;
iv = this.stringToArrayBuffer(iv);
return window.crypto.subtle.decrypt(
{
name: this.cypher,
iv: iv,
},
this.key,
data
);
},
importKey: function(key) {
var self = this;
return window.crypto.subtle.importKey(
"raw",
key,
{
name: self.cypher
},
true,
["encrypt", "decrypt"]
).then(function(k){
self.key = k;
});
},
arrayBufferToBase64: function(a) {
return btoa(String.fromCharCode(...new Uint8Array(a)))
},
base64ToArrayBuffer: function(b) {
var str = atob(b);
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
},
stringToArrayBuffer: function(str) {
var utf8 = str;
return new Uint8Array(utf8.split('').map(function (item) {
return item.charCodeAt();
})).buffer;
},
arrayBufferToString: function(buf) {
buf = new Uint8Array(buf);
var utf8 = Array.from(buf).map(function (item) {
return String.fromCharCode(item);
}).join('');
return utf8;
}
}