Commit d5283e45 authored by lucas.parsy's avatar lucas.parsy

added support of json crypto key in cryptstorage

modified example provided in cryptstorage.js comments.
corrected error introduced by last commit
(renaming of variable causing errors).
modified tests to comply with cryptstorage.js changes
parent 8d3d5de3
......@@ -7,17 +7,24 @@
/*jslint nomen: true*/
/*global jIO, RSVP, DOMException, Blob, crypto, Uint8Array, ArrayBuffer*/
(function (jIO, RSVP, DOMException, Blob) {
(function (jIO, RSVP, DOMException, Blob, crypto, Uint8Array, ArrayBuffer) {
"use strict";
// you the cryptography system used by this storage is AES-GCM.
// here is an example of how to generate a key.
// here is an example of how to generate a key to the json format.
// var key;
// var key,
// jsonKey;
// crypto.subtle.generateKey({name: "AES-GCM",length: 256},
// (true), ["encrypt", "decrypt"])
// .then(function(res){key = res;});
//
// window.crypto.subtle.exportKey("jwk", key)
// .then(function(res){jsonKey = val})
//
//var storage = jIO.createJIO({type: "crypt", key: jsonKey,
// sub_storage: {...}});
// find more informations about this cryptography system on
// https://github.com/diafygi/webcrypto-examples#aes-gcm
......@@ -32,13 +39,29 @@
var MIME_TYPE = "application/x-jio-aes-gcm-encryption";
function CryptStorage(spec) {
if (!spec.key || typeof spec.key !== "object") {
throw new TypeError("'key' must be a CryptoKey object");
}
this._key = spec.key;
this._jsonKey = true;
this._sub_storage = jIO.createJIO(spec.sub_storage);
}
function convertKey(that) {
return new RSVP.Queue()
.push(function () {
return crypto.subtle.importKey("jwk", that._key,
"AES-GCM", false,
["encrypt", "decrypt"]);
})
.push(function (res) {
that._key = res;
that._jsonKey = false;
return;
}, function () {
throw new TypeError(
"'key' must be a CryptoKey to JSON Web Key format"
);
});
}
CryptStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage,
arguments);
......@@ -75,6 +98,12 @@
that = this;
return new RSVP.Queue()
.push(function () {
if (that._jsonKey === true) {
return convertKey(that);
}
return;
})
.push(function () {
return jIO.util.readBlobAsDataURL(blob);
})
......@@ -91,7 +120,7 @@
}
return crypto.subtle.encrypt({
name : "AES-GCM",
initializaton_vector : initializaton_vector
iv : initializaton_vector
},
that._key, buf);
})
......@@ -110,6 +139,12 @@
return blob;
}
return new RSVP.Queue()
.push(function () {
if (that._jsonKey === true) {
return convertKey(that);
}
return;
})
.push(function () {
return jIO.util.readBlobAsArrayBuffer(blob);
})
......@@ -120,7 +155,7 @@
initializaton_vector = new Uint8Array(coded.slice(0, 12));
return crypto.subtle.decrypt({
name : "AES-GCM",
initializaton_vector : initializaton_vector
iv : initializaton_vector
},
that._key, coded.slice(12));
})
......@@ -135,7 +170,7 @@
}
throw error;
}
});
}, function () { return blob; });
});
};
......@@ -151,4 +186,4 @@
jIO.addStorage('crypt', CryptStorage);
}(jIO, RSVP, DOMException, Blob));
}(jIO, RSVP, DOMException, Blob, crypto, Uint8Array, ArrayBuffer));
......@@ -11,25 +11,9 @@
equal = QUnit.equal,
throws = QUnit.throws,
module = QUnit.module,
key;
crypto.subtle.importKey(
"jwk",
{
kty: "oct",
k: "L6hUS9PdMP5AIxXyiFM0GOBukp0heD5wHPRctvWBcVg",
alg: "A256GCM",
ext: true
},
{
name: "AES-GCM"
},
true,
["encrypt", "decrypt"]
)
.then(function (res) {
key = res;
});
key = {"alg": "A256GCM", "ext": true,
"k": "seeaLzpu8dHG07bO2ANH2GywbTqs_zrs4Vq8zmtYeE4",
"key_ops": ["encrypt", "decrypt"], "kty": "oct"};
/////////////////////////////////////////////////////////////////
// Custom test substorage definition
......@@ -50,6 +34,7 @@
key: key,
sub_storage: {type : "cryptstorage200"}
});
equal(jio.__type, "crypt");
equal(jio.__storage._sub_storage.__type, "cryptstorage200");
});
......@@ -405,10 +390,11 @@
var id = "/",
attachment = "stringattachment",
value = "azertyuio\npàç_è-('é&",
tocheck = "data:application/x-jio-aes-gcm-encryption;base64,L3" +
"LcvzpAlxu8/xd0fW7lPHZs5AP0ncexWoTfH57PCVkvrtp1JoB" +
"wDzUYO+DHsfjAkzXkxhHHNUmxAtDiiSkRSvcbderS9FfIC7U6" +
"KoGcqiP3OkEseL9Rd7F+qBwGuuDJyg==",
tocheck = "data:application/x-jio-aes-gcm-encryption;base64" +
",+p/Ho+KgGHZC2zDLMbQQS2tXcsy0g+Ho41VZnlPEkXdmG9zm36c8iLCkv" +
"lanyWCN510NK4hj1EgWQ6WrLS5pCmA/yeAWh+HyfPkYKDRHVBl6+Hxd53I" +
"TmiWQ6Vix2jaIQg==",
blob = jIO.util.dataURItoBlob(tocheck);
......@@ -455,7 +441,17 @@
});
function decodeAES(blob) {
var decryptKey;
return new RSVP.Queue()
.push(function () {
return crypto.subtle.importKey("jwk", key,
"AES-GCM", false, ["decrypt"]);
})
.push(function (res) {
decryptKey = res;
return;
})
.push(function () {
return jIO.util.readBlobAsArrayBuffer(blob);
})
......@@ -465,7 +461,7 @@
coded = coded.currentTarget.result;
iv = new Uint8Array(coded.slice(0, 12));
return crypto.subtle.decrypt({name : "AES-GCM", iv : iv},
key, coded.slice(12));
decryptKey, coded.slice(12));
})
.push(function (arr) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment