Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_digest_hmac.d

raw
1 @safe unittest
2 {
3 import std.digest.hmac;
4
5 import std.ascii : LetterCase;
6 import std.digest : toHexString;
7 import std.digest.sha : SHA1;
8 import std.string : representation;
9
10 auto secret = "secret".representation;
11 assert("The quick brown fox jumps over the lazy dog"
12 .representation
13 .hmac!SHA1(secret)
14 .toHexString!(LetterCase.lower) == "198ea1ea04c435c1246b586a06d5cf11c3ffcda6");
15 }
16
17 @safe pure nothrow @nogc unittest
18 {
19 import std.digest.hmac;
20
21 import std.digest.sha : SHA1;
22 import std.string : representation;
23 auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
24 hmac.put("Hello, world".representation);
25 static immutable expected = [
26 130, 32, 235, 44, 208, 141,
27 150, 232, 211, 214, 162, 195,
28 188, 127, 52, 89, 100, 68, 90, 216];
29 assert(hmac.finish() == expected);
30
31 }
32
33 @safe pure nothrow @nogc unittest
34 {
35 import std.digest.hmac;
36
37 import std.digest.sha : SHA1;
38 import std.string : representation;
39 string data1 = "Hello, world", data2 = "Hola mundo";
40 auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
41 hmac.put(data1.representation);
42 hmac.start(); // reset digest
43 hmac.put(data2.representation); // start over
44 static immutable expected = [
45 122, 151, 232, 240, 249, 80,
46 19, 178, 186, 77, 110, 23, 208,
47 52, 11, 88, 34, 151, 192, 255];
48 assert(hmac.finish() == expected);
49
50 }
51
52 @safe pure nothrow @nogc unittest
53 {
54 import std.digest.hmac;
55
56 import std.digest.hmac, std.digest.sha;
57 import std.string : representation;
58 string data1 = "Hello, world", data2 = "Hola mundo";
59 auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
60 hmac.put(data1.representation)
61 .put(data2.representation);
62 static immutable expected = [
63 197, 57, 52, 3, 13, 194, 13,
64 36, 117, 228, 8, 11, 111, 51,
65 165, 3, 123, 31, 251, 113];
66 assert(hmac.finish() == expected);
67
68 }
69
70 @safe pure nothrow @nogc unittest
71 {
72 import std.digest.hmac;
73
74 import std.digest.sha : SHA1;
75 import std.string : representation;
76 string data1 = "Hello, world", data2 = "Hola mundo";
77 auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
78 auto testDigest = hmac.put(data1.representation)
79 .put(data2.representation)
80 .finish();
81 static immutable expected = [
82 197, 57, 52, 3, 13, 194, 13,
83 36, 117, 228, 8, 11, 111, 51,
84 165, 3, 123, 31, 251, 113];
85 assert(testDigest == expected);
86
87 }
88
89 @safe pure nothrow @nogc unittest
90 {
91 import std.digest.hmac;
92
93 import std.digest.sha : SHA1;
94 import std.string : representation;
95 string data1 = "Hello, world", data2 = "Hola mundo";
96 auto digest = hmac!SHA1("My s3cR3T keY".representation)
97 .put(data1.representation)
98 .put(data2.representation)
99 .finish();
100 static immutable expected = [
101 197, 57, 52, 3, 13, 194, 13, 36,
102 117, 228, 8, 11, 111, 51, 165,
103 3, 123, 31, 251, 113];
104 assert(digest == expected);
105
106 }
107
108 @safe pure nothrow @nogc unittest
109 {
110 import std.digest.hmac;
111
112 import std.algorithm.iteration : map;
113 import std.digest.sha : SHA1;
114 import std.string : representation;
115 string data = "Hello, world";
116 auto digest = data.representation
117 .map!(a => cast(ubyte)(a+1))
118 .hmac!SHA1("My s3cR3T keY".representation);
119 static assert(is(typeof(digest) == ubyte[20]));
120 static immutable expected = [
121 163, 208, 118, 179, 216, 93,
122 17, 10, 84, 200, 87, 104, 244,
123 111, 136, 214, 167, 210, 58, 10];
124 assert(digest == expected);
125
126 }
127
128 @safe pure nothrow @nogc unittest
129 {
130 import std.digest.hmac;
131
132 import std.digest.sha : SHA1;
133 import std.string : representation;
134 string data1 = "Hello, world", data2 = "Hola mundo";
135 auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
136 auto digest = hmac.put(data1.representation)
137 .put(data2.representation)
138 .finish();
139 static immutable expected = [
140 197, 57, 52, 3, 13, 194, 13,
141 36, 117, 228, 8, 11, 111, 51,
142 165, 3, 123, 31, 251, 113];
143 assert(digest == expected);
144 }
145