Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_digest_md.d

raw
1 @safe unittest
2 {
3 import std.digest.md;
4
5 //Template API
6 import std.digest.md;
7
8 //Feeding data
9 ubyte[1024] data;
10 MD5 md5;
11 md5.start();
12 md5.put(data[]);
13 md5.start(); //Start again
14 md5.put(data[]);
15 auto hash = md5.finish();
16 }
17
18 @safe unittest
19 {
20 import std.digest.md;
21
22 //OOP API
23 import std.digest.md;
24
25 auto md5 = new MD5Digest();
26 ubyte[] hash = md5.digest("abc");
27 assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72");
28
29 //Feeding data
30 ubyte[1024] data;
31 md5.put(data[]);
32 md5.reset(); //Start again
33 md5.put(data[]);
34 hash = md5.finish();
35 }
36
37 @safe unittest
38 {
39 import std.digest.md;
40
41 //Simple example
42 MD5 hash;
43 hash.start();
44 hash.put(cast(ubyte) 0);
45 ubyte[16] result = hash.finish();
46
47 }
48
49 @safe unittest
50 {
51 import std.digest.md;
52
53 //Simple example, hashing a string using md5Of helper function
54 ubyte[16] hash = md5Of("abc");
55 //Let's get a hash string
56 assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72");
57 }
58
59 @safe unittest
60 {
61 import std.digest.md;
62
63 //Using the basic API
64 MD5 hash;
65 hash.start();
66 ubyte[1024] data;
67 //Initialize data here...
68 hash.put(data);
69 ubyte[16] result = hash.finish();
70 }
71
72 @safe unittest
73 {
74 import std.digest.md;
75
76 //Let's use the template features:
77 void doSomething(T)(ref T hash)
78 if (isDigest!T)
79 {
80 hash.put(cast(ubyte) 0);
81 }
82 MD5 md5;
83 md5.start();
84 doSomething(md5);
85 assert(toHexString(md5.finish()) == "93B885ADFE0DA089CDF634904FD59F71");
86 }
87
88 @safe unittest
89 {
90 import std.digest.md;
91
92 ubyte[16] hash = md5Of("abc");
93 assert(hash == digest!MD5("abc"));
94 }
95
96 @safe unittest
97 {
98 import std.digest.md;
99
100 //Simple example, hashing a string using Digest.digest helper function
101 auto md5 = new MD5Digest();
102 ubyte[] hash = md5.digest("abc");
103 //Let's get a hash string
104 assert(toHexString(hash) == "900150983CD24FB0D6963F7D28E17F72");
105 }
106
107 @system unittest
108 {
109 import std.digest.md;
110
111 //Let's use the OOP features:
112 void test(Digest dig)
113 {
114 dig.put(cast(ubyte) 0);
115 }
116 auto md5 = new MD5Digest();
117 test(md5);
118
119 //Let's use a custom buffer:
120 ubyte[16] buf;
121 ubyte[] result = md5.finish(buf[]);
122 assert(toHexString(result) == "93B885ADFE0DA089CDF634904FD59F71");
123 }
124