Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/core/hash.ts
3557 views
1
/*
2
* hash.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { crypto } from "crypto/crypto";
8
import blueimpMd5 from "blueimpMd5";
9
10
export function md5HashSync(content: string) {
11
return blueimpMd5(content);
12
}
13
14
export async function md5HashAsync(content: string) {
15
const buffer = new TextEncoder().encode(content);
16
return md5HashBytes(buffer);
17
}
18
19
export async function md5HashBytes(content: Uint8Array) {
20
const buffer = await crypto.subtle.digest(
21
"MD5",
22
content,
23
);
24
return Array.from(new Uint8Array(buffer))
25
.map((b) => b.toString(16).padStart(2, "0"))
26
.join("");
27
}
28
29
// Simple insecure hash for a string
30
export function insecureHash(content: string) {
31
let hash = 0;
32
for (let i = 0; i < content.length; i++) {
33
const char = content.charCodeAt(i);
34
hash = (hash << 5) - hash + char;
35
hash &= hash;
36
}
37
return new Uint32Array([hash])[0].toString(36);
38
}
39
40