js-keygen/js-keygen-ui.js

54 lines
2 KiB
JavaScript
Raw Normal View History

2018-02-13 08:45:06 +00:00
var generateKeyPair;
2015-09-04 07:13:08 +00:00
2015-09-03 12:54:43 +00:00
function copy(id) {
2018-02-13 08:30:29 +00:00
return function() {
2015-09-03 12:54:43 +00:00
var ta = document.querySelector(id);
ta.focus();
ta.select();
try {
2018-02-13 08:30:29 +00:00
var successful = document.execCommand("copy");
var msg = successful ? "successful" : "unsuccessful";
console.log("Copy key command was " + msg);
2015-09-04 07:13:08 +00:00
} catch (err) {
2018-02-13 08:30:29 +00:00
console.log("Oops, unable to copy");
2015-09-03 12:54:43 +00:00
}
window.getSelection().removeAllRanges();
ta.blur();
2015-09-04 07:13:08 +00:00
};
2015-09-03 12:54:43 +00:00
}
function buildHref(data) {
return "data:application/octet-stream;charset=utf-8;base64," + window.btoa(data);
}
2018-02-13 08:45:06 +00:00
document.addEventListener("DOMContentLoaded", function() {
document.querySelector("#savePrivate").addEventListener("click", function() {
2018-02-13 08:30:29 +00:00
document.querySelector("a#private").click();
2015-09-03 12:54:43 +00:00
});
2018-02-13 08:30:29 +00:00
document.querySelector("#copyPrivate").addEventListener("click", copy("#privateKey"));
2018-02-13 08:45:06 +00:00
document.querySelector("#savePublic").addEventListener("click", function() {
2018-02-13 08:30:29 +00:00
document.querySelector("a#public").click();
2015-09-03 12:54:43 +00:00
});
2018-02-13 08:30:29 +00:00
document.querySelector("#copyPublic").addEventListener("click", copy("#publicKey"));
2018-02-13 08:45:06 +00:00
document.querySelector("#generate").addEventListener("click", function() {
2018-02-13 08:30:29 +00:00
var name = document.querySelector("#name").value || "name";
document.querySelector("a#private").setAttribute("download", name + "_rsa");
document.querySelector("a#public").setAttribute("download", name + "_rsa.pub");
var alg = document.querySelector("#alg").value || "RSASSA-PKCS1-v1_5";
var size = parseInt(document.querySelector("#size").value || "2048", 10);
generateKeyPair(alg, size, name)
.then(function(keys) {
document.querySelector("#private").setAttribute("href", buildHref(keys[0]));
document.querySelector("#public").setAttribute("href", buildHref(keys[1]));
document.querySelector("#privateKey").textContent = keys[0];
document.querySelector("#publicKey").textContent = keys[1];
document.querySelector("#result").style.display = "block";
})
.catch(function(err) {
console.error(err);
});
2015-09-03 12:54:43 +00:00
});
2015-09-04 07:13:08 +00:00
});