-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate.go
123 lines (95 loc) · 2.89 KB
/
validate.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package uuid
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"time"
"unicode"
)
// Check if a string is a valid UUID
func IsValidString(uuid string) error {
newUUID := make([]byte, 0, 32)
for i, r := range uuid {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
newUUID = append(newUUID, byte(r))
continue
}
if r != '-' {
return errors.New("uuid string can only contain alphanumeric characters and hyphens")
}
if i != 8 && i != 13 && i != 18 && i != 23 {
return errors.New("uuid string has incorrect hyphen placement")
}
}
// Check if string is the right length
if len(newUUID) != 32 {
return errors.New("uuid string has incorrect length")
}
buf := make([]byte, 16)
// Decode string
_, err := hex.Decode(buf, newUUID)
if err != nil {
return fmt.Errorf("hexadecimal decoding of uuid string: %w", err)
}
return IsValid(buf)
}
// Check if a byte slice is a valid UUID
func IsValid(uuid []byte) error {
// Check if correct number of bytes are present
if len(uuid) != 16 {
return errors.New("incorrect number of bytes")
}
// Check variant bits
if uuid[8]|0b00111111 != 0b10111111 {
return errors.New("invalid variant bytes")
}
testUUID := UUID(uuid)
switch testUUID.Version() {
case Version4:
return isValidV4(testUUID)
case Version7:
return isValidV7(testUUID)
case Version8:
return isValidV8(testUUID)
default:
return ErrInvalidVersion
}
}
func isValidV4(uuid UUID) error {
uuid[6] &= 0b10111111
uuid[8] &= 0b01111111
if binary.BigEndian.Uint64(uuid[:]) == 0 {
return errors.New("uuid v4 should have non-zero random bits")
}
return nil
}
func isValidV7(uuid UUID) error {
// Right shift timestamp bytes
rightShiftTimestamp(uuid[:8])
extractedTime := time.UnixMilli(int64(binary.BigEndian.Uint64(uuid[:8]))).UTC()
// Reject UUIDs with invalid time
if extractedTime.Before(time.Time{}) || extractedTime.IsZero() || extractedTime.After(time.Now().UTC()) {
return fmt.Errorf("uuid v7 contains invalid timestamp: %s", extractedTime.Format(time.RFC3339Nano))
}
uuid[8] &= 0b01111111
// Check if random bits are filled
if binary.LittleEndian.Uint64(uuid[8:]) == 0 {
return errors.New("uuid v7 should have non-zero random bits")
}
return nil
}
func isValidV8(uuid UUID) error {
uuid[6] |= 0b0111_1111
extractedTime := time.UnixMicro(int64(binary.BigEndian.Uint64(uuid[:8])) / 1000).UTC()
// Reject UUIDs from invalid time
if extractedTime.Before(time.Time{}) || extractedTime.IsZero() || extractedTime.After(time.Now().UTC()) {
return fmt.Errorf("uuid v7 contains invalid timestamp: %s", extractedTime.Format(time.RFC3339Nano))
}
uuid[8] &= 0b01111111
// Check if random bits are filled
if binary.BigEndian.Uint64(uuid[8:]) == 0 {
return errors.New("uuid v8 should have non-zero random bits")
}
return nil
}