-
Notifications
You must be signed in to change notification settings - Fork 5
/
hash-macro.dylan
74 lines (60 loc) · 2.18 KB
/
hash-macro.dylan
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
module: hash-algorithms
author: Hannes Mehnert
copyright: See LICENSE file in this distribution.
define simple-C-mapped-subtype <C-buffer-offset> (<C-void*>)
export-map <machine-word>, export-function: identity;
end;
define macro hash-definer
{ define hash ?:name (?digest-size:expression; ?block-size:expression) } =>
{
define C-subtype "<" ## ?name ## "-context>" (<C-void*>) end;
define C-function "init-" ## ?name
result context :: "<" ## ?name ## "-context>";
c-name: ?"name" ## "_Init"
end;
define C-function "update-" ## ?name
parameter context :: "<" ## ?name ## "-context>";
parameter data :: <C-buffer-offset>;
parameter length :: <C-int>;
c-name: ?"name" ## "_Update"
end;
define C-function "final-" ## ?name
parameter hash :: <C-buffer-offset>;
parameter context :: "<" ## ?name ## "-context>";
c-name: ?"name" ## "_Final"
end;
define class "<" ## ?name ## ">" (<hash>)
inherited slot digest-size = ?digest-size;
inherited slot block-size = ?block-size;
end;
define method make (class == "<" ## ?name ## ">", #next next-method,
#rest rest, #key, #all-keys) => (hash :: "<" ## ?name ## ">")
let result = next-method();
let ctx = "init-" ## ?name ();
result.context := ctx;
result
end;
define method update-hash (hash :: "<" ## ?name ## ">", input) => ()
"update-" ## ?name (hash.context, byte-storage-address(input), input.size)
end;
define method digest (hash :: "<" ## ?name ## ">") => (result :: <byte-vector>)
let res = make(<byte-vector>, size: ?digest-size);
"final-" ## ?name (byte-storage-address(res), hash.context);
res;
end;
define function ?name (input) => (result :: <byte-vector>)
let ctx = make("<" ## ?name ## ">");
update-hash(ctx, input);
digest(ctx)
end
}
end;
//using md5.c
define hash md5 (16; 64);
//using sha1.c
define hash sha1 (20; 64);
//using sha2.c
define hash sha256 (32; 64);
define hash sha224 (28; 64);
define hash sha384 (48; 128);
define hash sha512 (64; 128);