forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigest.js
122 lines (110 loc) · 2.68 KB
/
digest.js
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
import * as Char from "./char.js";
import * as Bytes from "./bytes.js";
import * as $$String from "./string.js";
import * as Caml_md5 from "./caml_md5.js";
import * as Caml_bytes from "./caml_bytes.js";
import * as Caml_string from "./caml_string.js";
function string(str) {
return Caml_md5.md5_string(str, 0, str.length);
}
function bytes(b) {
return string(Bytes.unsafe_to_string(b));
}
function substring(str, ofs, len) {
if (ofs < 0 || len < 0 || ofs > (str.length - len | 0)) {
throw new Error("Invalid_argument", {
cause: {
RE_EXN_ID: "Invalid_argument",
_1: "Digest.substring"
}
});
}
return Caml_md5.md5_string(str, ofs, len);
}
function subbytes(b, ofs, len) {
return substring(Bytes.unsafe_to_string(b), ofs, len);
}
function char_hex(n) {
return n + (
n < 10 ? /* '0' */48 : 87
) | 0;
}
function to_hex(d) {
if (d.length !== 16) {
throw new Error("Invalid_argument", {
cause: {
RE_EXN_ID: "Invalid_argument",
_1: "Digest.to_hex"
}
});
}
let result = Caml_bytes.create(32);
for (let i = 0; i <= 15; ++i) {
let x = Caml_string.get(d, i);
result[(i << 1)] = char_hex((x >>> 4));
result[(i << 1) + 1 | 0] = char_hex(x & 15);
}
return Bytes.unsafe_to_string(result);
}
function from_hex(s) {
if (s.length !== 32) {
throw new Error("Invalid_argument", {
cause: {
RE_EXN_ID: "Invalid_argument",
_1: "Digest.from_hex"
}
});
}
let digit = c => {
if (c >= 65) {
if (c >= 97) {
if (c >= 103) {
throw new Error("Invalid_argument", {
cause: {
RE_EXN_ID: "Invalid_argument",
_1: "Digest.from_hex"
}
});
}
return (c - /* 'a' */97 | 0) + 10 | 0;
}
if (c >= 71) {
throw new Error("Invalid_argument", {
cause: {
RE_EXN_ID: "Invalid_argument",
_1: "Digest.from_hex"
}
});
}
return (c - /* 'A' */65 | 0) + 10 | 0;
}
if (c > 57 || c < 48) {
throw new Error("Invalid_argument", {
cause: {
RE_EXN_ID: "Invalid_argument",
_1: "Digest.from_hex"
}
});
}
return c - /* '0' */48 | 0;
};
let byte = i => (digit(Caml_string.get(s, i)) << 4) + digit(Caml_string.get(s, i + 1 | 0)) | 0;
let result = Caml_bytes.create(16);
for (let i = 0; i <= 15; ++i) {
Caml_bytes.set(result, i, Char.chr(byte((i << 1))));
}
return Bytes.unsafe_to_string(result);
}
let compare = $$String.compare;
let equal = $$String.equal;
export {
compare,
equal,
string,
bytes,
substring,
subbytes,
to_hex,
from_hex,
}
/* No side effect */