· 6 years ago · Dec 16, 2019, 06:58 PM
1// Can be executed e.g. in: https://playcode.io/
2
3function keys(){
4 var cryptoObj = window.crypto || window.msCrypto;
5 let msg = '///';
6 if(!cryptoObj){
7 alert("Crypto API is not supported by the Browser");
8 return;
9 }
10
11 window.crypto.subtle.generateKey(
12 {
13 name: "RSA-PSS",
14 modulusLength: 2048, //can be 1024, 2048, or 4096
15 publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
16 hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
17 },
18 true, //whether the key is extractable (i.e. can be used in exportKey)
19 ["sign", "verify"] //can be any combination of "sign" and "verify"
20 )
21 .then(function(key) {
22 publicKey = key.publicKey;
23 privateKey = key.privateKey;
24 // For Demo Purpos Only Exported in JWK format
25 if (/*document.getElementById('public').value == ""*/true) { // Change 1
26 window.crypto.subtle.exportKey("spki", key.publicKey)
27 .then(function (keydata) {
28 publicKeyhold = keydata;
29 let exported = publicKeyhold;
30 const exportedAsString = ab2str(exported);
31 const exportedAsBase64 = window.btoa(exportedAsString);
32 const pemExported = `-----BEGIN PUBLIC KEY-----\n${exportedAsBase64}\n-----END PUBLIC KEY-----`;
33 //document.getElementById('public').value = pemExported;
34 console.log(pemExported); // Change 2
35 });
36 }
37
38 if (/*document.getElementById('private').value == ""*/true) { // Change 4
39 //msg = document.getElementById('msg').value;
40 msg = "The quick brown fox jumps over the lazy dog"; // Change 5
41 window.crypto.subtle.exportKey("pkcs8", key.privateKey)
42 .then(function (keydata) {
43 privateKeyhold = keydata;
44 const priv = privateKeyhold;
45 const privExportedAsString = ab2str(priv);
46 const privExportedAsBase64 = window.btoa(privExportedAsString);
47 const privPemExported = `-----BEGIN RSA PRIVATE KEY-----\n${privExportedAsBase64}\n-----END RSA PRIVATE KEY-----`;
48 //document.getElementById('privJSON').data = key.privateKey; // Change 6
49 //document.getElementById('private').value = privPemExported; // Change 7
50 console.log(key.privateKey);
51 console.log(privPemExported);
52 });
53 }
54
55 window.crypto.subtle.sign(
56 {
57 name: "RSA-PSS",
58 saltLength: 128, //the length of the salt
59 },
60 //from generateKey or importKey above
61 //document.getElementById('privJSON').data, // Change 8
62 privateKey,
63 //getMessageEncoding() //ArrayBuffer of data you want to sign
64 getMessageEncoding(msg) //ArrayBuffer of data you want to sign // Change 9
65 )
66 .then(function(signature) {
67 //returns an ArrayBuffer containing the signature
68 //console.dir(ab2str(signature)); // Change 10
69 // document.getElementById("cryptmsg").value = window.btoa(ab2str(signature)); // Change 11
70 console.log(window.btoa(ab2str(signature)));
71 });
72 });
73}
74
75function ab2str(buf) {
76 return String.fromCharCode.apply(null, new Uint8Array(buf));
77}
78
79function asciiToUint8Array(str) {
80 var chars = [];
81 for (var i = 0; i < str.length; ++i)
82 chars.push(str.charCodeAt(i));
83 return new Uint8Array(chars);
84}
85
86function bytesToHexString(bytes) {
87 if (!bytes)
88 return null;
89
90 bytes = new Uint8Array(bytes);
91 var hexBytes = [];
92
93 for (var i = 0; i < bytes.length; ++i) {
94 var byteString = bytes[i].toString(16);
95 if (byteString.length < 2)
96 byteString = "0" + byteString;
97 hexBytes.push(byteString);
98 }
99
100 return hexBytes.join("");
101}
102
103function getMessageEncoding(message) { // Change 12
104 //const messageBox = document.getElementById('msg');
105 //let message = messageBox.value;
106 let enc = new TextEncoder();
107 return enc.encode(message);
108}
109
110keys();