-
Notifications
You must be signed in to change notification settings - Fork 7
/
encoding.test.ts
143 lines (118 loc) · 6.54 KB
/
encoding.test.ts
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
import { deepStrictEqual, strictEqual } from "node:assert";
import { Buffer } from "node:buffer";
import { decodeBase64, decodeHex, encodeBase64, encodeHex } from "./encoding.ts";
import bytes, { text } from "./bytes.ts";
import { dedent, random } from "./string.ts";
const chars1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()[]{}_-+/='\";:,.<>?";
const chars2 = "的一国在人了有中是年和大业不为发会工经上地市要个产这出行作生家以成到日民来我部对进多全建他公开们场展时理新方主企资实学报制政济用同于法高长现本月定化加动合品重关机分力自外者区能设后就等体下万元社过前面";
const chars = chars1 + chars2;
describe("encoding", () => {
it("encodeHex", () => {
const hex1 = encodeHex("Hello, World!");
strictEqual(hex1, "48656c6c6f2c20576f726c6421");
strictEqual(hex1, Buffer.from("Hello, World!").toString("hex"));
const hex2 = encodeHex(bytes("Hello, World!"));
strictEqual(hex2, "48656c6c6f2c20576f726c6421");
strictEqual(hex2, Buffer.from("Hello, World!").toString("hex"));
const hex3 = encodeHex(bytes("Hello, World!").buffer);
strictEqual(hex3, "48656c6c6f2c20576f726c6421");
strictEqual(hex3, Buffer.from("Hello, World!").toString("hex"));
const hex4 = encodeHex("你好,世界!");
strictEqual(hex4, "e4bda0e5a5bdefbc8ce4b896e7958cefbc81");
strictEqual(hex4, Buffer.from("你好,世界!").toString("hex"));
for (let i = 0; i < 100; i++) {
const original = random(16, chars);
const hex = encodeHex(original);
strictEqual(hex, Buffer.from(original).toString("hex"));
}
});
it("decodeHex", () => {
const data1 = decodeHex("48656c6c6f2c20576f726c6421");
deepStrictEqual(data1, new Uint8Array(bytes("Hello, World!")));
const data2 = decodeHex("e4bda0e5a5bdefbc8ce4b896e7958cefbc81");
deepStrictEqual(data2, new Uint8Array(bytes("你好,世界!")));
for (let i = 0; i < 100; i++) {
const original = random(16, chars);
const hex = encodeHex(original);
const decoded = text(decodeHex(hex));
strictEqual(decoded, original);
}
});
it("encodeBase64", () => {
const base641 = encodeBase64("Hello, World!");
strictEqual(base641, "SGVsbG8sIFdvcmxkIQ==");
strictEqual(base641, Buffer.from("Hello, World!").toString("base64"));
const base642 = encodeBase64(bytes("Hello, World!"));
strictEqual(base642, "SGVsbG8sIFdvcmxkIQ==");
strictEqual(base642, Buffer.from("Hello, World!").toString("base64"));
const base643 = encodeBase64(bytes("Hello, World!").buffer);
strictEqual(base643, "SGVsbG8sIFdvcmxkIQ==");
strictEqual(base643, Buffer.from("Hello, World!").toString("base64"));
const base644 = encodeBase64("你好,世界!");
strictEqual(base644, "5L2g5aW977yM5LiW55WM77yB");
strictEqual(base644, Buffer.from("你好,世界!").toString("base64"));
for (let i = 0; i < 100; i++) {
const original = random(16, chars);
const base64 = encodeBase64(original);
strictEqual(base64, Buffer.from(original).toString("base64"));
}
});
it("decodeBase64", () => {
const data1 = decodeBase64("SGVsbG8sIFdvcmxkIQ==");
deepStrictEqual(data1, new Uint8Array(bytes("Hello, World!")));
const data2 = decodeBase64("5L2g5aW977yM5LiW55WM77yB");
deepStrictEqual(data2, new Uint8Array(bytes("你好,世界!")));
for (let i = 0; i < 100; i++) {
const original = random(16, chars);
const base64 = encodeBase64(original);
const decoded = text(decodeBase64(base64));
strictEqual(decoded, original);
}
if (typeof atob === "function") {
let key = dedent`
-----BEGIN PRIVATE KEY-----
MIIEvgI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B32w2T0XR+28d/4R5MkY0
zo2b0ihixQKBgQCXG+pBxjCteTc6Tm09hrKlB/xP37rWl77VmHYTH8eN8IeDJgcG
m+Ir7MQhMWSrf9uNbGPNpLVCU5CsH21ACxdIFo4KQf+FwXq1ksZTTntt42ZlR8Kd
y3yPIapYTU5lWFEbXxwifNbgWolQr2enzylIUL0EN7/Qzid92aEIzY51zQKBgG1q
kybvN474q33rxpJEGOags3UNIyMgNdm1Gjy5ckW/pns/6bOO724qXAA5KYfgga8T
Nn0bmhtXlK5hOzaOlDMBnZaqZ9yQFz2BGozvfPu3dSeSLYsFKrsNDPL3zG3bI/z9
7fBz/3fNO8MJgCFt/IU5DcD9bLsfVT4Z/gsXCA8ZAoGBAMBqltm418h1J9IqiwRz
XWdlj9zwgR4ryOaATSlClkgR1Pe01ar5bKtjFSu4Mkp/7WjSD4PZ7LhEUYg2Wdj3
M1ecoIN75poN4JvzXb7yUXWl0QMg7jlVwJ1G0xaBoKbJrGqBdWMfgeKyjqtVX6vV
ETsrQcAZ6PqNg0aoJ97wJzPj
-----END PRIVATE KEY-----
`.trim();
const pemHeader = "-----BEGIN PRIVATE KEY-----";
const pemFooter = "-----END PRIVATE KEY-----";
key = key.substring(
pemHeader.length,
key.length - pemFooter.length - 1,
);
const binString = atob(key);
const size = binString.length;
const _bytes = new Uint8Array(size);
for (let i = 0; i < size; i++) {
_bytes[i] = binString.charCodeAt(i);
}
const data3 = decodeBase64(key);
deepStrictEqual(data3, _bytes);
}
});
});