mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 09:21:14 -06:00
36 lines
661 B
Go
36 lines
661 B
Go
package hashcode
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"hash/crc32"
|
|
)
|
|
|
|
// String hashes a string to a unique hashcode.
|
|
//
|
|
// crc32 returns a uint32, but for our use we need
|
|
// and non negative integer. Here we cast to an integer
|
|
// and invert it if the result is negative.
|
|
func String(s string) int {
|
|
v := int(crc32.ChecksumIEEE([]byte(s)))
|
|
if v >= 0 {
|
|
return v
|
|
}
|
|
if -v >= 0 {
|
|
return -v
|
|
}
|
|
// v == MinInt
|
|
return 0
|
|
}
|
|
|
|
// Strings hashes a list of strings to a unique hashcode.
|
|
func Strings(strings []string) string {
|
|
var buf bytes.Buffer
|
|
|
|
for _, s := range strings {
|
|
buf.WriteString(fmt.Sprintf("%s-", s))
|
|
}
|
|
|
|
return fmt.Sprintf("%d", String(buf.String()))
|
|
}
|