mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-21 16:22:36 +08:00
fix: make resource fingerprints deterministic
Sort map keys before hashing so Resource.ID is stable.
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"maps"
|
||||||
|
"slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Parser interface {
|
type Parser interface {
|
||||||
@@ -57,14 +59,15 @@ func (r *Resource) ID() string {
|
|||||||
h.Write([]byte(r.Extension))
|
h.Write([]byte(r.Extension))
|
||||||
fmt.Fprintf(h, "%d", r.Size)
|
fmt.Fprintf(h, "%d", r.Size)
|
||||||
|
|
||||||
for k, v := range r.Hash {
|
// Map iteration order is random: sort keys so the fingerprint is stable.
|
||||||
|
for _, k := range slices.Sorted(maps.Keys(r.Hash)) {
|
||||||
h.Write([]byte(k))
|
h.Write([]byte(k))
|
||||||
h.Write([]byte(v))
|
h.Write([]byte(r.Hash[k]))
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range r.Headers {
|
for _, k := range slices.Sorted(maps.Keys(r.Headers)) {
|
||||||
h.Write([]byte(k))
|
h.Write([]byte(k))
|
||||||
h.Write([]byte(v))
|
h.Write([]byte(r.Headers[k]))
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%x", h.Sum(nil))
|
return fmt.Sprintf("%x", h.Sum(nil))
|
||||||
|
|||||||
Reference in New Issue
Block a user