mirror of
https://github.com/thunderbrewhq/common.git
synced 2026-02-04 08:59:08 +00:00
feat(sha1): add SHA1_InterleaveHash
This commit is contained in:
parent
ca7b7c3bc6
commit
04ee35a71b
3 changed files with 89 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
|||
#include "common/SHA1.hpp"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
||||
|
|
@ -61,6 +62,60 @@ void SHA1_Init(SHA1_CONTEXT* context) {
|
|||
context->count[1] = 0;
|
||||
}
|
||||
|
||||
uint8_t* SHA1_InterleaveHash(uint8_t* const digest, const uint8_t* data, uint32_t len) {
|
||||
// Terminate data at first null
|
||||
uint32_t l;
|
||||
for (l = len; l; l--) {
|
||||
if (*data) {
|
||||
break;
|
||||
}
|
||||
|
||||
data++;
|
||||
}
|
||||
|
||||
if (l & 1) {
|
||||
data++;
|
||||
l--;
|
||||
}
|
||||
|
||||
SHA1_CONTEXT ctx;
|
||||
uint8_t scratchDigest[SHA1_DIGEST_SIZE];
|
||||
|
||||
auto scratchLen = l / 2;
|
||||
auto scratch = static_cast<uint8_t*>(alloca(scratchLen));
|
||||
if (!scratch) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Even half
|
||||
for (uint32_t i = 0; i < scratchLen; i++) {
|
||||
scratch[i] = data[i * 2];
|
||||
}
|
||||
|
||||
SHA1_Init(&ctx);
|
||||
SHA1_Update(&ctx, scratch, scratchLen);
|
||||
SHA1_Final(scratchDigest, &ctx);
|
||||
|
||||
for (uint32_t i = 0; i < sizeof(scratchDigest); i++) {
|
||||
digest[i * 2] = scratchDigest[i];
|
||||
}
|
||||
|
||||
// Odd half
|
||||
for (uint32_t i = 0; i < scratchLen; i++) {
|
||||
scratch[i] = data[i * 2 + 1];
|
||||
}
|
||||
|
||||
SHA1_Init(&ctx);
|
||||
SHA1_Update(&ctx, scratch, scratchLen);
|
||||
SHA1_Final(scratchDigest, &ctx);
|
||||
|
||||
for (uint32_t i = 0; i < sizeof(scratchDigest); i++) {
|
||||
digest[i * 2 + 1] = scratchDigest[i];
|
||||
}
|
||||
|
||||
return digest;
|
||||
}
|
||||
|
||||
void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]) {
|
||||
uint32_t a, b, c, d, e;
|
||||
typedef union {
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ void SHA1_Final(uint8_t* const digest, SHA1_CONTEXT* context);
|
|||
|
||||
void SHA1_Init(SHA1_CONTEXT* context);
|
||||
|
||||
uint8_t* SHA1_InterleaveHash(uint8_t* digest, const uint8_t* data, uint32_t len);
|
||||
|
||||
void SHA1_Update(SHA1_CONTEXT* context, const uint8_t* data, uint32_t len);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue