crypto: Add helper functions for hashing

pull/100/head
Andri Yngvason 2023-09-04 21:40:02 +00:00
parent 9507624cf3
commit d12973486a
2 changed files with 29 additions and 0 deletions

View File

@ -23,6 +23,11 @@ enum crypto_hash_type {
CRYPTO_HASH_SHA1,
};
struct crypto_data_entry {
uint8_t* data;
size_t len;
};
void crypto_dump_base16(const char* msg, const uint8_t* bytes, size_t len);
void crypto_dump_base64(const char* msg, const uint8_t* bytes, size_t len);
@ -73,6 +78,11 @@ void crypto_hash_append(struct crypto_hash* self, const uint8_t* src,
void crypto_hash_digest(struct crypto_hash* self, uint8_t* dst,
size_t len);
void crypto_hash_one(uint8_t* dst, size_t dst_len, enum crypto_hash_type type,
const uint8_t* src, size_t src_len);
void crypto_hash_many(uint8_t* dst, size_t dst_len, enum crypto_hash_type type,
const struct crypto_data_entry *src);
// RSA
struct crypto_rsa_pub_key* crypto_rsa_pub_key_new(void);
void crypto_rsa_pub_key_del(struct crypto_rsa_pub_key*);

View File

@ -471,6 +471,25 @@ void crypto_hash_digest(struct crypto_hash* self, uint8_t* dst, size_t len)
self->digest(&self->ctx, len, dst);
}
void crypto_hash_one(uint8_t* dst, size_t dst_len, enum crypto_hash_type type,
const uint8_t* src, size_t src_len)
{
struct crypto_hash *hash = crypto_hash_new(type);
crypto_hash_append(hash, src, src_len);
crypto_hash_digest(hash, dst, dst_len);
crypto_hash_del(hash);
}
void crypto_hash_many(uint8_t* dst, size_t dst_len, enum crypto_hash_type type,
const struct crypto_data_entry *src)
{
struct crypto_hash *hash = crypto_hash_new(type);
for (int i = 0; src[i].data && src[i].len; ++i)
crypto_hash_append(hash, src[i].data, src[i].len);
crypto_hash_digest(hash, dst, dst_len);
crypto_hash_del(hash);
}
struct crypto_rsa_pub_key *crypto_rsa_pub_key_new(void)
{
struct crypto_rsa_pub_key* self = calloc(1, sizeof(*self));