From f76b95636a2c60e52fafbbcae7d3631ca1503a67 Mon Sep 17 00:00:00 2001 From: deluan Date: Tue, 24 Feb 2026 14:10:26 -0500 Subject: [PATCH] refactor: replace MD5 hash with FNV-1a and remove regex dependency to reduce ndp size --- spotify.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/spotify.go b/spotify.go index 11ef8a8..0bc135a 100644 --- a/spotify.go +++ b/spotify.go @@ -1,12 +1,9 @@ package main import ( - "crypto/md5" - "encoding/hex" "encoding/json" "fmt" "net/url" - "regexp" "strings" "github.com/navidrome/navidrome/plugins/pdk/go/host" @@ -14,10 +11,16 @@ import ( "github.com/navidrome/navidrome/plugins/pdk/go/scrobbler" ) -// hashKey returns a hex-encoded MD5 hash of s, for use as a cache key suffix. +// hashKey returns a hex-encoded FNV-1a hash of s, for use as a cache key suffix. func hashKey(s string) string { - h := md5.Sum([]byte(s)) - return hex.EncodeToString(h[:]) + const offset64 uint64 = 14695981039346656037 + const prime64 uint64 = 1099511628211 + h := offset64 + for i := 0; i < len(s); i++ { + h ^= uint64(s[i]) + h *= prime64 + } + return fmt.Sprintf("%016x", h) } const ( @@ -115,11 +118,18 @@ func parseSpotifyID(body []byte) string { return "" } -// isValidSpotifyID checks that a Spotify track ID contains only base-62 characters. -var spotifyIDRegex = regexp.MustCompile(`^[0-9A-Za-z]+$`) - +// isValidSpotifyID checks that a Spotify track ID is non-empty and contains only base-62 characters. func isValidSpotifyID(id string) bool { - return spotifyIDRegex.MatchString(id) + if len(id) == 0 { + return false + } + for i := 0; i < len(id); i++ { + c := id[i] + if !((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { + return false + } + } + return true } // resolveSpotifyURL resolves a direct Spotify track URL via ListenBrainz Labs,