Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_digest_sha.d

raw
1 @safe unittest
2 {
3 import std.digest.sha;
4
5 //Template API
6 import std.digest.sha;
7
8 ubyte[20] hash1 = sha1Of("abc");
9 assert(toHexString(hash1) == "A9993E364706816ABA3E25717850C26C9CD0D89D");
10
11 ubyte[28] hash224 = sha224Of("abc");
12 assert(toHexString(hash224) == "23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7");
13
14 //Feeding data
15 ubyte[1024] data;
16 SHA1 sha1;
17 sha1.start();
18 sha1.put(data[]);
19 sha1.start(); //Start again
20 sha1.put(data[]);
21 hash1 = sha1.finish();
22 }
23
24 @safe unittest
25 {
26 import std.digest.sha;
27
28 //OOP API
29 import std.digest.sha;
30
31 auto sha1 = new SHA1Digest();
32 ubyte[] hash1 = sha1.digest("abc");
33 assert(toHexString(hash1) == "A9993E364706816ABA3E25717850C26C9CD0D89D");
34
35 auto sha224 = new SHA224Digest();
36 ubyte[] hash224 = sha224.digest("abc");
37 assert(toHexString(hash224) == "23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7");
38
39 //Feeding data
40 ubyte[1024] data;
41 sha1.put(data[]);
42 sha1.reset(); //Start again
43 sha1.put(data[]);
44 hash1 = sha1.finish();
45 }
46
47 @safe unittest
48 {
49 import std.digest.sha;
50
51 //Simple example
52 SHA1 hash;
53 hash.start();
54 hash.put(cast(ubyte) 0);
55 ubyte[20] result = hash.finish();
56
57 }
58
59 @safe unittest
60 {
61 import std.digest.sha;
62
63 //Simple example, hashing a string using sha1Of helper function
64 ubyte[20] hash = sha1Of("abc");
65 //Let's get a hash string
66 assert(toHexString(hash) == "A9993E364706816ABA3E25717850C26C9CD0D89D");
67
68 //The same, but using SHA-224
69 ubyte[28] hash224 = sha224Of("abc");
70 assert(toHexString(hash224) == "23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7");
71 }
72
73 @safe unittest
74 {
75 import std.digest.sha;
76
77 //Using the basic API
78 SHA1 hash;
79 hash.start();
80 ubyte[1024] data;
81 //Initialize data here...
82 hash.put(data);
83 ubyte[20] result = hash.finish();
84 }
85
86 @safe unittest
87 {
88 import std.digest.sha;
89
90 //Let's use the template features:
91 //Note: When passing a SHA1 to a function, it must be passed by reference!
92 void doSomething(T)(ref T hash)
93 if (isDigest!T)
94 {
95 hash.put(cast(ubyte) 0);
96 }
97 SHA1 sha;
98 sha.start();
99 doSomething(sha);
100 assert(toHexString(sha.finish()) == "5BA93C9DB0CFF93F52B521D7420E43F6EDA2784F");
101 }
102
103 @safe unittest
104 {
105 import std.digest.sha;
106
107 ubyte[20] hash = sha1Of("abc");
108 assert(hash == digest!SHA1("abc"));
109
110 ubyte[28] hash224 = sha224Of("abc");
111 assert(hash224 == digest!SHA224("abc"));
112
113 ubyte[32] hash256 = sha256Of("abc");
114 assert(hash256 == digest!SHA256("abc"));
115
116 ubyte[48] hash384 = sha384Of("abc");
117 assert(hash384 == digest!SHA384("abc"));
118
119 ubyte[64] hash512 = sha512Of("abc");
120 assert(hash512 == digest!SHA512("abc"));
121
122 ubyte[28] hash512_224 = sha512_224Of("abc");
123 assert(hash512_224 == digest!SHA512_224("abc"));
124
125 ubyte[32] hash512_256 = sha512_256Of("abc");
126 assert(hash512_256 == digest!SHA512_256("abc"));
127 }
128
129 @safe unittest
130 {
131 import std.digest.sha;
132
133 //Simple example, hashing a string using Digest.digest helper function
134 auto sha = new SHA1Digest();
135 ubyte[] hash = sha.digest("abc");
136 //Let's get a hash string
137 assert(toHexString(hash) == "A9993E364706816ABA3E25717850C26C9CD0D89D");
138
139 //The same, but using SHA-224
140 auto sha224 = new SHA224Digest();
141 ubyte[] hash224 = sha224.digest("abc");
142 //Let's get a hash string
143 assert(toHexString(hash224) == "23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7");
144 }
145
146 @system unittest
147 {
148 import std.digest.sha;
149
150 //Let's use the OOP features:
151 void test(Digest dig)
152 {
153 dig.put(cast(ubyte) 0);
154 }
155 auto sha = new SHA1Digest();
156 test(sha);
157
158 //Let's use a custom buffer:
159 ubyte[20] buf;
160 ubyte[] result = sha.finish(buf[]);
161 assert(toHexString(result) == "5BA93C9DB0CFF93F52B521D7420E43F6EDA2784F");
162 }
163