hash: add fnv1a algorithm (#6186)
This commit is contained in:
parent
873524ae06
commit
85cbb92b22
6 changed files with 120 additions and 1 deletions
|
|
@ -28,6 +28,7 @@ hash_plugins_list = [
|
|||
'fletcher16',
|
||||
'fletcher32',
|
||||
'fletcher64',
|
||||
'fnv1a',
|
||||
'adler32',
|
||||
'mod255',
|
||||
'crca_crc8smbus',
|
||||
|
|
@ -105,6 +106,7 @@ rz_hash_sources = [
|
|||
'p/algo_crca.c',
|
||||
'p/algo_adler32.c',
|
||||
'p/algo_fletcher.c',
|
||||
'p/algo_fnv1a.c',
|
||||
'p/algo_blake2b.c',
|
||||
'p/algo_blake2bp.c',
|
||||
'p/algo_blake2xb.c',
|
||||
|
|
|
|||
110
librz/hash/p/algo_fnv1a.c
Normal file
110
librz/hash/p/algo_fnv1a.c
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
// SPDX-FileCopyrightText: 2026 cheese-cakee
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
|
||||
#include <rz_hash.h>
|
||||
#include <rz_util/rz_assert.h>
|
||||
|
||||
#include <rz_lib.h>
|
||||
|
||||
typedef ut32 RzFnv1a;
|
||||
|
||||
#define RZ_HASH_FNV1A_DIGEST_SIZE 4
|
||||
#define RZ_HASH_FNV1A_BLOCK_LENGTH 0
|
||||
|
||||
#define RZ_HASH_FNV1A_OFFSET_BASIS 0x811c9dc5U
|
||||
#define RZ_HASH_FNV1A_PRIME 0x01000193U
|
||||
|
||||
static void *plugin_fnv1a_context_new() {
|
||||
return RZ_NEW0(RzFnv1a);
|
||||
}
|
||||
|
||||
static void plugin_fnv1a_context_free(void *context) {
|
||||
free(context);
|
||||
}
|
||||
|
||||
static RzHashSize plugin_fnv1a_digest_size(void *context) {
|
||||
return RZ_HASH_FNV1A_DIGEST_SIZE;
|
||||
}
|
||||
|
||||
static RzHashSize plugin_fnv1a_block_size(void *context) {
|
||||
return RZ_HASH_FNV1A_BLOCK_LENGTH;
|
||||
}
|
||||
|
||||
static bool plugin_fnv1a_init(void *context) {
|
||||
rz_return_val_if_fail(context, false);
|
||||
|
||||
RzFnv1a *ctx = (RzFnv1a *)context;
|
||||
*ctx = RZ_HASH_FNV1A_OFFSET_BASIS;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_fnv1a_update(void *context, const ut8 *data, ut64 size) {
|
||||
rz_return_val_if_fail(context, false);
|
||||
if (size > 0) {
|
||||
rz_return_val_if_fail(data, false);
|
||||
}
|
||||
|
||||
RzFnv1a *ctx = (RzFnv1a *)context;
|
||||
ut32 hash = *ctx;
|
||||
for (ut64 i = 0; i < size; ++i) {
|
||||
hash ^= data[i];
|
||||
hash *= RZ_HASH_FNV1A_PRIME;
|
||||
}
|
||||
*ctx = hash;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_fnv1a_final(void *context, ut8 *digest) {
|
||||
rz_return_val_if_fail(context && digest, false);
|
||||
|
||||
RzFnv1a *ctx = (RzFnv1a *)context;
|
||||
rz_write_be32(digest, *ctx);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_fnv1a_small_block(const ut8 *data, ut64 size, ut8 **digest, RzHashSize *digest_size) {
|
||||
rz_return_val_if_fail(digest, false);
|
||||
if (size > 0) {
|
||||
rz_return_val_if_fail(data, false);
|
||||
}
|
||||
|
||||
ut8 *dgst = malloc(RZ_HASH_FNV1A_DIGEST_SIZE);
|
||||
if (!dgst) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RzFnv1a ctx = { 0 };
|
||||
plugin_fnv1a_init(&ctx);
|
||||
plugin_fnv1a_update(&ctx, data, size);
|
||||
plugin_fnv1a_final(&ctx, dgst);
|
||||
|
||||
*digest = dgst;
|
||||
if (digest_size) {
|
||||
*digest_size = RZ_HASH_FNV1A_DIGEST_SIZE;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
RzHashPlugin rz_hash_plugin_fnv1a = {
|
||||
.name = "fnv1a",
|
||||
.license = "LGPL3",
|
||||
.author = "cheese-cakee",
|
||||
.description = "FNV-1a 32-bit non-cryptographic hash",
|
||||
.support_hmac = false,
|
||||
.context_new = plugin_fnv1a_context_new,
|
||||
.context_free = plugin_fnv1a_context_free,
|
||||
.digest_size = plugin_fnv1a_digest_size,
|
||||
.block_size = plugin_fnv1a_block_size,
|
||||
.init = plugin_fnv1a_init,
|
||||
.update = plugin_fnv1a_update,
|
||||
.final = plugin_fnv1a_final,
|
||||
.small_block = plugin_fnv1a_small_block,
|
||||
};
|
||||
|
||||
#ifndef RZ_PLUGIN_INCORE
|
||||
RZ_API RzLibStruct rizin_plugin = {
|
||||
.type = RZ_LIB_TYPE_HASH,
|
||||
.data = &rz_hash_plugin_fnv1a,
|
||||
.version = RZ_VERSION
|
||||
};
|
||||
#endif
|
||||
|
|
@ -122,6 +122,7 @@ extern RzHashPlugin rz_hash_plugin_fletcher8;
|
|||
extern RzHashPlugin rz_hash_plugin_fletcher16;
|
||||
extern RzHashPlugin rz_hash_plugin_fletcher32;
|
||||
extern RzHashPlugin rz_hash_plugin_fletcher64;
|
||||
extern RzHashPlugin rz_hash_plugin_fnv1a;
|
||||
extern RzHashPlugin rz_hash_plugin_adler32;
|
||||
extern RzHashPlugin rz_hash_plugin_mod255;
|
||||
extern RzHashPlugin rz_hash_plugin_crca_crc8smbus;
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -281,6 +281,7 @@ ____h_ fletcher16 LGPL3 deroad Fletcher16 check
|
|||
____h_ fletcher32 LGPL3 deroad Fletcher32 checksum
|
||||
____h_ fletcher64 LGPL3 deroad Fletcher64 checksum
|
||||
____h_ fletcher8 LGPL3 deroad Fletcher8 checksum
|
||||
____h_ fnv1a LGPL3 cheese-cakee FNV-1a 32-bit non-cryptographic hash
|
||||
____h_ jenkins LGPL3 shessaanand Jenkins non-cryptographic hash
|
||||
____hm keccak-256 BSD-3 Andrey Jivsov KECCAK-256 cryptographic hash
|
||||
____hm keccak-384 BSD-3 Andrey Jivsov KECCAK-384 cryptographic hash
|
||||
|
|
@ -759,6 +760,7 @@ string: 0x00000000-0x00000005 fletcher16: computed hash doesn't match the expect
|
|||
string: 0x00000000-0x00000005 fletcher32: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 fletcher64: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 fletcher8: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 fnv1a: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 jenkins: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 keccak-256: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 keccak-384: computed hash doesn't match the expected one
|
||||
|
|
|
|||
|
|
@ -70,6 +70,7 @@ static hash_data_t hashes_to_test[] = {
|
|||
{ INDATA("password"), .algo = "fletcher16", .expected = "7698" },
|
||||
{ INDATA("password"), .algo = "fletcher32", .expected = "cda87d23" },
|
||||
{ INDATA("password"), .algo = "fletcher64", .expected = "e7d0e5d75732594b" },
|
||||
{ INDATA("password"), .algo = "fnv1a", .expected = "364b5f18" },
|
||||
{ INDATA("password"), .algo = "jenkins", .expected = "08d63509" },
|
||||
{ INDATA("password"), .algo = "adler32", .expected = "7403910f" },
|
||||
{ INDATA("password"), .algo = "crc8smbus", .expected = "4f" },
|
||||
|
|
|
|||
Loading…
Reference in a new issue