commit a0a15fea8438ce7fe4a3e3d5eb22b389b29e0703
parent 9d08bbfbdfcb55d26bd3c70433c6a5ad297007b2
Author: vaplv <vaplv@free.fr>
Date: Mon, 14 Sep 2020 15:12:12 +0200
Add and test the hash256_eq function
Diffstat:
3 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/src/hash.c b/src/hash.c
@@ -184,3 +184,9 @@ hash256_to_cstr(const hash256_T hash, char cstr[65])
}
}
+int
+hash256_eq(const hash256_T hash0, const hash256_T hash1)
+{
+ return memcmp(hash0, hash1, sizeof(hash256_T)) == 0;
+}
+
diff --git a/src/hash.h b/src/hash.h
@@ -60,7 +60,7 @@ hash_fnv64(const void* data, const size_t len)
return hash;
}
-BEGIN_DECLS
+BEGIN_DECLS
RSYS_API res_T
hash_sha256
@@ -74,6 +74,11 @@ hash256_to_cstr
(const hash256_T hash,
char cstr[65]);
+RSYS_API int
+hash256_eq
+ (const hash256_T hash0,
+ const hash256_T hash1);
+
END_DECLS
#endif /* HASH_H */
diff --git a/src/test_hash_sha256.c b/src/test_hash_sha256.c
@@ -13,6 +13,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
+#include "endianness.h"
#include "hash.h"
#include "mem_allocator.h"
#include <string.h>
@@ -42,6 +43,7 @@ int
main(int argc, char** argv)
{
char* data = NULL;
+ hash256_T hash0, hash1;
(void)argc, (void)argv;
data = mem_alloc(0x6000003e);
@@ -51,6 +53,17 @@ main(int argc, char** argv)
chk_hash(data, 0,
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+ CHK(hash_sha256(&mem_default_allocator, data, 0, hash0) == RES_OK);
+ ((uint32_t*)hash1)[0] = big_endian_32(0xe3b0c442);
+ ((uint32_t*)hash1)[1] = big_endian_32(0x98fc1c14);
+ ((uint32_t*)hash1)[2] = big_endian_32(0x9afbf4c8);
+ ((uint32_t*)hash1)[3] = big_endian_32(0x996fb924);
+ ((uint32_t*)hash1)[4] = big_endian_32(0x27ae41e4);
+ ((uint32_t*)hash1)[5] = big_endian_32(0x649b934c);
+ ((uint32_t*)hash1)[6] = big_endian_32(0xa495991b);
+ ((uint32_t*)hash1)[7] = big_endian_32(0x7852b855);
+ CHK(hash256_eq(hash0, hash1));
+
sprintf(data, "abc");
chk_hash(data, strlen(data),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");