Skip to content

Commit 92ebb60

Browse files
committed
helper/resource: ok let's actually use RFC4122
1 parent 33de319 commit 92ebb60

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

helper/resource/id.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,30 @@ const UniqueIdPrefix = `terraform-`
1212
// Helper for a resource to generate a unique identifier
1313
//
1414
// This uses a simple RFC 4122 v4 UUID with some basic cosmetic filters
15-
// applied (remove padding, downcase) to help distinguishing visually between
16-
// identifiers.
15+
// applied (base32, remove padding, downcase) to make visually distinguishing
16+
// identifiers easier.
1717
func UniqueId() string {
18-
var uuid [16]byte
19-
rand.Read(uuid[:])
2018
return fmt.Sprintf("%s%s", UniqueIdPrefix,
2119
strings.ToLower(
2220
strings.Replace(
23-
base32.StdEncoding.EncodeToString(uuid[:]),
21+
base32.StdEncoding.EncodeToString(uuidV4()),
2422
"=", "", -1)))
2523
}
24+
25+
func uuidV4() []byte {
26+
var uuid [16]byte
27+
28+
// Set all the other bits to randomly (or pseudo-randomly) chosen
29+
// values.
30+
rand.Read(uuid[:])
31+
32+
// Set the two most significant bits (bits 6 and 7) of the
33+
// clock_seq_hi_and_reserved to zero and one, respectively.
34+
uuid[8] = (uuid[8] | 0x80) & 0x8f
35+
36+
// Set the four most significant bits (bits 12 through 15) of the
37+
// time_hi_and_version field to the 4-bit version number from Section 4.1.3.
38+
uuid[6] = (uuid[6] | 0x40) & 0x4f
39+
40+
return uuid[:]
41+
}

0 commit comments

Comments
 (0)