github.com/hashicorp/terraform-plugin-sdk@v1.17.2/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  // Deprecated: This will be removed in v2 without replacement. If you need
    12  // its functionality, you can copy it, import crc32 directly, or reference the
    13  // v1 package.
    14  //
    15  // crc32 returns a uint32, but for our use we need
    16  // and non negative integer. Here we cast to an integer
    17  // and invert it if the result is negative.
    18  func String(s string) int {
    19  	v := int(crc32.ChecksumIEEE([]byte(s)))
    20  	if v >= 0 {
    21  		return v
    22  	}
    23  	if -v >= 0 {
    24  		return -v
    25  	}
    26  	// v == MinInt
    27  	return 0
    28  }
    29  
    30  // Strings hashes a list of strings to a unique hashcode.
    31  //
    32  // Deprecated: This will be removed in v2 without replacement. If you need
    33  // its functionality, you can copy it, import crc32 directly, or reference the
    34  // v1 package.
    35  func Strings(strings []string) string {
    36  	var buf bytes.Buffer
    37  
    38  	for _, s := range strings {
    39  		buf.WriteString(fmt.Sprintf("%s-", s))
    40  	}
    41  
    42  	return fmt.Sprintf("%d", String(buf.String()))
    43  }