golang.org/x/tools/gopls@v0.15.3/internal/file/hash.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package file
     6  
     7  import (
     8  	"crypto/sha256"
     9  	"fmt"
    10  )
    11  
    12  // A Hash is a cryptographic digest of the contents of a file.
    13  // (Although at 32B it is larger than a 16B string header, it is smaller
    14  // and has better locality than the string header + 64B of hex digits.)
    15  type Hash [sha256.Size]byte
    16  
    17  // HashOf returns the hash of some data.
    18  func HashOf(data []byte) Hash {
    19  	return Hash(sha256.Sum256(data))
    20  }
    21  
    22  // String returns the digest as a string of hex digits.
    23  func (h Hash) String() string {
    24  	return fmt.Sprintf("%64x", [sha256.Size]byte(h))
    25  }
    26  
    27  // XORWith updates *h to *h XOR h2.
    28  func (h *Hash) XORWith(h2 Hash) {
    29  	// Small enough that we don't need crypto/subtle.XORBytes.
    30  	for i := range h {
    31  		h[i] ^= h2[i]
    32  	}
    33  }