blob: a9cf45fa94e056b4a2fd8fd18dfe51e7c546ae36 (
plain)
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
|
@safe unittest
{
import std.digest.md;
//Template API
import std.digest.md;
//Feeding data
ubyte[1024] data;
MD5 md5;
md5.start();
md5.put(data[]);
md5.start(); //Start again
md5.put(data[]);
auto hash = md5.finish();
}
@safe unittest
{
import std.digest.md;
//OOP API
import std.digest.md;
auto md5 = new MD5Digest();
ubyte[] hash = md5.digest("abc");
assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72");
//Feeding data
ubyte[1024] data;
md5.put(data[]);
md5.reset(); //Start again
md5.put(data[]);
hash = md5.finish();
}
@safe unittest
{
import std.digest.md;
//Simple example
MD5 hash;
hash.start();
hash.put(cast(ubyte) 0);
ubyte[16] result = hash.finish();
}
@safe unittest
{
import std.digest.md;
//Simple example, hashing a string using md5Of helper function
ubyte[16] hash = md5Of("abc");
//Let's get a hash string
assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72");
}
@safe unittest
{
import std.digest.md;
//Using the basic API
MD5 hash;
hash.start();
ubyte[1024] data;
//Initialize data here...
hash.put(data);
ubyte[16] result = hash.finish();
}
@safe unittest
{
import std.digest.md;
//Let's use the template features:
void doSomething(T)(ref T hash)
if (isDigest!T)
{
hash.put(cast(ubyte) 0);
}
MD5 md5;
md5.start();
doSomething(md5);
assert(toHexString(md5.finish()) == "93B885ADFE0DA089CDF634904FD59F71");
}
@safe unittest
{
import std.digest.md;
ubyte[16] hash = md5Of("abc");
assert(hash == digest!MD5("abc"));
}
@safe unittest
{
import std.digest.md;
//Simple example, hashing a string using Digest.digest helper function
auto md5 = new MD5Digest();
ubyte[] hash = md5.digest("abc");
//Let's get a hash string
assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72");
}
@system unittest
{
import std.digest.md;
//Let's use the OOP features:
void test(Digest dig)
{
dig.put(cast(ubyte) 0);
}
auto md5 = new MD5Digest();
test(md5);
//Let's use a custom buffer:
ubyte[16] buf;
ubyte[] result = md5.finish(buf[]);
assert(toHexString(result) == "93B885ADFE0DA089CDF634904FD59F71");
}
|