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
54
55
56
57
58
59
60
|
<!DOCTYPE html>
<h1>private</h1>
<canvas id=privkey></canvas>
<div id=privd></div>
<h1>public</h1>
<canvas id=pubkey></canvas>
<div id=pubsec1></div>
<script src=QR-Code-generator/typescript-javascript/qrcodegen.js></script>
<script>
function draw_canvas (qr, scale, border, light, dark, canvas) {
canvas.width = canvas.height = (qr.size + border * 2) * scale;
let ctx = canvas.getContext("2d");
for (let y = -border; y < qr.size + border; y++) {
for (let x = -border; x < qr.size + border; x++) {
ctx.fillStyle = qr.getModule(x, y) ? dark : light;
ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale);
}
}
}
function a2hex (a) {
let r = "";
const hexchars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
for (let i = 0; i < a.length; i++) {
r += hexchars[a[i] >> 4];
r += hexchars[a[i] % 16];
}
return r;
}
async function main () {
while (!pubsec1.innerText.startsWith("0200") && !pubsec1.innerText.startsWith("0300")) {
window.keyobj = await crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-384",
},
true,
["sign", "verify"]
);
let jwk = await crypto.subtle.exportKey("jwk", keyobj.privateKey);
let d = jwk.d.replace(/-/g, "+").replace(/_/g, "/");
let x = (await crypto.subtle.exportKey("jwk", keyobj.publicKey)).x.replace(/-/g, "+").replace(/_/g, "/");
let y = (await crypto.subtle.exportKey("jwk", keyobj.publicKey)).y.replace(/-/g, "+").replace(/_/g, "/");
d = Uint8Array.from(atob(d), c => c.charCodeAt(0));
privd.innerText = a2hex(d) + "(big endian d) jwk: " + JSON.stringify(jwk);
draw_canvas(qrcodegen.QrCode.encodeText(JSON.stringify(jwk), qrcodegen.QrCode.Ecc.LOW), 5, 4, "#FFF", "#000", privkey);
x = Uint8Array.from(atob(x), c => c.charCodeAt(0));
y = Uint8Array.from(atob(y), c => c.charCodeAt(0));
var sec1 = new Uint8Array(49);
if (y[47] % 2)
sec1[0] = 3;
else
sec1[0] = 2;
for (let i = 0; i < 48; i++)
sec1[1+i] = x[i];
pubsec1.innerText = a2hex(sec1) + " (sec-1)";
draw_canvas(qrcodegen.QrCode.encodeBinary(sec1, qrcodegen.QrCode.Ecc.LOW), 10, 4, "#FFF", "#000", pubkey);
}
}
main();
</script>
|