common: hoist hash_str helper into its own header.
What changed, and why it matters
This commit is a routine code cleanup: it moves a small string-hashing helper function into a shared header file so it can be reused without being copied and pasted. There is no change to behavior, no bug fix, and no security relevance.
No security action needed. Treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change hoists the existing hash_str (and equivalent hash_acctname) helper, which wraps siphash24(siphash_seed(), str, strlen(str)), into a new common/hash_str.h header. Three existing call sites are updated to include the header and remove their local definitions. The implementation is identical; only code duplication is reduced.
Changed components
common/hash_str.h (new shared header)lightningd/plugin.hplugins/bkpr/account.cplugins/bkpr/onchain_fee.cInspect captured patch +16 / −17
diff --git a/common/Makefile b/common/Makefile
index 69befa16..d6060642 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -121,6 +121,7 @@ COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) \
common/ecdh.h \
common/errcode.h \
common/gossip_constants.h \
+ common/hash_str.h \
common/hsm_version.h \
common/htlc.h \
common/jsonrpc_errors.h \
diff --git a/common/hash_str.h b/common/hash_str.h
new file mode 100644
index 00000000..111073e9
--- /dev/null
+++ b/common/hash_str.h
@@ -0,0 +1,11 @@
+#ifndef LIGHTNING_COMMON_HASH_STR_H
+#define LIGHTNING_COMMON_HASH_STR_H
+#include "config.h"
+#include <common/pseudorand.h>
+
+static inline size_t hash_str(const char *str)
+{
+ return siphash24(siphash_seed(), str, strlen(str));
+}
+
+#endif /* LIGHTNING_COMMON_HASH_STR_H */
diff --git a/common/utils.c b/common/utils.c
index 860cd446..5dcbee5a 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -253,4 +253,3 @@ void *membuf_tal_resize(struct membuf *mb, void *rawelems, size_t newsize)
tal_resize(&p, newsize);
return p;
}
-
diff --git a/lightningd/plugin.h b/lightningd/plugin.h
index b7f03360..5bc0c3af 100644
--- a/lightningd/plugin.h
+++ b/lightningd/plugin.h
@@ -2,6 +2,7 @@
#define LIGHTNING_LIGHTNINGD_PLUGIN_H
#include "config.h"
#include <ccan/intmap/intmap.h>
+#include <common/hash_str.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
@@ -24,11 +25,6 @@ struct plugin_subscription {
const char *topic;
};
-static inline size_t hash_str(const char *str)
-{
- return siphash24(siphash_seed(), str, strlen(str));
-}
-
static inline const char *plugin_subscription_key(const struct plugin_subscription *ps)
{
return ps->topic;
diff --git a/plugins/bkpr/account.c b/plugins/bkpr/account.c
index c8b07468..c01bafef 100644
--- a/plugins/bkpr/account.c
+++ b/plugins/bkpr/account.c
@@ -4,6 +4,7 @@
#include <ccan/json_out/json_out.h>
#include <ccan/str/str.h>
#include <ccan/tal/str/str.h>
+#include <common/hash_str.h>
#include <common/memleak.h>
#include <common/mkdatastorekey.h>
#include <common/node_id.h>
@@ -14,11 +15,6 @@
#include <plugins/libplugin.h>
#include <wire/wire.h>
-static size_t hash_str(const char *str)
-{
- return siphash24(siphash_seed(), str, strlen(str));
-}
-
static const char *account_key(const struct account *account)
{
return account->name;
diff --git a/plugins/bkpr/onchain_fee.c b/plugins/bkpr/onchain_fee.c
index 79b6f15a..8a6cc14b 100644
--- a/plugins/bkpr/onchain_fee.c
+++ b/plugins/bkpr/onchain_fee.c
@@ -6,6 +6,7 @@
#include <ccan/intmap/intmap.h>
#include <ccan/json_out/json_out.h>
#include <ccan/tal/str/str.h>
+#include <common/hash_str.h>
#include <common/json_stream.h>
#include <common/memleak.h>
#include <common/mkdatastorekey.h>
@@ -57,11 +58,6 @@ struct ordered_ofees {
struct onchain_fee **ofs;
};
-static size_t hash_acctname(const char *str)
-{
- return siphash24(siphash_seed(), str, strlen(str));
-}
-
static const char *onchain_fees_keyof(const struct ordered_ofees *ofees)
{
return ofees->ofs[0]->acct_name;
@@ -75,7 +71,7 @@ static bool onchain_account_eq(const struct ordered_ofees *ofees,
HTABLE_DEFINE_NODUPS_TYPE(struct ordered_ofees,
onchain_fees_keyof,
- hash_acctname,
+ hash_str,
onchain_account_eq,
ofees_hash);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.