github.com/svenhamers/terraform@v0.11.12-beta1/helper/hashcode/hashcode.go (about)

     1  package hashcode
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"hash/crc32"
     7  )
     8  
     9  // String hashes a string to a unique hashcode.
    10  //
    11  // crc32 returns a uint32, but for our use we need
    12  // and non negative integer. Here we cast to an integer
    13  // and invert it if the result is negative.
    14  func String(s string) int {
    15  	v := int(crc32.ChecksumIEEE([]byte(s)))
    16  	if v >= 0 {
    17  		return v
    18  	}
    19  	if -v >= 0 {
    20  		return -v
    21  	}
    22  	// v == MinInt
    23  	return 0
    24  }
    25  
    26  // Strings hashes a list of strings to a unique hashcode.
    27  func Strings(strings []string) string {
    28  	var buf bytes.Buffer
    29  
    30  	for _, s := range strings {
    31  		buf.WriteString(fmt.Sprintf("%s-", s))
    32  	}
    33  
    34  	return fmt.Sprintf("%d", String(buf.String()))
    35  }