-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjs-keygen-ui.js
53 lines (48 loc) · 2 KB
/
js-keygen-ui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var generateKeyPair;
function copy(id) {
return function() {
var ta = document.querySelector(id);
ta.focus();
ta.select();
try {
var successful = document.execCommand("copy");
var msg = successful ? "successful" : "unsuccessful";
console.log("Copy key command was " + msg);
} catch (err) {
console.log("Oops, unable to copy");
}
window.getSelection().removeAllRanges();
ta.blur();
};
}
function buildHref(data) {
return "data:application/octet-stream;charset=utf-8;base64," + window.btoa(data);
}
document.addEventListener("DOMContentLoaded", function() {
document.querySelector("#savePrivate").addEventListener("click", function() {
document.querySelector("a#private").click();
});
document.querySelector("#copyPrivate").addEventListener("click", copy("#privateKey"));
document.querySelector("#savePublic").addEventListener("click", function() {
document.querySelector("a#public").click();
});
document.querySelector("#copyPublic").addEventListener("click", copy("#publicKey"));
document.querySelector("#generate").addEventListener("click", function() {
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);
});
});
});