bluecrypt-keypairs.js/app.js

86 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-04-18 06:20:51 +00:00
(function () {
2019-04-27 04:09:57 +00:00
'use strict';
2019-04-18 06:20:51 +00:00
2019-04-27 04:09:57 +00:00
var Keypairs = window.Keypairs;
2019-04-18 06:20:51 +00:00
2019-04-27 04:09:57 +00:00
function $(sel) {
return document.querySelector(sel);
}
function $$(sel) {
return Array.prototype.slice.call(document.querySelectorAll(sel));
}
2019-04-18 06:20:51 +00:00
2019-04-27 04:09:57 +00:00
function run() {
console.log('hello');
2019-04-18 06:20:51 +00:00
2019-04-27 04:09:57 +00:00
// Show different options for ECDSA vs RSA
$$('input[name="kty"]').forEach(function ($el) {
$el.addEventListener('change', function (ev) {
console.log(this);
console.log(ev);
if ("RSA" === ev.target.value) {
$('.js-rsa-opts').hidden = false;
$('.js-ec-opts').hidden = true;
} else {
$('.js-rsa-opts').hidden = true;
$('.js-ec-opts').hidden = false;
}
});
2019-04-18 06:20:51 +00:00
});
2019-04-27 04:09:57 +00:00
// Generate a key on submit
$('form.js-keygen').addEventListener('submit', function (ev) {
ev.preventDefault();
ev.stopPropagation();
$('.js-loading').hidden = false;
$('.js-jwk').hidden = true;
$('.js-toc-der').hidden = true;
$('.js-toc-pem').hidden = true;
$$('input').map(function ($el) { $el.disabled = true; });
$$('button').map(function ($el) { $el.disabled = true; });
var opts = {
kty: $('input[name="kty"]:checked').value
, namedCurve: $('input[name="ec-crv"]:checked').value
, modulusLength: $('input[name="rsa-len"]:checked').value
};
console.log('opts', opts);
Keypairs.generate(opts).then(function (results) {
2019-04-27 04:31:16 +00:00
var der;
2019-04-27 04:09:57 +00:00
if (opts.kty == 'EC') {
2019-04-27 04:31:16 +00:00
der = x509.packPkcs8(results.private);
2019-04-27 04:09:57 +00:00
var pem = Eckles.export({ jwk: results.private })
$('.js-input-pem').innerText = pem;
$('.js-toc-pem').hidden = false;
2019-04-27 04:31:16 +00:00
} else {
der = x509.packPkcs8(results.private);
var pem = Rasha.pack({ jwk: results.private }).then(function (pem) {
$('.js-input-pem').innerText = pem;
$('.js-toc-pem').hidden = false;
})
2019-04-27 04:09:57 +00:00
}
2019-04-27 04:31:16 +00:00
$('.js-der').innerText = JSON.stringify(der, null, 2);
$('.js-toc-der').hidden = false;
2019-04-27 04:09:57 +00:00
$('.js-jwk').innerText = JSON.stringify(results, null, 2);
$('.js-loading').hidden = true;
$('.js-jwk').hidden = false;
$$('input').map(function ($el) { $el.disabled = false; });
$$('button').map(function ($el) { $el.disabled = false; });
$('.js-toc-jwk').hidden = false;
});
2019-04-18 06:20:51 +00:00
});
2019-04-27 04:09:57 +00:00
$('form.js-acme-account').addEventListener('submit', function (ev) {
ev.preventDefault();
ev.stopPropagation();
$('.js-loading').hidden = false;
ACME.accounts.create
});
2019-04-26 22:26:33 +00:00
2019-04-27 04:09:57 +00:00
$('.js-generate').hidden = false;
$('.js-create-account').hidden = false;
}
2019-04-18 06:20:51 +00:00
2019-04-27 04:09:57 +00:00
window.addEventListener('load', run);
2019-04-18 06:20:51 +00:00
}());