This commit is contained in:
Patrick Roumanoff 2018-02-13 09:53:20 +01:00
parent 5bed911bfc
commit 436edde32c
3 changed files with 8 additions and 16 deletions

View file

@ -26,4 +26,5 @@ function base64urlDecode(s) {
return window.atob(s); // Regular base64 decoder return window.atob(s); // Regular base64 decoder
} }
module = module || {};
module.exports = { base64urlDecode, base64urlEncode }; module.exports = { base64urlDecode, base64urlEncode };

View file

@ -38,19 +38,17 @@ function generateKeyPair(alg, size, name) {
extractable, extractable,
["sign", "verify"] ["sign", "verify"]
) )
.then(function(key) { .then(key => {
var privateKey = window.crypto.subtle var privateKey = window.crypto.subtle
.exportKey("jwk", key.privateKey) .exportKey("jwk", key.privateKey)
.then(encodePrivateKey) .then(encodePrivateKey)
.then(wrap) .then(wrap)
.then(rsaPrivateKey); .then(rsaPrivateKey);
var publicKey = window.crypto.subtle.exportKey("jwk", key.publicKey).then(function(jwk) { var publicKey = window.crypto.subtle.exportKey("jwk", key.publicKey).then(jwk => encodePublicKey(jwk, name));
return encodePublicKey(jwk, name);
});
return Promise.all([privateKey, publicKey]); return Promise.all([privateKey, publicKey]);
}); });
} }
module = module || {};
module.exportKey = { arrayBufferToBase64, generateKeyPair }; module.exportKey = { arrayBufferToBase64, generateKeyPair };

View file

@ -5,9 +5,7 @@ function arrayToString(a) {
} }
function stringToArray(s) { function stringToArray(s) {
return s.split("").map(function(c) { return s.split("").map(c => c.charCodeAt());
return c.charCodeAt();
});
} }
function base64urlToArray(s) { function base64urlToArray(s) {
@ -19,13 +17,7 @@ function pemToArray(pem) {
} }
function arrayToPem(a) { function arrayToPem(a) {
return window.btoa( return window.btoa(a.map(c => String.fromCharCode(c)).join(""));
a
.map(function(c) {
return String.fromCharCode(c);
})
.join("")
);
} }
function arrayToLen(a) { function arrayToLen(a) {
@ -114,7 +106,7 @@ function asnEncodeLen(n) {
function encodePrivateKey(jwk) { function encodePrivateKey(jwk) {
var order = ["n", "e", "d", "p", "q", "dp", "dq", "qi"]; var order = ["n", "e", "d", "p", "q", "dp", "dq", "qi"];
var list = order.map(function(prop) { var list = order.map(prop => {
var v = checkHighestBit(stringToArray(base64urlDecode(jwk[prop]))); var v = checkHighestBit(stringToArray(base64urlDecode(jwk[prop])));
var len = asnEncodeLen(v.length); var len = asnEncodeLen(v.length);
return [0x02].concat(len, v); // int tag is 0x02 return [0x02].concat(len, v); // int tag is 0x02
@ -126,4 +118,5 @@ function encodePrivateKey(jwk) {
return arrayToPem(a); return arrayToPem(a);
} }
module = module || {};
module.exports = { base64urlToArray, decodePublicKey, encodePublicKey, encodePrivateKey }; module.exports = { base64urlToArray, decodePublicKey, encodePublicKey, encodePrivateKey };