diff --git a/include/libantelope/hash.hpp b/include/libantelope/hash.hpp index 4ad57d7..67dd698 100644 --- a/include/libantelope/hash.hpp +++ b/include/libantelope/hash.hpp @@ -25,6 +25,7 @@ #define LIBANTELOPE_HASH_H #include +#include namespace libantelope { @@ -34,6 +35,26 @@ namespace libantelope { typedef unsigned char ripemd160_t[20]; typedef unsigned char sha256_t[32]; +typedef internal::sha256_state sha256_ctx_t; +typedef internal::ripemd160_state ripemd160_ctx_t; + +/** + * Initialize a sha256_ctx_t structure + */ +int sha256_init(sha256_ctx_t* ctx); + +/** + * Update the sha256 hash value with the contents in `data` up to `len` bytes. + * This can be called repeatedly to hash chunks of data. + */ +int sha256_update(sha256_ctx_t* ctx, const void *data, std::size_t len); + +/** + * Place the message digest in out variable. + * The ctx's internal state is reset after this operation. + */ +int sha256_final(sha256_ctx_t* ctx, sha256_t* out); + /** * sha256 hashing function. * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. @@ -48,6 +69,23 @@ sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out); */ sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out); +/** + * Initialize a ripmemd160_ctx_t structure + */ +int ripemd160_init(ripemd160_ctx_t* ctx); + +/** + * Update the RipeMD160 hash value with the contents in `data` up to `len` bytes. + * This can be called repeatedly to hash chunks of data. + */ +int ripemd160_update(ripemd160_ctx_t* ctx, const void *data, std::size_t len); + +/** + * Places the RipeMD160 message digest in out variable. + * The ctx's internal state is reset after this operation. + */ +int ripemd160_final(ripemd160_ctx_t* ctx, ripemd160_t* out); + /** * RipeMD160 hashing function. * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. diff --git a/src/openssl/hash.cpp b/src/openssl/hash.cpp index 492e23a..1fa07b3 100644 --- a/src/openssl/hash.cpp +++ b/src/openssl/hash.cpp @@ -27,6 +27,18 @@ namespace libantelope { +int sha256_init(struct sha256_ctx* ctx) { + return SHA256_Init((SHA256_CTX*)ctx); +} + +int sha256_update(sha256_ctx_t* ctx, const void *data, std::size_t len) { + return SHA256_Update((SHA256_CTX*)ctx, data, len); +} + +int sha256_final(sha256_ctx_t* ctx, sha256_t* out) { + return SHA256_Final((unsigned char*) out, (SHA256_CTX*)ctx); +} + sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out) { return (sha256_t *) SHA256(data, len, (unsigned char*) out); } @@ -36,6 +48,18 @@ sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out) { return (sha256_t *) SHA256((unsigned char*) out, 32, (unsigned char*) out); } +int ripemd160_init(ripemd160_ctx_t* ctx) { + return RIPEMD160_Init((RIPEMD160_CTX*)ctx); +} + +int ripemd160_update(ripemd160_ctx_t* ctx, const void *data, std::size_t len) { + return RIPEMD160_Update((RIPEMD160_CTX*)ctx, data, len); +} + +int ripemd160_final(ripemd160_ctx_t* ctx, ripemd160_t* out) { + return RIPEMD160_Final((unsigned char*) out, (RIPEMD160_CTX*)ctx); +} + ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out) { return (ripemd160_t *) RIPEMD160(data, len, (unsigned char*) out); }