Source file src/cmd/internal/hash/hash.go
1 // Copyright 2024 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 hash implements hash functions used in the compiler toolchain. 6 package hash 7 8 import ( 9 "crypto/md5" 10 "crypto/sha1" 11 "crypto/sha256" 12 "hash" 13 ) 14 15 const ( 16 // Size32 is the size of 32 bytes hash checksum. 17 Size32 = sha256.Size 18 // Size20 is the size of 20 bytes hash checksum. 19 Size20 = sha1.Size 20 // Size16 is the size of 16 bytes hash checksum. 21 Size16 = md5.Size 22 ) 23 24 // New32 returns a new [hash.Hash] computing the 32 bytes hash checksum. 25 func New32() hash.Hash { 26 h := sha256.New() 27 _, _ = h.Write([]byte{1}) // make this hash different from sha256 28 return h 29 } 30 31 // New20 returns a new [hash.Hash] computing the 20 bytes hash checksum. 32 func New20() hash.Hash { 33 return sha1.New() 34 } 35 36 // New16 returns a new [hash.Hash] computing the 16 bytes hash checksum. 37 func New16() hash.Hash { 38 return md5.New() 39 } 40 41 // Sum32 returns the 32 bytes checksum of the data. 42 func Sum32(data []byte) [Size32]byte { 43 sum := sha256.Sum256(data) 44 sum[0] ^= 1 // make this hash different from sha256 45 return sum 46 } 47 48 // Sum20 returns the 20 bytes checksum of the data. 49 func Sum20(data []byte) [Size20]byte { 50 return sha1.Sum(data) 51 } 52 53 // Sum16 returns the 16 bytes checksum of the data. 54 func Sum16(data []byte) [Size16]byte { 55 return md5.Sum(data) 56 } 57