Handle uppercase hashes properly (case-fold compare or normalise to lowercase)

This commit is contained in:
comp500 2022-06-20 16:58:50 +01:00
parent b8d9727833
commit 0d8c1762a3
2 changed files with 6 additions and 6 deletions

View File

@ -299,7 +299,7 @@ func (c *CacheIndex) getHashesMap(i int) map[string]string {
func (c *CacheIndex) GetHandleFromHash(hashFormat string, hash string) *CacheIndexHandle { func (c *CacheIndex) GetHandleFromHash(hashFormat string, hash string) *CacheIndexHandle {
storedHashFmtList, hasStoredHashFmt := c.Hashes[hashFormat] storedHashFmtList, hasStoredHashFmt := c.Hashes[hashFormat]
if hasStoredHashFmt { if hasStoredHashFmt {
hashIdx := slices.Index(storedHashFmtList, hash) hashIdx := slices.Index(storedHashFmtList, strings.ToLower(hash))
if hashIdx > -1 { if hashIdx > -1 {
return &CacheIndexHandle{ return &CacheIndexHandle{
index: c, index: c,
@ -322,7 +322,7 @@ func (c *CacheIndex) GetHandleFromHashForce(hashFormat string, hash string) (*Ca
c.Hashes[hashFormat] = storedHashFmtList c.Hashes[hashFormat] = storedHashFmtList
// Rehash every file that doesn't have this hash with this hash // Rehash every file that doesn't have this hash with this hash
for hashIdx, curHash := range storedHashFmtList { for hashIdx, curHash := range storedHashFmtList {
if curHash == hash { if strings.EqualFold(curHash, hash) {
return &CacheIndexHandle{ return &CacheIndexHandle{
index: c, index: c,
hashIdx: hashIdx, hashIdx: hashIdx,
@ -334,7 +334,7 @@ func (c *CacheIndex) GetHandleFromHashForce(hashFormat string, hash string) (*Ca
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to rehash %s: %w", c.Hashes[cacheHashFormat][hashIdx], err) return nil, fmt.Errorf("failed to rehash %s: %w", c.Hashes[cacheHashFormat][hashIdx], err)
} }
if storedHashFmtList[hashIdx] == hash { if strings.EqualFold(storedHashFmtList[hashIdx], hash) {
return &CacheIndexHandle{ return &CacheIndexHandle{
index: c, index: c,
hashIdx: hashIdx, hashIdx: hashIdx,
@ -353,7 +353,7 @@ func (c *CacheIndex) GetHandleFromHashForce(hashFormat string, hash string) (*Ca
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to rehash %s: %w", cacheHash, err) return nil, fmt.Errorf("failed to rehash %s: %w", cacheHash, err)
} }
if storedHashFmtList[hashIdx] == hash { if strings.EqualFold(storedHashFmtList[hashIdx], hash) {
return &CacheIndexHandle{ return &CacheIndexHandle{
index: c, index: c,
hashIdx: hashIdx, hashIdx: hashIdx,
@ -399,7 +399,7 @@ func (c *CacheIndex) NewHandleFromHashes(hashes map[string]string) (*CacheIndexH
if handle != nil { if handle != nil {
// Add hashes to handle // Add hashes to handle
for hashFormat2, hash2 := range hashes { for hashFormat2, hash2 := range hashes {
handle.Hashes[hashFormat2] = hash2 handle.Hashes[hashFormat2] = strings.ToLower(hash2)
} }
return handle, true return handle, true
} }

View File

@ -400,7 +400,7 @@ func (in Index) SaveFile(f IndexFile, dest io.Writer) error {
} }
calculatedHash := h.HashToString(h.Sum(nil)) calculatedHash := h.HashToString(h.Sum(nil))
if calculatedHash != f.Hash && !viper.GetBool("no-internal-hashes") { if !strings.EqualFold(calculatedHash, f.Hash) && !viper.GetBool("no-internal-hashes") {
return errors.New("hash of saved file is invalid") return errors.New("hash of saved file is invalid")
} }