forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
40 lines (35 loc) · 830 Bytes
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package digitalocean
import (
"crypto/sha1"
"encoding/hex"
"hash/crc32"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func HashString(s string) string {
hash := sha1.Sum([]byte(s))
return hex.EncodeToString(hash[:])
}
func HashStringStateFunc() schema.SchemaStateFunc {
return func(v interface{}) string {
switch v.(type) {
case string:
return HashString(v.(string))
default:
return ""
}
}
}
// hashcode.String in the terraform-plugin-sdk was made internal to the SDK in v2.
// Embed the implementation here to allow same hash function to continue to be used
// by the code in this provider that used it for hash computation.
func SDKHashString(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
if -v >= 0 {
return -v
}
// v == MinInt
return 0
}