forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigest.js
137 lines (123 loc) · 3.29 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
var Char = require("./char.js");
var $$String = require("./string.js");
var Caml_md5 = require("./caml_md5.js");
var Pervasives = require("./pervasives.js");
var Caml_string = require("./caml_string.js");
var Caml_builtin_exceptions = require("./caml_builtin_exceptions.js");
function string(str) {
return Caml_md5.caml_md5_string(str, 0, str.length);
}
function bytes(b) {
return string(Caml_string.bytes_to_string(b));
}
function substring(str, ofs, len) {
if (ofs < 0 || len < 0 || ofs > (str.length - len | 0)) {
throw [
Caml_builtin_exceptions.invalid_argument,
"Digest.substring"
];
} else {
return Caml_md5.caml_md5_string(str, ofs, len);
}
}
function subbytes(b, ofs, len) {
return substring(Caml_string.bytes_to_string(b), ofs, len);
}
function file(filename) {
Pervasives.open_in_bin(filename);
var exit = 0;
var d;
try {
d = function () {
throw "caml_md5_chan not implemented by bucklescript yet\n";
}();
exit = 1;
}
catch (e){
(function () {
throw "caml_ml_close_channel not implemented by bucklescript yet\n";
}());
throw e;
}
if (exit === 1) {
(function () {
throw "caml_ml_close_channel not implemented by bucklescript yet\n";
}());
return d;
}
}
var output = Pervasives.output_string;
function input(chan) {
return Pervasives.really_input_string(chan, 16);
}
function char_hex(n) {
return n + (
n < 10 ? /* "0" */48 : 87
) | 0;
}
function to_hex(d) {
var result = new Array(32);
for(var i = 0; i <= 15; ++i){
var x = Caml_string.get(d, i);
result[(i << 1)] = char_hex((x >>> 4));
result[(i << 1) + 1 | 0] = char_hex(x & 15);
}
return Caml_string.bytes_to_string(result);
}
function from_hex(s) {
if (s.length !== 32) {
throw [
Caml_builtin_exceptions.invalid_argument,
"Digest.from_hex"
];
}
var digit = function (c) {
if (c >= 65) {
if (c >= 97) {
if (c >= 103) {
throw [
Caml_builtin_exceptions.invalid_argument,
"Digest.from_hex"
];
} else {
return (c - /* "a" */97 | 0) + 10 | 0;
}
} else if (c >= 71) {
throw [
Caml_builtin_exceptions.invalid_argument,
"Digest.from_hex"
];
} else {
return (c - /* "A" */65 | 0) + 10 | 0;
}
} else if (c > 57 || c < 48) {
throw [
Caml_builtin_exceptions.invalid_argument,
"Digest.from_hex"
];
} else {
return c - /* "0" */48 | 0;
}
};
var $$byte = function (i) {
return (digit(Caml_string.get(s, i)) << 4) + digit(Caml_string.get(s, i + 1 | 0)) | 0;
};
var result = new Array(16);
for(var i = 0; i <= 15; ++i){
result[i] = Char.chr($$byte((i << 1)));
}
return Caml_string.bytes_to_string(result);
}
var compare = $$String.compare;
exports.compare = compare;
exports.string = string;
exports.bytes = bytes;
exports.substring = substring;
exports.subbytes = subbytes;
exports.file = file;
exports.output = output;
exports.input = input;
exports.to_hex = to_hex;
exports.from_hex = from_hex;
/* No side effect */