| 153 | /* Add padding and return the message digest. */ |
| 154 | |
| 155 | void SHA1Final(unsigned char digest[20], SHA1_CTX* context) |
| 156 | { |
| 157 | unsigned i; |
| 158 | unsigned char finalcount[8]; |
| 159 | unsigned char c; |
| 160 | |
| 161 | #if 0 /* untested "improvement" by DHR */ |
| 162 | /* Convert context->count to a sequence of bytes |
| 163 | * in finalcount. Second element first, but |
| 164 | * big-endian order within element. |
| 165 | * But we do it all backwards. |
| 166 | */ |
| 167 | unsigned char *fcp = &finalcount[8]; |
| 168 | |
| 169 | for (i = 0; i < 2; i++) |
| 170 | { |
| 171 | uint32_t t = context->count[i]; |
| 172 | int j; |
| 173 | |
| 174 | for (j = 0; j < 4; t >>= 8, j++) |
| 175 | *--fcp = (unsigned char) t; |
| 176 | } |
| 177 | #else |
| 178 | for (i = 0; i < 8; i++) { |
| 179 | finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] |
| 180 | >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ |
| 181 | } |
| 182 | #endif |
| 183 | c = 0200; |
| 184 | SHA1Update(context, &c, 1); |
| 185 | while ((context->count[0] & 504) != 448) { |
| 186 | c = 0000; |
| 187 | SHA1Update(context, &c, 1); |
| 188 | } |
| 189 | SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ |
| 190 | for (i = 0; i < 20; i++) { |
| 191 | digest[i] = (unsigned char) |
| 192 | ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
| 193 | } |
| 194 | /* Wipe variables */ |
| 195 | memset(context, '\0', sizeof(*context)); |
| 196 | memset(&finalcount, '\0', sizeof(finalcount)); |
| 197 | } |
| 198 | /* ================ end of sha1.c ================ */ |
| 199 | |
| 200 | #ifdef REDIS_TEST |