mirror of
https://github.com/subsurface/subsurface.git
synced 2024-12-05 00:21:29 +00:00
core: C++-ify SHA1 interface
All callers of the SHA1 code are C++. Might just as well use a C++ like interface. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
16137a85af
commit
93b151a75f
6 changed files with 71 additions and 78 deletions
|
@ -91,7 +91,7 @@ SOURCES += subsurface-mobile-main.cpp \
|
||||||
core/gas.c \
|
core/gas.c \
|
||||||
core/membuffer.cpp \
|
core/membuffer.cpp \
|
||||||
core/selection.cpp \
|
core/selection.cpp \
|
||||||
core/sha1.c \
|
core/sha1.cpp \
|
||||||
core/string-format.cpp \
|
core/string-format.cpp \
|
||||||
core/strtod.cpp \
|
core/strtod.cpp \
|
||||||
core/tag.cpp \
|
core/tag.cpp \
|
||||||
|
|
|
@ -162,7 +162,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
|
||||||
save-xml.cpp
|
save-xml.cpp
|
||||||
selection.cpp
|
selection.cpp
|
||||||
selection.h
|
selection.h
|
||||||
sha1.c
|
sha1.cpp
|
||||||
sha1.h
|
sha1.h
|
||||||
ssrf.h
|
ssrf.h
|
||||||
statistics.c
|
statistics.c
|
||||||
|
|
|
@ -138,18 +138,14 @@ extern "C" int add_dive_site_to_table(struct dive_site *ds, struct dive_site_tab
|
||||||
/* If the site doesn't yet have an UUID, create a new one.
|
/* If the site doesn't yet have an UUID, create a new one.
|
||||||
* Make this deterministic for testing. */
|
* Make this deterministic for testing. */
|
||||||
if (!ds->uuid) {
|
if (!ds->uuid) {
|
||||||
SHA_CTX ctx;
|
SHA1 sha;
|
||||||
uint32_t csum[5];
|
|
||||||
|
|
||||||
SHA1_Init(&ctx);
|
|
||||||
if (ds->name)
|
if (ds->name)
|
||||||
SHA1_Update(&ctx, ds->name, strlen(ds->name));
|
sha.update(ds->name, strlen(ds->name));
|
||||||
if (ds->description)
|
if (ds->description)
|
||||||
SHA1_Update(&ctx, ds->description, strlen(ds->description));
|
sha.update(ds->description, strlen(ds->description));
|
||||||
if (ds->notes)
|
if (ds->notes)
|
||||||
SHA1_Update(&ctx, ds->notes, strlen(ds->notes));
|
sha.update(ds->notes, strlen(ds->notes));
|
||||||
SHA1_Final((unsigned char *)csum, &ctx);
|
ds->uuid = sha.hash_uint32();
|
||||||
ds->uuid = csum[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Take care to never have the same uuid twice. This could happen on
|
/* Take care to never have the same uuid twice. This could happen on
|
||||||
|
|
|
@ -135,9 +135,6 @@ std::string normalize_cloud_name(const std::string &remote_in)
|
||||||
|
|
||||||
std::string get_local_dir(const std::string &url, const std::string &branch)
|
std::string get_local_dir(const std::string &url, const std::string &branch)
|
||||||
{
|
{
|
||||||
SHA_CTX ctx;
|
|
||||||
unsigned char hash[20];
|
|
||||||
|
|
||||||
// this optimization could in theory lead to odd things happening if the
|
// this optimization could in theory lead to odd things happening if the
|
||||||
// cloud backend servers ever get out of sync - but when a user switches
|
// cloud backend servers ever get out of sync - but when a user switches
|
||||||
// between those servers (either because one is down, or because the algorithm
|
// between those servers (either because one is down, or because the algorithm
|
||||||
|
@ -148,11 +145,11 @@ std::string get_local_dir(const std::string &url, const std::string &branch)
|
||||||
|
|
||||||
// That zero-byte update is so that we don't get hash
|
// That zero-byte update is so that we don't get hash
|
||||||
// collisions for "repo1 branch" vs "repo 1branch".
|
// collisions for "repo1 branch" vs "repo 1branch".
|
||||||
SHA1_Init(&ctx);
|
SHA1 sha;
|
||||||
SHA1_Update(&ctx, remote.c_str(), remote.size());
|
sha.update(remote);
|
||||||
SHA1_Update(&ctx, "", 1);
|
sha.update("", 1);
|
||||||
SHA1_Update(&ctx, branch.c_str(), branch.size());
|
sha.update(branch);
|
||||||
SHA1_Final(hash, &ctx);
|
auto hash = sha.hash();
|
||||||
return format_string_std("%s/cloudstorage/%02x%02x%02x%02x%02x%02x%02x%02x",
|
return format_string_std("%s/cloudstorage/%02x%02x%02x%02x%02x%02x%02x%02x",
|
||||||
system_default_directory(),
|
system_default_directory(),
|
||||||
hash[0], hash[1], hash[2], hash[3],
|
hash[0], hash[1], hash[2], hash[3],
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
/* this is only to get definitions for memcpy(), ntohl() and htonl() */
|
/* this is only to get definitions for memcpy(), ntohl() and htonl() */
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#else
|
#else
|
||||||
|
@ -132,16 +131,16 @@
|
||||||
#define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B &C) + (D &(B ^ C))), 0x8f1bbcdc, A, B, C, D, E)
|
#define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B &C) + (D &(B ^ C))), 0x8f1bbcdc, A, B, C, D, E)
|
||||||
#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B ^ C ^ D), 0xca62c1d6, A, B, C, D, E)
|
#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B ^ C ^ D), 0xca62c1d6, A, B, C, D, E)
|
||||||
|
|
||||||
static void blk_SHA1_Block(blk_SHA_CTX *ctx, const void *block)
|
static void blk_SHA1_Block(unsigned int H[], const void *block)
|
||||||
{
|
{
|
||||||
unsigned int A, B, C, D, E;
|
unsigned int A, B, C, D, E;
|
||||||
unsigned int array[16];
|
unsigned int array[16];
|
||||||
|
|
||||||
A = ctx->H[0];
|
A = H[0];
|
||||||
B = ctx->H[1];
|
B = H[1];
|
||||||
C = ctx->H[2];
|
C = H[2];
|
||||||
D = ctx->H[3];
|
D = H[3];
|
||||||
E = ctx->H[4];
|
E = H[4];
|
||||||
|
|
||||||
/* Round 1 - iterations 0-16 take their input from 'block' */
|
/* Round 1 - iterations 0-16 take their input from 'block' */
|
||||||
T_0_15(0, A, B, C, D, E);
|
T_0_15(0, A, B, C, D, E);
|
||||||
|
@ -233,80 +232,84 @@ static void blk_SHA1_Block(blk_SHA_CTX *ctx, const void *block)
|
||||||
T_60_79(78, C, D, E, A, B);
|
T_60_79(78, C, D, E, A, B);
|
||||||
T_60_79(79, B, C, D, E, A);
|
T_60_79(79, B, C, D, E, A);
|
||||||
|
|
||||||
ctx->H[0] += A;
|
H[0] += A;
|
||||||
ctx->H[1] += B;
|
H[1] += B;
|
||||||
ctx->H[2] += C;
|
H[2] += C;
|
||||||
ctx->H[3] += D;
|
H[3] += D;
|
||||||
ctx->H[4] += E;
|
H[4] += E;
|
||||||
}
|
}
|
||||||
|
|
||||||
void blk_SHA1_Init(blk_SHA_CTX *ctx)
|
SHA1::SHA1() :
|
||||||
{
|
size(0),
|
||||||
ctx->size = 0;
|
|
||||||
|
|
||||||
/* Initialize H with the magic constants (see FIPS180 for constants) */
|
/* Initialize H with the magic constants (see FIPS180 for constants) */
|
||||||
ctx->H[0] = 0x67452301;
|
H { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 }
|
||||||
ctx->H[1] = 0xefcdab89;
|
{
|
||||||
ctx->H[2] = 0x98badcfe;
|
|
||||||
ctx->H[3] = 0x10325476;
|
|
||||||
ctx->H[4] = 0xc3d2e1f0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
|
void SHA1::update(const void *data, unsigned long len)
|
||||||
{
|
{
|
||||||
unsigned int lenW = ctx->size & 63;
|
unsigned int lenW = size & 63;
|
||||||
|
|
||||||
ctx->size += len;
|
size += len;
|
||||||
|
|
||||||
/* Read the data into W and process blocks as they get full */
|
/* Read the data into W and process blocks as they get full */
|
||||||
if (lenW) {
|
if (lenW) {
|
||||||
unsigned int left = 64 - lenW;
|
unsigned int left = 64 - lenW;
|
||||||
if (len < left)
|
if (len < left)
|
||||||
left = len;
|
left = len;
|
||||||
memcpy(lenW + (char *)ctx->W, data, left);
|
memcpy(lenW + (char *)W, data, left);
|
||||||
lenW = (lenW + left) & 63;
|
lenW = (lenW + left) & 63;
|
||||||
len -= left;
|
len -= left;
|
||||||
data = ((const char *)data + left);
|
data = ((const char *)data + left);
|
||||||
if (lenW)
|
if (lenW)
|
||||||
return;
|
return;
|
||||||
blk_SHA1_Block(ctx, ctx->W);
|
blk_SHA1_Block(H, W);
|
||||||
}
|
}
|
||||||
while (len >= 64) {
|
while (len >= 64) {
|
||||||
blk_SHA1_Block(ctx, data);
|
blk_SHA1_Block(H, data);
|
||||||
data = ((const char *)data + 64);
|
data = ((const char *)data + 64);
|
||||||
len -= 64;
|
len -= 64;
|
||||||
}
|
}
|
||||||
if (len)
|
if (len)
|
||||||
memcpy(ctx->W, data, len);
|
memcpy(W, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)
|
void SHA1::update(const std::string &s)
|
||||||
{
|
{
|
||||||
|
update(s.data(), s.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::array<unsigned char, 20> SHA1::hash()
|
||||||
|
{
|
||||||
|
std::array<unsigned char, 20> hashout;
|
||||||
static const unsigned char pad[64] = { 0x80 };
|
static const unsigned char pad[64] = { 0x80 };
|
||||||
unsigned int padlen[2];
|
unsigned int padlen[2];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* Pad with a binary 1 (ie 0x80), then zeroes, then length */
|
/* Pad with a binary 1 (ie 0x80), then zeroes, then length */
|
||||||
padlen[0] = htonl((uint32_t)(ctx->size >> 29));
|
padlen[0] = htonl((uint32_t)(size >> 29));
|
||||||
padlen[1] = htonl((uint32_t)(ctx->size << 3));
|
padlen[1] = htonl((uint32_t)(size << 3));
|
||||||
|
|
||||||
i = ctx->size & 63;
|
i = size & 63;
|
||||||
blk_SHA1_Update(ctx, pad, 1 + (63 & (55 - i)));
|
update(pad, 1 + (63 & (55 - i)));
|
||||||
blk_SHA1_Update(ctx, padlen, 8);
|
update(padlen, 8);
|
||||||
|
|
||||||
/* Output hash */
|
/* Output hash */
|
||||||
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], H[i]);
|
||||||
|
return hashout;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t SHA1::hash_uint32()
|
||||||
|
{
|
||||||
|
auto hashout = hash();
|
||||||
|
return (hashout[0] << 0) | (hashout[1] << 8) |
|
||||||
|
(hashout[2] << 16) | (hashout[3] << 24);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t SHA1_uint32(const void *dataIn, unsigned long len)
|
uint32_t SHA1_uint32(const void *dataIn, unsigned long len)
|
||||||
{
|
{
|
||||||
uint32_t hashout[5];
|
SHA1 sha;
|
||||||
SHA_CTX ctx;
|
sha.update(dataIn, len);
|
||||||
|
return sha.hash_uint32();
|
||||||
SHA1_Init(&ctx);
|
|
||||||
SHA1_Update(&ctx, dataIn, len);
|
|
||||||
SHA1_Final((unsigned char *)hashout, &ctx);
|
|
||||||
|
|
||||||
return hashout[0];
|
|
||||||
}
|
}
|
29
core/sha1.h
29
core/sha1.h
|
@ -9,31 +9,28 @@
|
||||||
#define SHA1_H
|
#define SHA1_H
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef struct
|
#include <array>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct SHA1
|
||||||
{
|
{
|
||||||
|
SHA1();
|
||||||
|
void update(const void *dataIn, unsigned long len);
|
||||||
|
void update(const std::string &s);
|
||||||
|
// Note: the hash() functions change state. Call only once.
|
||||||
|
std::array<unsigned char, 20> hash();
|
||||||
|
uint32_t hash_uint32(); // Return first 4 bytes of hash interpreted
|
||||||
|
// as little-endian unsigned integer.
|
||||||
|
private:
|
||||||
unsigned long long size;
|
unsigned long long size;
|
||||||
unsigned int H[5];
|
unsigned int H[5];
|
||||||
unsigned int W[16];
|
unsigned int W[16];
|
||||||
} blk_SHA_CTX;
|
};
|
||||||
|
|
||||||
void blk_SHA1_Init(blk_SHA_CTX *ctx);
|
|
||||||
void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *dataIn, unsigned long len);
|
|
||||||
void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx);
|
|
||||||
|
|
||||||
/* Make us use the standard names */
|
|
||||||
#define SHA_CTX blk_SHA_CTX
|
|
||||||
#define SHA1_Init blk_SHA1_Init
|
|
||||||
#define SHA1_Update blk_SHA1_Update
|
|
||||||
#define SHA1_Final blk_SHA1_Final
|
|
||||||
|
|
||||||
/* Helper function that calculates an SHA1 has and returns the first 4 bytes as uint32_t */
|
/* Helper function that calculates an SHA1 has and returns the first 4 bytes as uint32_t */
|
||||||
uint32_t SHA1_uint32(const void *dataIn, unsigned long len);
|
uint32_t SHA1_uint32(const void *dataIn, unsigned long len);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // SHA1_H
|
#endif // SHA1_H
|
||||||
|
|
Loading…
Reference in a new issue