go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/checksums/checksum.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package checksums 5 6 import ( 7 "encoding/base64" 8 "encoding/binary" 9 10 "github.com/segmentio/fasthash/fnv1a" 11 ) 12 13 // Fast checksums 14 type Fast uint64 15 16 // New is the default starting checksum 17 const New = Fast(fnv1a.Init64) 18 19 // Add a string to a fast checksum 20 func (f Fast) Add(s string) Fast { 21 return Fast(fnv1a.AddString64(uint64(f), s)) 22 } 23 24 // Add an integer to a fast checksum 25 func (f Fast) AddUint(u uint64) Fast { 26 return Fast(fnv1a.AddUint64(uint64(f), u)) 27 } 28 29 // String returns a safe string representation of the checksum 30 func (f Fast) String() string { 31 b := make([]byte, 8) 32 binary.LittleEndian.PutUint64(b, uint64(f)) 33 return base64.StdEncoding.EncodeToString(b) 34 } 35 36 // FastList returns the fast checksum as a string of a list of input strings 37 func FastList(strings ...string) string { 38 checksum := New 39 for i := range strings { 40 checksum = checksum.Add(strings[i]) 41 } 42 return checksum.String() 43 }