forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigest.res
78 lines (66 loc) · 2.09 KB
/
digest.res
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
/* ************************************************************************ */
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/* ************************************************************************ */
/* Message digest (MD5) */
type t = string
let compare = String.compare
let equal = String.equal
external unsafe_string: (string, int, int) => t = "?md5_string"
let string = str => unsafe_string(str, 0, String.length(str))
let bytes = b => string(Bytes.unsafe_to_string(b))
let substring = (str, ofs, len) =>
if ofs < 0 || (len < 0 || ofs > String.length(str) - len) {
invalid_arg("Digest.substring")
} else {
unsafe_string(str, ofs, len)
}
let subbytes = (b, ofs, len) => substring(Bytes.unsafe_to_string(b), ofs, len)
let char_hex = n =>
Char.unsafe_chr(
n + if n < 10 {
Char.code('0')
} else {
Char.code('a') - 10
},
)
let to_hex = d => {
if String.length(d) != 16 {
invalid_arg("Digest.to_hex")
}
let result = Bytes.create(32)
for i in 0 to 15 {
let x = Char.code(String.get(d, i))
Bytes.unsafe_set(result, i * 2, char_hex(lsr(x, 4)))
Bytes.unsafe_set(result, i * 2 + 1, char_hex(land(x, 0x0f)))
}
Bytes.unsafe_to_string(result)
}
let from_hex = s => {
if String.length(s) != 32 {
invalid_arg("Digest.from_hex")
}
let digit = c =>
switch c {
| '0' .. '9' => Char.code(c) - Char.code('0')
| 'A' .. 'F' => Char.code(c) - Char.code('A') + 10
| 'a' .. 'f' => Char.code(c) - Char.code('a') + 10
| _ => raise(Invalid_argument("Digest.from_hex"))
}
let byte = i => lsl(digit(String.get(s, i)), 4) + digit(String.get(s, i + 1))
let result = Bytes.create(16)
for i in 0 to 15 {
Bytes.set(result, i, Char.chr(byte(2 * i)))
}
Bytes.unsafe_to_string(result)
}