From 225a1947ae4948d5228f2378f6a0e956295248a5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 30 May 2023 13:55:07 +0200 Subject: [PATCH] include/libantelope/hash.hpp: split into hash/ripemd160.hpp and hash/sha256.hpp --- include/libantelope/hash.hpp | 72 +------------------------- include/libantelope/hash/ripemd160.hpp | 62 ++++++++++++++++++++++ include/libantelope/hash/sha256.hpp | 69 ++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 70 deletions(-) create mode 100644 include/libantelope/hash/ripemd160.hpp create mode 100644 include/libantelope/hash/sha256.hpp diff --git a/include/libantelope/hash.hpp b/include/libantelope/hash.hpp index 67dd698..a471c67 100644 --- a/include/libantelope/hash.hpp +++ b/include/libantelope/hash.hpp @@ -24,75 +24,7 @@ #ifndef LIBANTELOPE_HASH_H #define LIBANTELOPE_HASH_H -#include -#include - -namespace libantelope { - -/** - * Hashes - */ -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`. - * Returns the same pointer as `out`. - */ -sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out); - -/** - * sha256 double hashing function. - * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. - * Returns the same pointer as `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`. - * Returns the same pointer as `out`. - */ -ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out); - -} // namespace libantelope +#include +#include #endif /* LIBANTELOPE_HASH_H */ diff --git a/include/libantelope/hash/ripemd160.hpp b/include/libantelope/hash/ripemd160.hpp new file mode 100644 index 0000000..45ba4ff --- /dev/null +++ b/include/libantelope/hash/ripemd160.hpp @@ -0,0 +1,62 @@ +/** + * MIT License + * + * Copyright (c) 2019-2023 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef LIBANTELOPE_HASH_RIPEMD160_H +#define LIBANTELOPE_HASH_RIPEMD160_H + +#include +#include + +namespace libantelope { + +typedef unsigned char ripemd160_t[20]; + +typedef internal::ripemd160_state ripemd160_ctx_t; + +/** + * 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`. + * Returns the same pointer as `out`. + */ +ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out); + +} // namespace libantelope + +#endif /* LIBANTELOPE_RIPEMD160_H */ diff --git a/include/libantelope/hash/sha256.hpp b/include/libantelope/hash/sha256.hpp new file mode 100644 index 0000000..3f79d4a --- /dev/null +++ b/include/libantelope/hash/sha256.hpp @@ -0,0 +1,69 @@ +/** + * MIT License + * + * Copyright (c) 2019-2023 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef LIBANTELOPE_HASH_SHA256_H +#define LIBANTELOPE_HASH_SHA256_H + +#include +#include + +namespace libantelope { + +typedef unsigned char sha256_t[32]; + +typedef internal::sha256_state sha256_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`. + * Returns the same pointer as `out`. + */ +sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out); + +/** + * sha256 double hashing function. + * Hashes the content in `data` up to `len` bytes. The result is stored in `out`. + * Returns the same pointer as `out`. + */ +sha256_t* sha256d(const unsigned char *data, std::size_t len, sha256_t* out); + +} // namespace libantelope + +#endif /* LIBANTELOPE_HASH_SHA256_H */