zqlu 0.2.1

zqlu is a convenient text-based format for public keys
Documentation
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>zqlu Playground</title>
    <style>
      :root {
        color-scheme: light;
        --bg: #f3efe4;
        --panel: rgba(255, 252, 244, 0.92);
        --ink: #1d1b18;
        --muted: #665f52;
        --accent: #0d6b63;
        --accent-strong: #084740;
        --accent-soft: rgba(13, 107, 99, 0.14);
        --border: rgba(29, 27, 24, 0.12);
        --danger: #a12424;
        --shadow: 0 14px 32px rgba(53, 44, 25, 0.1);
      }

      * {
        box-sizing: border-box;
      }

      body {
        margin: 0;
        font-family: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Georgia, serif;
        color: var(--ink);
        background: transparent;
        padding: 0;
      }

      main {
        width: 100%;
        max-width: 36rem;
        margin: 0 auto;
      }

      .panel {
        backdrop-filter: blur(12px);
        background: var(--panel);
        border: 1px solid var(--border);
        border-radius: 12px;
        box-shadow: var(--shadow);
        padding: 10px 12px;
      }

      label {
        display: block;
        font-size: 0.7rem;
        text-transform: uppercase;
        letter-spacing: 0.08em;
        color: var(--muted);
        margin-bottom: 6px;
      }

      .label-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        margin-bottom: 6px;
      }

      .label-row label {
        margin-bottom: 0;
      }

      .copy-button {
        border: 1px solid rgba(29, 27, 24, 0.14);
        border-radius: 999px;
        background: rgba(255, 255, 255, 0.8);
        color: var(--accent-strong);
        padding: 5px 10px;
        font: 600 0.68rem/1 "SFMono-Regular", "Cascadia Code", "Fira Code", Consolas, monospace;
        letter-spacing: 0.02em;
        cursor: pointer;
        transition: border-color 160ms ease, background-color 160ms ease, transform 160ms ease;
      }

      .copy-button:hover {
        border-color: rgba(13, 107, 99, 0.4);
        background: rgba(255, 255, 255, 0.96);
        transform: translateY(-1px);
      }

      .copy-button:active {
        transform: translateY(0);
      }

      .copy-button:disabled {
        cursor: not-allowed;
        opacity: 0.55;
        transform: none;
      }

      textarea {
        width: 100%;
        height: 5.1rem;
        border: 1px solid rgba(29, 27, 24, 0.14);
        border-radius: 0;
        padding: 10px 11px;
        resize: none;
        font: 500 0.75rem/1.28 "SFMono-Regular", "Cascadia Code", "Fira Code", Consolas, monospace;
        color: var(--ink);
        background: rgba(255, 255, 255, 0.75);
        transition: border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease;
        white-space: pre;
        overflow-wrap: normal;
        overflow-x: auto;
      }

      textarea:focus {
        outline: none;
        border-color: rgba(13, 107, 99, 0.48);
        box-shadow: 0 0 0 4px var(--accent-soft);
        transform: translateY(-1px);
      }

      .output {
        height: 3.3rem;
        white-space: pre-wrap;
        overflow-wrap: anywhere;
        overflow-x: hidden;
      }

      .stack {
        display: grid;
        gap: 8px;
      }

      .intro {
        margin: 0 0 4px;
        font-size: 0.74rem;
        line-height: 1.3;
        color: var(--muted);
      }

      .status {
        margin-top: 6px;
        min-height: 1.1em;
        font-size: 0.74rem;
        color: var(--muted);
      }

      .status.error {
        color: var(--danger);
      }

      .meta {
        display: grid;
        gap: 6px;
        grid-template-columns: minmax(0, 0.22fr) minmax(0, 0.78fr);
      }

      .card {
        padding: 9px 10px;
        border-radius: 0;
        background: rgba(255, 255, 255, 0.72);
        border: 1px solid rgba(29, 27, 24, 0.08);
      }

      .card h2 {
        margin: 0 0 4px;
        font-size: 0.66rem;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: var(--muted);
      }

      .card p,
      .card code {
        margin: 0;
        font-size: 0.78rem;
      }

      #fingerprint {
        white-space: nowrap;
        overflow-wrap: normal;
      }

      .mono {
        font-family: "SFMono-Regular", "Cascadia Code", "Fira Code", Consolas, monospace;
        overflow-wrap: anywhere;
      }

      @media (max-width: 680px) {
        .meta {
          grid-template-columns: 1fr;
        }
      }
    </style>
  </head>
  <body>
    <main>
      <section class="panel stack">
        <p class="intro">Paste an OpenSSH, PEM/SPKI, or <span class="mono">zq.lu...</span> public key to convert it and show the OpenSSH-style SHA-256 fingerprint.</p>
        <div>
          <label for="source">Input Key</label>
          <textarea id="source" rows="3" cols="80" spellcheck="false" placeholder="Paste an OpenSSH key, PEM public key, or zq.lu key"></textarea>
          <div id="status" class="status">Waiting for input.</div>
        </div>

        <div>
          <div class="label-row">
            <label id="resultLabel" for="result">Converted Key</label>
            <button id="copyButton" class="copy-button" type="button">Copy</button>
          </div>
          <textarea id="result" class="output" rows="3" cols="80" spellcheck="false" readonly placeholder="The converted key appears here"></textarea>

          <div class="meta">
            <div class="card">
              <h2>Key Type</h2>
              <p id="keyType">-</p>
            </div>

            <div class="card">
              <h2>SHA-256 Fingerprint</h2>
              <p id="fingerprint" class="mono">-</p>
            </div>
          </div>
        </div>
      </section>
    </main>

    <script>
      const BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
      const ZQLU_PREFIX = "zq.lu";
      const KEY_TYPE_LABELS = {
        A: "Ed25519",
        C: "P-256",
        D: "P-256",
        E: "P-384",
        F: "P-384",
        G: "P-521",
        H: "P-521"
      };
      const CURVE_TO_TYPES = {
        nistp256: { even: "D", odd: "C", xLength: 32, label: "P-256" },
        nistp384: { even: "F", odd: "E", xLength: 48, label: "P-384" },
        nistp521: { even: "H", odd: "G", xLength: 66, label: "P-521" }
      };
      const KEY_TYPE_TO_CURVE = {
        C: "nistp256",
        D: "nistp256",
        E: "nistp384",
        F: "nistp384",
        G: "nistp521",
        H: "nistp521"
      };
      const CURVE_PARAMS = {
        nistp256: {
          p: hexToBigInt("ffffffff00000001000000000000000000000000ffffffffffffffffffffffff"),
          b: hexToBigInt("5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"),
          size: 32
        },
        nistp384: {
          p: hexToBigInt("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff"),
          b: hexToBigInt("b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef"),
          size: 48
        },
        nistp521: {
          p: hexToBigInt("01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
          b: hexToBigInt("0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00"),
          size: 66
        }
      };
      const OID_ED25519 = "1.3.101.112";
      const OID_EC_PUBLIC_KEY = "1.2.840.10045.2.1";
      const OID_P256 = "1.2.840.10045.3.1.7";
      const OID_P384 = "1.3.132.0.34";
      const OID_P521 = "1.3.132.0.35";

      const source = document.getElementById("source");
      const result = document.getElementById("result");
      const status = document.getElementById("status");
      const keyTypeNode = document.getElementById("keyType");
      const fingerprintNode = document.getElementById("fingerprint");
      const resultLabelNode = document.getElementById("resultLabel");
      const copyButton = document.getElementById("copyButton");

      source.addEventListener("input", update);
      copyButton.addEventListener("click", copyResult);
      update();

      async function update() {
        const raw = source.value;
        if (!raw.trim()) {
          result.value = "";
          resultLabelNode.textContent = "Converted Key";
          copyButton.disabled = true;
          keyTypeNode.textContent = "-";
          fingerprintNode.textContent = "-";
          setStatus("Waiting for input.");
          return;
        }

        try {
          const parsed = parseAny(raw);
          if (parsed.format === "zqlu") {
            result.value = encodeOpenSsh(parsed);
            resultLabelNode.textContent = "OpenSSH Key";
          } else {
            result.value = encodeZqlu(parsed.keyType, parsed.payload);
            resultLabelNode.textContent = "zqlu Key";
          }
          copyButton.disabled = false;
          keyTypeNode.textContent = KEY_TYPE_LABELS[parsed.keyType] || parsed.keyType;
          fingerprintNode.textContent = await computeFingerprint(parsed);
          setStatus("Key parsed successfully.");
        } catch (error) {
          result.value = "";
          resultLabelNode.textContent = "Converted Key";
          copyButton.disabled = true;
          keyTypeNode.textContent = "-";
          fingerprintNode.textContent = "-";
          setStatus(error instanceof Error ? error.message : String(error), true);
        }
      }

      async function copyResult() {
        if (!result.value) {
          return;
        }

        try {
          await navigator.clipboard.writeText(result.value);
          setStatus("Converted key copied to clipboard.");
        } catch {
          setStatus("Could not copy to clipboard in this browser.", true);
        }
      }

      function setStatus(message, isError = false) {
        status.textContent = message;
        status.className = isError ? "status error" : "status";
      }

      function parseAny(input) {
        const trimmed = input.trim();
        if (trimmed.startsWith(ZQLU_PREFIX)) {
          return parseZqlu(trimmed);
        }
        if (trimmed.startsWith("-----BEGIN PUBLIC KEY-----")) {
          return parsePem(trimmed);
        }
        return parseOpenSsh(trimmed);
      }

      function parseZqlu(input) {
        if (!input.startsWith(ZQLU_PREFIX)) {
          throw new Error("Input must start with 'zq.lu'.");
        }
        if (input.length < 7) {
          throw new Error("zqlu input is too short.");
        }

        const keyType = input[5];
        if (!(keyType in KEY_TYPE_LABELS)) {
          throw new Error("Unsupported zqlu key type.");
        }

        const encoded = input.slice(6).replace(/\s+/g, "");
        if (!encoded) {
          throw new Error("zqlu key is missing payload bytes.");
        }
        if (!/^[0-9A-Za-z]+$/.test(encoded)) {
          throw new Error("zqlu payload contains invalid characters.");
        }

        const keyAndChecksum = decodeBase62(encoded);
        if (keyAndChecksum.length < 3) {
          throw new Error("zqlu payload is too short.");
        }

        const payload = keyAndChecksum.slice(0, -2);
        const expected = crc16IbmSdlc(bytes(ZQLU_PREFIX), bytes(keyType), payload);
        const actual = (keyAndChecksum[keyAndChecksum.length - 2] << 8) | keyAndChecksum[keyAndChecksum.length - 1];
        if (expected !== actual) {
          throw new Error("CRC check failed for zqlu input.");
        }

        return { format: "zqlu", keyType, payload };
      }

      function parseOpenSsh(input) {
        const parts = input.split(/\s+/).filter(Boolean);
        if (parts.length < 2) {
          throw new Error("Expected an OpenSSH public key.");
        }

        const algorithm = parts[0];
        const decoded = decodeBase64(parts[1]);
        const reader = createReader(decoded);
        const wireAlgorithm = decodeUtf8(reader.readString());
        if (wireAlgorithm !== algorithm) {
          throw new Error("OpenSSH algorithm prefix does not match key payload.");
        }

        if (algorithm === "ssh-ed25519") {
          const key = reader.readString();
          if (key.length !== 32) {
            throw new Error("Ed25519 OpenSSH key must be 32 bytes.");
          }
          reader.assertDone();
          return { format: "OpenSSH", keyType: "A", payload: key };
        }

        if (algorithm.startsWith("ecdsa-sha2-")) {
          const curveName = decodeUtf8(reader.readString());
          const point = reader.readString();
          reader.assertDone();
          return {
            format: "OpenSSH",
            ...parseEcPoint(curveName, point)
          };
        }

        throw new Error("Unsupported OpenSSH key type.");
      }

      function parsePem(input) {
        const match = input.match(/-----BEGIN PUBLIC KEY-----([\s\S]*?)-----END PUBLIC KEY-----/);
        if (!match) {
          throw new Error("Expected a PEM public key.");
        }

        const der = decodeBase64(match[1].replace(/\s+/g, ""));
        const spki = parseSubjectPublicKeyInfo(der);

        if (spki.algorithmOid === OID_ED25519) {
          if (spki.algorithmParameters !== null) {
            throw new Error("Ed25519 PEM keys must not include algorithm parameters.");
          }
          if (spki.subjectPublicKey.length !== 32) {
            throw new Error("Ed25519 PEM key must contain a 32-byte public key.");
          }
          return { format: "PEM/SPKI", keyType: "A", payload: spki.subjectPublicKey };
        }

        if (spki.algorithmOid === OID_EC_PUBLIC_KEY) {
          if (spki.algorithmParameters === null) {
            throw new Error("EC PEM key is missing its curve OID.");
          }
          const curveName = curveNameFromOid(spki.algorithmParameters);
          return {
            format: "PEM/SPKI",
            ...parseEcPoint(curveName, spki.subjectPublicKey)
          };
        }

        throw new Error("Unsupported PEM public key type.");
      }

      function parseEcPoint(curveName, point) {
        const curve = CURVE_TO_TYPES[curveName];
        if (!curve) {
          throw new Error("Unsupported EC curve.");
        }
        const expectedLength = 1 + curve.xLength * 2;
        if (point.length !== expectedLength) {
          throw new Error(`Unexpected ${curve.label} point length.`);
        }
        if (point[0] !== 0x04) {
          throw new Error("Only uncompressed EC public keys are supported.");
        }

        const x = point.slice(1, 1 + curve.xLength);
        const yLastByte = point[point.length - 1];
        return {
          keyType: yLastByte % 2 === 0 ? curve.even : curve.odd,
          payload: x
        };
      }

      function encodeZqlu(keyType, payload) {
        const checksum = crc16IbmSdlc(bytes(ZQLU_PREFIX), bytes(keyType), payload);
        const combined = new Uint8Array(payload.length + 2);
        combined.set(payload, 0);
        combined[payload.length] = checksum >> 8;
        combined[payload.length + 1] = checksum & 0xff;
        return `${ZQLU_PREFIX}${keyType}${encodeBase62(combined)}`;
      }

      async function computeFingerprint(parsed) {
        const keyBlob = buildOpenSshBlob(parsed);
        const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", keyBlob));
        return `SHA256:${base64Encode(digest).replace(/=+$/g, "")}`;
      }

      function buildOpenSshBlob(parsed) {
        if (parsed.keyType === "A") {
          return concatBytes(
            sshString(bytes("ssh-ed25519")),
            sshString(parsed.payload)
          );
        }

        const curveName = KEY_TYPE_TO_CURVE[parsed.keyType];
        if (!curveName) {
          throw new Error("Unsupported key type for fingerprint generation.");
        }
        const algorithm = `ecdsa-sha2-${curveName}`;
        const point = encodeEcPoint(parsed.keyType, parsed.payload);
        return concatBytes(
          sshString(bytes(algorithm)),
          sshString(bytes(curveName)),
          sshString(point)
        );
      }

      function encodeOpenSsh(parsed) {
        const keyBlob = buildOpenSshBlob(parsed);
        const algorithm = parsed.keyType === "A"
          ? "ssh-ed25519"
          : `ecdsa-sha2-${KEY_TYPE_TO_CURVE[parsed.keyType]}`;
        return `${algorithm} ${base64Encode(keyBlob)}`;
      }

      function encodeEcPoint(keyType, xBytes) {
        const curveName = KEY_TYPE_TO_CURVE[keyType];
        const params = CURVE_PARAMS[curveName];
        const x = bytesToBigInt(xBytes);
        const y = recoverY(curveName, x, keyType === "D" || keyType === "F" || keyType === "H");
        return concatBytes(Uint8Array.of(0x04), xBytes, bigIntToBytes(y, params.size));
      }

      function recoverY(curveName, x, wantEven) {
        const params = CURVE_PARAMS[curveName];
        const { p, b } = params;
        const rhs = mod(modPow(x, 3n, p) - mod(3n * x, p) + b, p);
        let y = modPow(rhs, (p + 1n) / 4n, p);
        if (modPow(y, 2n, p) !== rhs) {
          throw new Error("Failed to reconstruct EC point from zqlu key.");
        }
        const isEven = (y & 1n) === 0n;
        if (isEven !== wantEven) {
          y = mod(-y, p);
        }
        return y;
      }

      function mod(value, modulus) {
        const result = value % modulus;
        return result >= 0n ? result : result + modulus;
      }

      function modPow(base, exponent, modulus) {
        let result = 1n;
        let factor = mod(base, modulus);
        let power = exponent;
        while (power > 0n) {
          if (power & 1n) {
            result = mod(result * factor, modulus);
          }
          factor = mod(factor * factor, modulus);
          power >>= 1n;
        }
        return result;
      }

      function bytesToBigInt(data) {
        let result = 0n;
        for (const byte of data) {
          result = (result << 8n) | BigInt(byte);
        }
        return result;
      }

      function bigIntToBytes(value, size) {
        const out = new Uint8Array(size);
        let current = value;
        for (let i = size - 1; i >= 0; i -= 1) {
          out[i] = Number(current & 0xffn);
          current >>= 8n;
        }
        return out;
      }

      function hexToBigInt(hex) {
        return BigInt(`0x${hex}`);
      }

      function sshString(data) {
        const out = new Uint8Array(4 + data.length);
        const view = new DataView(out.buffer);
        view.setUint32(0, data.length);
        out.set(data, 4);
        return out;
      }

      function concatBytes(...chunks) {
        const length = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
        const out = new Uint8Array(length);
        let offset = 0;
        for (const chunk of chunks) {
          out.set(chunk, offset);
          offset += chunk.length;
        }
        return out;
      }

      function encodeBase62(data) {
        let leadingZeroes = 0;
        while (leadingZeroes < data.length && data[leadingZeroes] === 0) {
          leadingZeroes += 1;
        }

        const encoded = rawBase62Encode(data);
        if (leadingZeroes === 0) {
          return encoded;
        }
        return "0".repeat(leadingZeroes) + encoded.slice(leadingZeroes);
      }

      function rawBase62Encode(data) {
        let zeros = 0;
        while (zeros < data.length && data[zeros] === 0) {
          zeros += 1;
        }

        let num = Array.from(data);
        let encoded = "";

        while (!num.every((value) => value === 0)) {
          const { quotient, remainder } = divmod62(num);
          encoded += BASE62_ALPHABET[remainder];
          num = quotient;
        }

        for (let i = 0; i < zeros; i += 1) {
          encoded += "1";
        }

        return encoded.split("").reverse().join("");
      }

      function divmod62(num) {
        const quotient = [];
        let remainder = 0;
        for (const digit of num) {
          const value = remainder * 256 + digit;
          remainder = value % 62;
          quotient.push(Math.floor(value / 62));
        }
        while (quotient.length > 1 && quotient[0] === 0) {
          quotient.shift();
        }
        return { quotient, remainder };
      }

      function decodeBase62(input) {
        let num = [0];
        for (const char of input) {
          const value = BASE62_ALPHABET.indexOf(char);
          if (value === -1) {
            continue;
          }

          let carry = value;
          for (let i = 0; i < num.length; i += 1) {
            const total = num[i] * 62 + carry;
            num[i] = total & 0xff;
            carry = total >> 8;
          }

          while (carry > 0) {
            num.push(carry & 0xff);
            carry >>= 8;
          }
        }

        let zeroes = 0;
        while (zeroes < input.length && input[zeroes] === "0") {
          zeroes += 1;
        }

        const result = new Uint8Array(zeroes + num.length);
        for (let i = 0; i < num.length; i += 1) {
          result[zeroes + i] = num[num.length - 1 - i];
        }
        return result;
      }

      function crc16IbmSdlc(...chunks) {
        let crc = 0xffff;
        for (const chunk of chunks) {
          for (const byte of chunk) {
            crc ^= reflect8(byte) << 8;
            for (let i = 0; i < 8; i += 1) {
              if (crc & 0x8000) {
                crc = ((crc << 1) ^ 0x1021) & 0xffff;
              } else {
                crc = (crc << 1) & 0xffff;
              }
            }
          }
        }
        return reflect16(crc ^ 0xffff);
      }

      function reflect8(value) {
        let reflected = 0;
        for (let i = 0; i < 8; i += 1) {
          reflected = (reflected << 1) | ((value >> i) & 1);
        }
        return reflected;
      }

      function reflect16(value) {
        let reflected = 0;
        for (let i = 0; i < 16; i += 1) {
          reflected = (reflected << 1) | ((value >> i) & 1);
        }
        return reflected;
      }

      function parseSubjectPublicKeyInfo(der) {
        const reader = createAsn1Reader(der);
        const outer = reader.readElement(0x30);
        reader.assertDone();

        const outerReader = createAsn1Reader(outer.value);
        const algorithmSequence = outerReader.readElement(0x30);
        const bitString = outerReader.readElement(0x03);
        outerReader.assertDone();

        const algorithmReader = createAsn1Reader(algorithmSequence.value);
        const algorithmOid = readOid(algorithmReader.readElement(0x06).value);
        let algorithmParameters = null;
        if (!algorithmReader.done()) {
          const parameterElement = algorithmReader.readAny();
          if (parameterElement.tag === 0x06) {
            algorithmParameters = readOid(parameterElement.value);
          } else if (parameterElement.tag === 0x05 && parameterElement.value.length === 0) {
            algorithmParameters = null;
          } else {
            throw new Error("Unsupported PEM algorithm parameters.");
          }
        }
        algorithmReader.assertDone();

        if (bitString.value.length === 0 || bitString.value[0] !== 0x00) {
          throw new Error("PEM public key contains an invalid BIT STRING.");
        }

        return {
          algorithmOid,
          algorithmParameters,
          subjectPublicKey: bitString.value.slice(1)
        };
      }

      function curveNameFromOid(oid) {
        switch (oid) {
          case OID_P256:
            return "nistp256";
          case OID_P384:
            return "nistp384";
          case OID_P521:
            return "nistp521";
          default:
            throw new Error("Unsupported EC curve OID.");
        }
      }

      function readOid(bytesValue) {
        if (bytesValue.length === 0) {
          throw new Error("OID is empty.");
        }
        const values = [];
        const first = bytesValue[0];
        values.push(Math.floor(first / 40));
        values.push(first % 40);

        let current = 0;
        for (let i = 1; i < bytesValue.length; i += 1) {
          current = (current << 7) | (bytesValue[i] & 0x7f);
          if ((bytesValue[i] & 0x80) === 0) {
            values.push(current);
            current = 0;
          }
        }
        if ((bytesValue[bytesValue.length - 1] & 0x80) !== 0) {
          throw new Error("OID terminated unexpectedly.");
        }

        return values.join(".");
      }

      function createReader(data) {
        let offset = 0;
        return {
          readString() {
            const length = this.readUint32();
            if (offset + length > data.length) {
              throw new Error("OpenSSH key ended unexpectedly.");
            }
            const value = data.slice(offset, offset + length);
            offset += length;
            return value;
          },
          readUint32() {
            if (offset + 4 > data.length) {
              throw new Error("OpenSSH key ended unexpectedly.");
            }
            const value =
              (data[offset] << 24) |
              (data[offset + 1] << 16) |
              (data[offset + 2] << 8) |
              data[offset + 3];
            offset += 4;
            return value >>> 0;
          },
          assertDone() {
            if (offset !== data.length) {
              throw new Error("OpenSSH key has trailing data.");
            }
          }
        };
      }

      function createAsn1Reader(data) {
        let offset = 0;
        return {
          readAny() {
            if (offset >= data.length) {
              throw new Error("ASN.1 data ended unexpectedly.");
            }
            const tag = data[offset++];
            const length = readAsn1Length(data, () => offset++, offset - 1);
            offset = length.nextOffset;
            const end = offset + length.length;
            if (end > data.length) {
              throw new Error("ASN.1 element length exceeds input.");
            }
            const value = data.slice(offset, end);
            offset = end;
            return { tag, value };
          },
          readElement(expectedTag) {
            const element = this.readAny();
            if (element.tag !== expectedTag) {
              throw new Error("Unexpected ASN.1 structure.");
            }
            return element;
          },
          done() {
            return offset === data.length;
          },
          assertDone() {
            if (offset !== data.length) {
              throw new Error("ASN.1 data has trailing bytes.");
            }
          }
        };
      }

      function readAsn1Length(data, advance, offsetAfterTag) {
        const first = data[offsetAfterTag + 1];
        if (first === undefined) {
          throw new Error("ASN.1 length missing.");
        }

        if ((first & 0x80) === 0) {
          return { length: first, nextOffset: offsetAfterTag + 2 };
        }

        const byteCount = first & 0x7f;
        if (byteCount === 0 || byteCount > 4) {
          throw new Error("Unsupported ASN.1 length encoding.");
        }

        let length = 0;
        let index = offsetAfterTag + 2;
        for (let i = 0; i < byteCount; i += 1) {
          if (index >= data.length) {
            throw new Error("ASN.1 length ended unexpectedly.");
          }
          length = (length << 8) | data[index];
          index += 1;
        }
        return { length, nextOffset: index };
      }

      function decodeBase64(input) {
        try {
          return Uint8Array.from(atob(input), (char) => char.charCodeAt(0));
        } catch {
          throw new Error("Invalid base64 payload.");
        }
      }

      function decodeUtf8(input) {
        return new TextDecoder().decode(input);
      }

      function base64Encode(input) {
        let binary = "";
        for (const byte of input) {
          binary += String.fromCharCode(byte);
        }
        return btoa(binary);
      }

      function bytes(value) {
        return new TextEncoder().encode(value);
      }
    </script>
  </body>
</html>