diff options
author | Jake Champlin <jake.champlin.27@gmail.com> | 2017-06-06 12:40:07 -0400 |
---|---|---|
committer | Jake Champlin <jake.champlin.27@gmail.com> | 2017-06-06 12:40:07 -0400 |
commit | bae9f6d2fd5eb5bc80929bd393932b23f14d7c93 (patch) | |
tree | ca9ab12a7d78b1fc27a8f734729081357ce6d252 /vendor/github.com/hashicorp/go-uuid/uuid.go | |
parent | 254c495b6bebab3fb72a243c4bce858d79e6ee99 (diff) | |
download | terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.gz terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.zst terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.zip |
Initial transfer of provider code
Diffstat (limited to 'vendor/github.com/hashicorp/go-uuid/uuid.go')
-rw-r--r-- | vendor/github.com/hashicorp/go-uuid/uuid.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/go-uuid/uuid.go b/vendor/github.com/hashicorp/go-uuid/uuid.go new file mode 100644 index 0000000..322b522 --- /dev/null +++ b/vendor/github.com/hashicorp/go-uuid/uuid.go | |||
@@ -0,0 +1,57 @@ | |||
1 | package uuid | ||
2 | |||
3 | import ( | ||
4 | "crypto/rand" | ||
5 | "encoding/hex" | ||
6 | "fmt" | ||
7 | ) | ||
8 | |||
9 | // GenerateUUID is used to generate a random UUID | ||
10 | func GenerateUUID() (string, error) { | ||
11 | buf := make([]byte, 16) | ||
12 | if _, err := rand.Read(buf); err != nil { | ||
13 | return "", fmt.Errorf("failed to read random bytes: %v", err) | ||
14 | } | ||
15 | |||
16 | return FormatUUID(buf) | ||
17 | } | ||
18 | |||
19 | func FormatUUID(buf []byte) (string, error) { | ||
20 | if len(buf) != 16 { | ||
21 | return "", fmt.Errorf("wrong length byte slice (%d)", len(buf)) | ||
22 | } | ||
23 | |||
24 | return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x", | ||
25 | buf[0:4], | ||
26 | buf[4:6], | ||
27 | buf[6:8], | ||
28 | buf[8:10], | ||
29 | buf[10:16]), nil | ||
30 | } | ||
31 | |||
32 | func ParseUUID(uuid string) ([]byte, error) { | ||
33 | if len(uuid) != 36 { | ||
34 | return nil, fmt.Errorf("uuid string is wrong length") | ||
35 | } | ||
36 | |||
37 | hyph := []byte("-") | ||
38 | |||
39 | if uuid[8] != hyph[0] || | ||
40 | uuid[13] != hyph[0] || | ||
41 | uuid[18] != hyph[0] || | ||
42 | uuid[23] != hyph[0] { | ||
43 | return nil, fmt.Errorf("uuid is improperly formatted") | ||
44 | } | ||
45 | |||
46 | hexStr := uuid[0:8] + uuid[9:13] + uuid[14:18] + uuid[19:23] + uuid[24:36] | ||
47 | |||
48 | ret, err := hex.DecodeString(hexStr) | ||
49 | if err != nil { | ||
50 | return nil, err | ||
51 | } | ||
52 | if len(ret) != 16 { | ||
53 | return nil, fmt.Errorf("decoded hex is the wrong length") | ||
54 | } | ||
55 | |||
56 | return ret, nil | ||
57 | } | ||