11/* *
22 * @file
3- * @author [Shree Harish S](https://github.com/ShreeHarish)
43 * @brief C++ implementation of the [RIPEMD-160 Hashing
54 * Algorithm](https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf)
65 *
2120 * of 80 processing steps, split into two parallel paths that converge at the
2221 * end.
2322 *
23+ * @author [Shree Harish S](https://github.com/ShreeHarish)
2424 */
2525
2626#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
2828#include < iomanip> // / For functions like setw, setfill
2929#include < iostream> // / For managing io
3030#include < sstream> // / For bytes to hex string
@@ -65,28 +65,30 @@ class RIPEMD160 {
6565 // add padding to data
6666 int extra = data.size () % 4 ;
6767
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 ;
9092 }
9193
9294 while (word_data.size () % 16 != 14 ) {
@@ -105,30 +107,21 @@ class RIPEMD160 {
105107 /* *
106108 * @brief implements f(j,x,y,z)
107109 * @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
111111 * @return returns the function value
112112 */
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);
132125 }
133126 }
134127
@@ -138,23 +131,15 @@ class RIPEMD160 {
138131 * @return appropriate K value
139132 */
140133 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 :
146137 return static_cast <uint32_t >(0x5A827999 );
147- }
148-
149- else if (j == 2 ) {
138+ case 2 :
150139 return static_cast <uint32_t >(0x6ED9EBA1 );
151- }
152-
153- else if (j == 3 ) {
140+ case 3 :
154141 return static_cast <uint32_t >(0x8F1BBCDC );
155- }
156-
157- else { // / if (j == 4)
142+ case 4 :
158143 return static_cast <uint32_t >(0xA953FD4E );
159144 }
160145 }
@@ -165,24 +150,17 @@ class RIPEMD160 {
165150 * @return appropriate K' value
166151 */
167152 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 ;
186164 }
187165 }
188166
@@ -452,6 +430,5 @@ static void test() {
452430 */
453431int main (int argc, char *argv[]) {
454432 test ();
455-
456433 return 0 ;
457- }
434+ }
0 commit comments