-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhash.ts
235 lines (222 loc) · 7.07 KB
/
hash.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
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
/**
* Simplified hash functions for various data types, based on the Web Crypto API
* and `crypto` package in Node.js.
* @module
*/
import bytes from "./bytes.ts";
import { isDeno, isNodeLike } from "./env.ts";
import { toBytesAsync } from "./hash/util.ts";
import hash, {
type DataSource,
adler32,
crc32,
sha1 as _sha1,
sha256 as _sha256,
sha512 as _sha512,
hmac as _hmac,
} from "./hash/web.ts";
export type { DataSource };
export default hash;
export { adler32, crc32 };
async function nodeHash(
algorithm: "sha1" | "sha256" | "sha512" | "md5",
data: DataSource,
encoding: "hex" | "base64" | undefined = undefined
): Promise<ArrayBuffer | string> {
const crypto = await import("node:crypto");
const bytes = await toBytesAsync(data);
const hash = crypto.createHash(algorithm);
hash.update(bytes);
if (encoding) {
return hash.digest(encoding);
} else {
const result = hash.digest();
// Truncate the buffer to the actual byte length so it's consistent with the web API.
return result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength);
}
}
/**
* Calculates the SHA-1 hash of the given data.
*
* @example
* ```ts
* import { sha1 } from "@ayonli/jsext/hash";
*
* const buffer = await sha1("Hello, World!");
* console.log(buffer); // ArrayBuffer(20) { ... }
* ```
*/
export function sha1(data: DataSource): Promise<ArrayBuffer>;
/**
* @example
* ```ts
* import { sha1 } from "@ayonli/jsext/hash";
*
* const hex = await sha1("Hello, World!", "hex");
* console.log(hex); // 0a0a9f2a6772942557ab5355d76af442f8f65e01
*
* const base64 = await sha1("Hello, World!", "base64");
* console.log(base64); // CgqfKmdylCVXq1NV12r0Qvj2XgE=
* ```
*/
export function sha1(data: DataSource, encoding: "hex" | "base64"): Promise<string>;
export async function sha1(
data: DataSource,
encoding: "hex" | "base64" | undefined = undefined
): Promise<string | ArrayBuffer> {
if (typeof crypto === "object" && typeof crypto.subtle === "object") {
return encoding ? _sha1(data, encoding) : _sha1(data);
} else if (isDeno || isNodeLike) {
return nodeHash("sha1", data, encoding);
} else {
throw new Error("Unsupported runtime");
}
}
/**
* Calculates the SHA-256 hash of the given data.
*
* @example
* ```ts
* import { sha256 } from "@ayonli/jsext/hash";
*
* const buffer = await sha256("Hello, World!");
* console.log(buffer); // ArrayBuffer(32) { ... }
* ```
*/
export async function sha256(data: DataSource): Promise<ArrayBuffer>;
/**
* @example
* ```ts
* import { sha256 } from "@ayonli/jsext/hash";
*
* const hex = await sha256("Hello, World!", "hex");
* console.log(hex); // dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
*
* const base64 = await sha256("Hello, World!", "base64");
* console.log(base64); // 3/1gIbsr1bCvZ2KQgJ7DpTGR3YHH9wpLKGiKNiGCmG8=
* ```
*/
export async function sha256(data: DataSource, encoding: "hex" | "base64"): Promise<string>;
export async function sha256(
data: DataSource,
encoding: "hex" | "base64" | undefined = undefined
): Promise<string | ArrayBuffer> {
if (typeof crypto === "object" && typeof crypto.subtle === "object") {
return encoding ? _sha256(data, encoding) : _sha256(data);
} else if (isDeno || isNodeLike) {
return nodeHash("sha256", data, encoding);
} else {
throw new Error("Unsupported runtime");
}
}
/**
* Calculates the SHA-512 hash of the given data.
*
* @example
* ```ts
* import { sha512 } from "@ayonli/jsext/hash";
*
* const buffer = await sha512("Hello, World!");
* console.log(buffer); // ArrayBuffer(64) { ... }
* ```
*/
export async function sha512(data: DataSource): Promise<ArrayBuffer>;
/**
* @example
* ```ts
* import { sha512 } from "@ayonli/jsext/hash";
*
* const hex = await sha512("Hello, World!", "hex");
* console.log(hex);
* // 374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387
*
* const base64 = await sha512("Hello, World!", "base64");
* console.log(base64);
* // N015SpXNz9izWZMYX++bo2jxYNja9DLQi6nx7R5avmzGkpHg+i/gAGpSVw7xjBne9OYXwzzlLvCm5fvjGMsDhw==
* ```
*/
export async function sha512(data: DataSource, encoding: "hex" | "base64"): Promise<string>;
export async function sha512(
data: DataSource,
encoding: "hex" | "base64" | undefined = undefined
): Promise<string | ArrayBuffer> {
if (typeof crypto === "object" && typeof crypto.subtle === "object") {
return encoding ? _sha512(data, encoding) : _sha512(data);
} else if (isDeno || isNodeLike) {
return nodeHash("sha512", data, encoding);
} else {
throw new Error("Unsupported runtime");
}
}
/**
* Calculates the MD5 hash of the given data.
*
* NOTE: This function is not available in the browser.
*
* @example
* ```ts
* import { md5 } from "@ayonli/jsext/hash";
*
* const buffer = await md5("Hello, World!");
* console.log(buffer); // ArrayBuffer(16) { ... }
* ```
*/
export async function md5(data: DataSource): Promise<ArrayBuffer>;
/**
* @example
* ```ts
* import { md5 } from "@ayonli/jsext/hash";
*
* const hex = await md5("Hello, World!", "hex");
* console.log(hex); // 65a8e27d8879283831b664bd8b7f0ad4
*
* const base64 = await md5("Hello, World!", "base64");
* console.log(base64); // ZajifYh5KDgxtmS9i38K1A==
* ```
*/
export async function md5(data: DataSource, encoding: "hex" | "base64"): Promise<string>;
export async function md5(
data: DataSource,
encoding: "hex" | "base64" | undefined = undefined
): Promise<string | ArrayBuffer> {
if (isDeno || isNodeLike) {
return nodeHash("md5", data, encoding);
} else {
throw new Error("Unsupported runtime");
}
}
export async function hmac(
algorithm: "sha1" | "sha256" | "sha512",
key: string | BufferSource,
data: DataSource
): Promise<ArrayBuffer>;
export async function hmac(
algorithm: "sha1" | "sha256" | "sha512",
key: string | BufferSource,
data: DataSource,
encoding: "hex" | "base64"
): Promise<string>;
export async function hmac(
algorithm: "sha1" | "sha256" | "sha512",
key: string | BufferSource,
data: DataSource,
encoding: "hex" | "base64" | undefined = undefined
): Promise<string | ArrayBuffer> {
if (typeof crypto === "object" && typeof crypto.subtle === "object") {
return encoding ? _hmac(algorithm, key, data, encoding) : _hmac(algorithm, key, data);
} else if (isDeno || isNodeLike) {
const crypto = await import("node:crypto");
const binary = await toBytesAsync(data);
const hash = crypto.createHmac(algorithm, bytes(key));
hash.update(binary);
if (encoding) {
return hash.digest(encoding);
} else {
const result = hash.digest();
// Truncate the buffer to the actual byte length so it's consistent with the web API.
return result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength);
}
} else {
throw new Error("Unsupported runtime");
}
}