forked from connor4312/chromehash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
169 lines (143 loc) · 5.47 KB
/
index.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
import { Hasher } from './pkg/chromehash';
import { promises as fs } from 'fs';
import { StringDecoder } from 'string_decoder';
import { createHash } from 'crypto';
const output = Buffer.alloc(4 * 5);
/** Runs a traditional pre-Chrome 104 hash of the bytes. */
export const hash = (input: Buffer) => {
const hash = new Hasher();
hash.update(normalizeChromehashBuffer(input));
hash.digest(output);
hash.free();
return output.toString('hex');
};
/** Runs a traditional pre-Chrome 104 hash of the bytes. */
export const shaHash = (input: Buffer) => {
const hash = createHash('sha256');
hash.update(normalizeShaBuffer(input));
return hash.digest('hex');
};
/** Runs a traditional pre-Chrome 104 hash of the file. */
export const hashFile = async (file: string, bufferSize = 4096) => {
if (bufferSize % 2 === 1) {
bufferSize++; // ensure buffer size is even for the swap16() in BE BOM reading.
}
const buf = Buffer.alloc(bufferSize);
const hasher = new Hasher();
let fd;
try {
fd = await fs.open(file, 'r');
let lastRead = await fd.read(buf, 0, buf.length, null);
const bomBuf = buf.slice(0, lastRead.bytesRead);
if (hasUtf16LEBOM(bomBuf)) {
hasher.update(bomBuf.slice(2)); // add the trailing BOM read byte
while (lastRead.bytesRead === buf.length) {
lastRead = await fd.read(buf, 0, buf.length, null);
hasher.update(buf.slice(0, lastRead.bytesRead));
}
} else if (hasUtf16BEBOM(bomBuf)) {
hasher.update(bomBuf.slice(2).swap16()); // add the trailing BOM read byte
while (lastRead.bytesRead === buf.length) {
lastRead = await fd.read(buf, 0, buf.length, null);
hasher.update(buf.slice(0, lastRead.bytesRead).swap16());
}
} else if (hasUTF8BOM(bomBuf)) {
const decoder = new StringDecoder('utf8');
hasher.update(Buffer.from(decoder.write(bomBuf.slice(3)), 'utf16le'));
while (lastRead.bytesRead === buf.length) {
lastRead = await fd.read(buf, 0, buf.length, null);
hasher.update(Buffer.from(decoder.write(buf.slice(0, lastRead.bytesRead)), 'utf16le'));
}
} else {
const decoder = new StringDecoder('utf8');
hasher.update(Buffer.from(decoder.write(bomBuf), 'utf16le'));
while (lastRead.bytesRead === buf.length) {
lastRead = await fd.read(buf, 0, buf.length, null);
hasher.update(Buffer.from(decoder.write(buf.slice(0, lastRead.bytesRead)), 'utf16le'));
}
}
hasher.digest(output);
return output.toString('hex');
} finally {
hasher.free();
if (fd !== undefined) {
await fd.close();
}
}
};
const decodeOpts = { stream: true };
/** Runs a modern SHA hash of the file */
export const shaHashFile = async (file: string, bufferSize = 4096) => {
if (bufferSize % 2 === 1) {
bufferSize++; // ensure buffer size is even for the swap16() in BE BOM reading.
}
const buf = Buffer.alloc(bufferSize);
const hasher = createHash('sha256');
let fd: fs.FileHandle | undefined;
try {
fd = await fs.open(file, 'r');
let lastRead = await fd.read(buf, 0, buf.length, null);
const bomBuf = buf.slice(0, lastRead.bytesRead);
if (hasUtf16LEBOM(bomBuf)) {
const decoder = new TextDecoder('utf-16le');
hasher.update(decoder.decode(bomBuf.slice(2), decodeOpts)); // add the trailing BOM read byte
while (lastRead.bytesRead > 0) {
lastRead = await fd.read(buf, 0, buf.length, null);
hasher.update(decoder.decode(buf.slice(0, lastRead.bytesRead), decodeOpts));
}
} else if (hasUtf16BEBOM(bomBuf)) {
const decoder = new TextDecoder('utf-16be');
hasher.update(decoder.decode(bomBuf.slice(2), decodeOpts)); // add the trailing BOM read byte
while (lastRead.bytesRead > 0) {
lastRead = await fd.read(buf, 0, buf.length, null);
hasher.update(decoder.decode(buf.slice(0, lastRead.bytesRead), decodeOpts));
}
} else if (hasUTF8BOM(bomBuf)) {
hasher.update(bomBuf.slice(3));
while (lastRead.bytesRead > 0) {
lastRead = await fd.read(buf, 0, buf.length, null);
hasher.update(buf.slice(0, lastRead.bytesRead));
}
} else {
hasher.update(bomBuf);
while (lastRead.bytesRead > 0) {
lastRead = await fd.read(buf, 0, buf.length, null);
hasher.update(buf.slice(0, lastRead.bytesRead));
}
}
return hasher.digest('hex');
} finally {
await fd?.close();
}
};
const hasUTF8BOM = (buffer: Uint8Array) =>
buffer.length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf;
const hasUtf16LEBOM = (buffer: Uint8Array) =>
buffer.length >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe;
const hasUtf16BEBOM = (buffer: Uint8Array) =>
buffer.length >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff;
const normalizeChromehashBuffer = (buffer: Buffer) => {
if (hasUTF8BOM(buffer)) {
return utf8ToUtf16(buffer.slice(3));
}
if (hasUtf16LEBOM(buffer)) {
return buffer.slice(2);
}
if (hasUtf16BEBOM(buffer)) {
return buffer.slice(2).swap16();
}
return utf8ToUtf16(buffer);
};
const normalizeShaBuffer = (buffer: Buffer) => {
if (hasUTF8BOM(buffer)) {
return buffer.slice(3);
}
if (hasUtf16LEBOM(buffer)) {
return new TextEncoder().encode(new TextDecoder('utf-16le').decode(buffer.slice(2)));
}
if (hasUtf16BEBOM(buffer)) {
return new TextEncoder().encode(new TextDecoder('utf-16be').decode(buffer.slice(2)));
}
return buffer;
};
const utf8ToUtf16 = (buffer: Buffer) => Buffer.from(buffer.toString('utf8'), 'utf16le');