core: replace SHA1() function by SHA1_uint32()

The SHA1() helper function was only used when calculating a
SHA1 hash and taking the first four bytes of it as uint32.

Make that explicit by renaming the function into SHA1_uint32()
and directly returning an uint32_t.

Note that the usage in cochran.cpp is sketchy: it generates
a four-byte hash out of two-byte data. Why!?

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-04-23 15:28:11 +08:00 committed by bstoeger
parent 0b817e468a
commit b24f37fb4f
4 changed files with 17 additions and 18 deletions

View file

@ -610,7 +610,6 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod,
struct dive *dive; struct dive *dive;
struct divecomputer *dc; struct divecomputer *dc;
struct tm tm = {0}; struct tm tm = {0};
uint32_t csum[5];
double max_depth, avg_depth, min_temp; double max_depth, avg_depth, min_temp;
unsigned int duration = 0, corrupt_dive = 0; unsigned int duration = 0, corrupt_dive = 0;
@ -719,8 +718,7 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod,
* (double) log[CMD_ALTITUDE] * 250 * FEET, 5.25588) * 1000); * (double) log[CMD_ALTITUDE] * 250 * FEET, 5.25588) * 1000);
dc->salinity = 10000 + 150 * log[CMD_WATER_CONDUCTIVITY]; dc->salinity = 10000 + 150 * log[CMD_WATER_CONDUCTIVITY];
SHA1(log + CMD_NUMBER, 2, (unsigned char *)csum); dc->diveid = SHA1_uint32(log + CMD_NUMBER, 2);
dc->diveid = csum[0];
if (log[CMD_MAX_DEPTH] == 0xff && log[CMD_MAX_DEPTH + 1] == 0xff) if (log[CMD_MAX_DEPTH] == 0xff && log[CMD_MAX_DEPTH + 1] == 0xff)
corrupt_dive = 1; corrupt_dive = 1;
@ -765,8 +763,7 @@ static void cochran_parse_dive(const unsigned char *decode, unsigned mod,
* (double) log[EMC_ALTITUDE] * 250 * FEET, 5.25588) * 1000); * (double) log[EMC_ALTITUDE] * 250 * FEET, 5.25588) * 1000);
dc->salinity = 10000 + 150 * (log[EMC_WATER_CONDUCTIVITY] & 0x3); dc->salinity = 10000 + 150 * (log[EMC_WATER_CONDUCTIVITY] & 0x3);
SHA1(log + EMC_NUMBER, 2, (unsigned char *)csum); dc->diveid = SHA1_uint32(log + EMC_NUMBER, 2);
dc->diveid = csum[0];
if (log[EMC_MAX_DEPTH] == 0xff && log[EMC_MAX_DEPTH + 1] == 0xff) if (log[EMC_MAX_DEPTH] == 0xff && log[EMC_MAX_DEPTH + 1] == 0xff)
corrupt_dive = 1; corrupt_dive = 1;

View file

@ -601,13 +601,10 @@ static char *str_printf(const char *fmt, ...)
*/ */
static uint32_t calculate_diveid(const unsigned char *fingerprint, unsigned int fsize) static uint32_t calculate_diveid(const unsigned char *fingerprint, unsigned int fsize)
{ {
uint32_t csum[5];
if (!fingerprint || !fsize) if (!fingerprint || !fsize)
return 0; return 0;
SHA1(fingerprint, fsize, (unsigned char *)csum); return SHA1_uint32(fingerprint, fsize);
return csum[0];
} }
uint32_t calculate_string_hash(const char *str) uint32_t calculate_string_hash(const char *str)

View file

@ -298,3 +298,15 @@ void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)
for (i = 0; i < 5; i++) for (i = 0; i < 5; i++)
put_be32(hashout + i * 4, ctx->H[i]); put_be32(hashout + i * 4, ctx->H[i]);
} }
uint32_t SHA1_uint32(const void *dataIn, unsigned long len)
{
uint32_t hashout[5];
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, dataIn, len);
SHA1_Final((unsigned char *)hashout, &ctx);
return hashout[0];
}

View file

@ -29,15 +29,8 @@ void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx);
#define SHA1_Update blk_SHA1_Update #define SHA1_Update blk_SHA1_Update
#define SHA1_Final blk_SHA1_Final #define SHA1_Final blk_SHA1_Final
/* Trivial helper function */ /* Helper function that calculates an SHA1 has and returns the first 4 bytes as uint32_t */
static inline void SHA1(const void *dataIn, unsigned long len, unsigned char hashout[20]) uint32_t SHA1_uint32(const void *dataIn, unsigned long len);
{
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, dataIn, len);
SHA1_Final(hashout, &ctx);
}
#ifdef __cplusplus #ifdef __cplusplus
} }