This repository was archived by the owner on Jul 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhash.js
More file actions
88 lines (74 loc) · 2.7 KB
/
hash.js
File metadata and controls
88 lines (74 loc) · 2.7 KB
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
/**
* @copyright 2021-2023 Chris Zuber <admin@kernvalley.us>
*/
export const SHA_1 = 'SHA-1';
export const SHA_256 = 'SHA-256';
export const SHA_384 = 'SHA-384';
export const SHA_512 = 'SHA-512';
export const SHA = SHA_1; // Alias of SHA_1
export const HEX = 'hex';
export const BASE_64 = 'base64';
export const ARRAY_BUFFER = 'ArrayBuffer';
export const SRI = 'sri';
const DEFAULT_OUTPUT = HEX;
import { default as md5Shim } from './md5.js';
export async function md5(str) {
return md5Shim(str);
}
export function bufferToHex(buffer) {
if (! (buffer instanceof ArrayBuffer)) {
throw new TypeError('`bufferToHex()` requires an ArrayBuffer');
} else {
return [...new Uint8Array(buffer)].map(value => value.toString(16).padStart(2, '0')).join('');
}
}
export function hexToBuffer(hex) {
if (typeof hex !== 'string') {
throw new TypeError('`hexToBuffer()` only accepts strings');
} else if (hex.length === 0) {
throw new TypeError('Empty string');
} else if (hex.length % 2 !== 0) {
throw new TypeError('Strings must be an even length');
} else {
return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(h => parseInt(h, 16))).buffer;
}
}
export function sha1(data, { output = DEFAULT_OUTPUT } = {}) {
return hash(data, { algorithm: SHA_1, output });
}
export function sha256(data, { output = DEFAULT_OUTPUT } = {}) {
return hash(data, { algorithm: SHA_256, output });
}
export function sha384(data, { output = DEFAULT_OUTPUT } = {}) {
return hash(data, { algorithm: SHA_384, output });
}
export function sha512(data, { output = DEFAULT_OUTPUT } = {}) {
return hash(data, { algorithm: SHA_512, output });
}
export async function hash(data, { algorithm = SHA_256, output = DEFAULT_OUTPUT } = {}) {
if (data instanceof File) {
return hash(await data.arrayBuffer(), { algorithm, output });
} else if (typeof data === 'string') {
return hash(new TextEncoder().encode(data), { algorithm, output });
} else if (typeof data === 'undefined') {
throw new TypeError('Cannot hash `undefined`');
} else if (data instanceof ArrayBuffer || data.buffer instanceof ArrayBuffer) {
const buffer = await crypto.subtle.digest(algorithm.toUpperCase(), data);
switch (output.toLowerCase()) {
case HEX:
return bufferToHex(buffer);
case BASE_64:
return btoa(buffer);
case SRI: {
const codeUnits = new Uint16Array(buffer);
const charCodes = new Uint8Array(codeUnits.buffer);
const result = btoa(String.fromCharCode(...charCodes));
return `${algorithm.replace('-', '').toLowerCase()}-${result}`;
}
case ARRAY_BUFFER:
return buffer;
default:
throw new TypeError(`Unsupported output format: '${output}'`);
}
}
}