123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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;
- }
- }
|