1
1
/* *
2
2
* @file
3
- * @author [Shree Harish S](https://github.com/ShreeHarish)
4
3
* @brief C++ implementation of the [RIPEMD-160 Hashing
5
4
* Algorithm](https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf)
6
5
*
21
20
* of 80 processing steps, split into two parallel paths that converge at the
22
21
* end.
23
22
*
23
+ * @author [Shree Harish S](https://github.com/ShreeHarish)
24
24
*/
25
25
26
26
#include < cassert> // / For assert
27
- #include < cstdint> // / For data types such as int32_t, uint32_t, etc
27
+ #include < cstdint> // / For data types such as std:: int32_t, std:: uint32_t, etc
28
28
#include < iomanip> // / For functions like setw, setfill
29
29
#include < iostream> // / For managing io
30
30
#include < sstream> // / For bytes to hex string
@@ -65,28 +65,30 @@ class RIPEMD160 {
65
65
// add padding to data
66
66
int extra = data.size () % 4 ;
67
67
68
- if (extra == 0 ) {
69
- word_data.push_back (0x00000080 );
70
- }
71
-
72
- else if (extra == 1 ) {
73
- word_data.push_back (static_cast <uint32_t >(data[data.size () - 1 ]) |
74
- 0x00008000 );
75
- }
76
-
77
- else if (extra == 2 ) {
78
- word_data.push_back (
79
- static_cast <uint32_t >(data[data.size () - 2 ]) |
80
- (static_cast <uint32_t >(data[data.size () - 1 ]) << 8 ) |
81
- 0x00800000 );
82
- }
83
-
84
- else if (extra == 3 ) {
85
- word_data.push_back (
86
- static_cast <uint32_t >(data[data.size () - 3 ]) |
87
- (static_cast <uint32_t >(data[data.size () - 2 ]) << 8 ) |
88
- (static_cast <uint32_t >(data[data.size () - 1 ]) << 16 ) |
89
- 0x80000000 );
68
+ switch (extra) {
69
+ case 0 :
70
+ word_data.push_back (0x00000080 );
71
+ break ;
72
+
73
+ case 1 :
74
+ word_data.push_back (
75
+ static_cast <uint32_t >(data[data.size () - 1 ]) | 0x00008000 );
76
+ break ;
77
+
78
+ case 2 :
79
+ word_data.push_back (
80
+ static_cast <uint32_t >(data[data.size () - 2 ]) |
81
+ (static_cast <uint32_t >(data[data.size () - 1 ]) << 8 ) |
82
+ 0x00800000 );
83
+ break ;
84
+
85
+ case 3 :
86
+ word_data.push_back (
87
+ static_cast <uint32_t >(data[data.size () - 3 ]) |
88
+ (static_cast <uint32_t >(data[data.size () - 2 ]) << 8 ) |
89
+ (static_cast <uint32_t >(data[data.size () - 1 ]) << 16 ) |
90
+ 0x80000000 );
91
+ break ;
90
92
}
91
93
92
94
while (word_data.size () % 16 != 14 ) {
@@ -105,30 +107,21 @@ class RIPEMD160 {
105
107
/* *
106
108
* @brief implements f(j,x,y,z)
107
109
* @param j round number j / 16
108
- * @param x value x
109
- * @param y value y
110
- * @param z value z
110
+ * @param B,C,D the state values
111
111
* @return returns the function value
112
112
*/
113
- uint32_t f (int j, uint32_t x, uint32_t y, uint32_t z) {
114
- if (j == 0 ) {
115
- return x ^ y ^ z;
116
- }
117
-
118
- else if (j == 1 ) {
119
- return (x & y) | (~x & z);
120
- }
121
-
122
- else if (j == 2 ) {
123
- return (x | ~y) ^ z;
124
- }
125
-
126
- else if (j == 3 ) {
127
- return (x & z) | (y & ~z);
128
- }
129
-
130
- else { // / if (j == 4)
131
- return x ^ (y | ~z);
113
+ uint32_t f (int j, uint32_t B, uint32_t C, uint32_t D) {
114
+ switch (j) {
115
+ case 0 :
116
+ return B ^ C ^ D;
117
+ case 1 :
118
+ return (B & C) | (~B & D);
119
+ case 2 :
120
+ return (B | ~C) ^ D;
121
+ case 3 :
122
+ return (B & D) | (C & ~D);
123
+ case 4 :
124
+ return B ^ (C | ~D);
132
125
}
133
126
}
134
127
@@ -138,23 +131,15 @@ class RIPEMD160 {
138
131
* @return appropriate K value
139
132
*/
140
133
uint32_t K (int j) {
141
- if (j == 0 ) {
142
- return static_cast <uint32_t >(0x00000000 );
143
- }
144
-
145
- else if (j == 1 ) {
134
+ switch (j) {
135
+ case 0 : return static_cast <uint32_t >(0x00000000 );
136
+ case 1 :
146
137
return static_cast <uint32_t >(0x5A827999 );
147
- }
148
-
149
- else if (j == 2 ) {
138
+ case 2 :
150
139
return static_cast <uint32_t >(0x6ED9EBA1 );
151
- }
152
-
153
- else if (j == 3 ) {
140
+ case 3 :
154
141
return static_cast <uint32_t >(0x8F1BBCDC );
155
- }
156
-
157
- else { // / if (j == 4)
142
+ case 4 :
158
143
return static_cast <uint32_t >(0xA953FD4E );
159
144
}
160
145
}
@@ -165,24 +150,17 @@ class RIPEMD160 {
165
150
* @return appropriate K' value
166
151
*/
167
152
uint32_t K_dash (int j) {
168
- if (j == 0 ) {
169
- return 0x50A28BE6 ;
170
- }
171
-
172
- else if (j == 1 ) {
173
- return 0x5C4DD124 ;
174
- }
175
-
176
- else if (j == 2 ) {
177
- return 0x6D703EF3 ;
178
- }
179
-
180
- else if (j == 3 ) {
181
- return 0x7A6D76E9 ;
182
- }
183
-
184
- else { // / if (j == 4)
185
- return 0x00000000 ;
153
+ switch (j){
154
+ case 0 :
155
+ return 0x50A28BE6 ;
156
+ case 1 :
157
+ return 0x5C4DD124 ;
158
+ case 2 :
159
+ return 0x6D703EF3 ;
160
+ case 3 :
161
+ return 0x7A6D76E9 ;
162
+ case 4 :
163
+ return 0x00000000 ;
186
164
}
187
165
}
188
166
@@ -452,6 +430,5 @@ static void test() {
452
430
*/
453
431
int main (int argc, char *argv[]) {
454
432
test ();
455
-
456
433
return 0 ;
457
- }
434
+ }
0 commit comments