Skip to content

Commit 6aee0b8

Browse files
committed
feat(yahash): fnv 32-bit
1 parent dd4abaa commit 6aee0b8

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

yahash/yahash.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,25 @@ func FNVStringToInt64(data string, args ...string) int64 {
265265

266266
return int64(hasher.Sum64())
267267
}
268+
269+
// FNVStringToInt32 is a ready‑to‑use 32‑bit FNV‑1a implementation compatible
270+
// with the `HashFunc` signature.
271+
//
272+
// It concatenates *data* and *args* (already salted) and returns the 32‑bit
273+
// digest as a signed integer (`int32`).
274+
//
275+
// # Example
276+
//
277+
// hasher := yahash.NewHash(yahash.FNVStringToInt32, "secret", time.Minute, 3)
278+
// code := hasher.HashWithTime(time.Now(), "ya_args")
279+
// fmt.Println(code)
280+
func FNVStringToInt32(data string, args ...string) int32 {
281+
hasher := fnv.New32()
282+
hasher.Write([]byte(data))
283+
284+
for _, arg := range args {
285+
hasher.Write([]byte(arg))
286+
}
287+
288+
return int32(hasher.Sum32())
289+
}

0 commit comments

Comments
 (0)