Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_digest_ripemd.d

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