go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/caching/cache/hash.go (about)

     1  // Copyright 2021 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cache
    16  
    17  import (
    18  	"crypto"
    19  	"encoding/hex"
    20  	"hash"
    21  	"io"
    22  )
    23  
    24  // HexDigest is the hash of a file that is hex-encoded. Only lower case letters
    25  // are accepted.
    26  type HexDigest string
    27  
    28  // Validate returns true if the hash is valid.
    29  func (d HexDigest) Validate(h crypto.Hash) bool {
    30  	if l := h.Size() * 2; len(d) != l {
    31  		return false
    32  	}
    33  	for _, c := range d {
    34  		if ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') {
    35  			continue
    36  		}
    37  		return false
    38  	}
    39  	return true
    40  }
    41  
    42  // HexDigests is a slice of HexDigest that implements sort.Interface.
    43  type HexDigests []HexDigest
    44  
    45  func (h HexDigests) Len() int           { return len(h) }
    46  func (h HexDigests) Less(i, j int) bool { return h[i] < h[j] }
    47  func (h HexDigests) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
    48  
    49  // Sum is a shortcut to get a HexDigest from a hash.Hash.
    50  func Sum(h hash.Hash) HexDigest {
    51  	return HexDigest(hex.EncodeToString(h.Sum(nil)))
    52  }
    53  
    54  // Hash hashes a reader and returns a HexDigest from it.
    55  func Hash(h crypto.Hash, src io.Reader) (HexDigest, error) {
    56  	a := h.New()
    57  	_, err := io.Copy(a, src)
    58  	if err != nil {
    59  		return "", err
    60  	}
    61  	return Sum(a), nil
    62  }
    63  
    64  // HashBytes hashes content and returns a HexDigest from it.
    65  func HashBytes(h crypto.Hash, content []byte) HexDigest {
    66  	a := h.New()
    67  	_, _ = a.Write(content)
    68  	return Sum(a)
    69  }