1
1
package unify4g
2
2
3
3
import (
4
+ cr "crypto/rand"
5
+ "encoding/hex"
4
6
"fmt"
7
+ "log"
8
+ "math/rand"
5
9
"os"
10
+ "time"
6
11
)
7
12
8
13
// GenerateUUID generates a new universally unique identifier (UUID) using random data from /dev/urandom (Unix-based systems).
@@ -30,6 +35,37 @@ import (
30
35
// - This function is designed for Unix-based systems. On non-Unix systems, this may not work because /dev/urandom
31
36
// may not be available.
32
37
func GenerateUUID () (string , error ) {
38
+ dash := "-"
39
+ return GenerateUUIDDelimiter (dash )
40
+ }
41
+
42
+ // GenerateUUIDDelimiter generates a new universally unique identifier (UUID) using random data from /dev/urandom
43
+ // (Unix-based systems) with a customizable delimiter.
44
+ //
45
+ // This function is similar to GenerateUUID but allows the user to specify a custom delimiter to separate
46
+ // different sections of the UUID. It opens the special file /dev/urandom to read 16 random bytes,
47
+ // which are then used to construct a UUID. The UUID is returned as a string in the format:
48
+ // XXXXXXXX<delimiter>XXXX<delimiter>XXXX<delimiter>XXXX<delimiter>XXXXXXXXXXXX, where X is a hexadecimal digit.
49
+ //
50
+ // Parameters:
51
+ // - delimiter: A string used to separate sections of the UUID. Common choices are "-" or "" (no delimiter).
52
+ //
53
+ // Returns:
54
+ // - A string representing the newly generated UUID with the specified delimiter.
55
+ // - An error if there is an issue opening or reading from /dev/urandom.
56
+ //
57
+ // Example:
58
+ //
59
+ // uuid, err := GenerateUUIDDelimiter("-")
60
+ // if err != nil {
61
+ // log.Fatalf("Failed to generate UUID: %v", err)
62
+ // }
63
+ // fmt.Println("Generated UUID:", uuid)
64
+ //
65
+ // Notes:
66
+ // - This function is designed for Unix-based systems. On non-Unix systems, it may not work because /dev/urandom
67
+ // may not be available.
68
+ func GenerateUUIDDelimiter (delimiter string ) (string , error ) {
33
69
file , err := os .Open ("/dev/urandom" )
34
70
if err != nil {
35
71
return "" , fmt .Errorf ("open /dev/urandom error:[%v]" , err )
@@ -44,6 +80,94 @@ func GenerateUUID() (string, error) {
44
80
if err != nil {
45
81
return "" , err
46
82
}
47
- uuid := fmt .Sprintf ("%x-%x-%x-%x-%x" , b [0 :4 ], b [4 :6 ], b [6 :8 ], b [8 :10 ], b [10 :])
83
+ // Format the bytes as a UUID string with the specified delimiter.
84
+ // The UUID is structured as XXXXXXXX<delimiter>XXXX<delimiter>XXXX<delimiter>XXXX<delimiter>XXXXXXXXXXXX.
85
+ uuid := fmt .Sprintf ("%x%s%x%s%x%s%x%s%x" , b [0 :4 ], delimiter , b [4 :6 ], delimiter , b [6 :8 ], delimiter , b [8 :10 ], delimiter , b [10 :])
48
86
return uuid , nil
49
87
}
88
+
89
+ // GenerateRandomID generates a random alphanumeric string of the specified length.
90
+ // This string includes uppercase letters, lowercase letters, and numbers, making it
91
+ // suitable for use as unique IDs or tokens.
92
+ //
93
+ // Parameters:
94
+ // - length: The length of the random ID to generate. Must be a positive integer.
95
+ //
96
+ // Returns:
97
+ // - A string of random alphanumeric characters with the specified length.
98
+ //
99
+ // The function uses a custom random source seeded with the current Unix timestamp
100
+ // in nanoseconds to ensure that each call produces a unique sequence.
101
+ // This function is intended to generate random strings quickly and is not
102
+ // cryptographically secure.
103
+ //
104
+ // Example:
105
+ //
106
+ // id := GenerateRandomID(16)
107
+ // fmt.Println("Generated Random ID:", id)
108
+ //
109
+ // Notes:
110
+ // - This function is suitable for use cases where simple random IDs are needed.
111
+ // However, for cryptographic purposes, consider using more secure random generation.
112
+ func GenerateRandomID (length int ) string {
113
+ const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
114
+ seededRand := rand .New (rand .NewSource (time .Now ().UnixNano ())) // Create a seeded random generator for unique results each call
115
+ // Allocate a byte slice for the generated ID and populate it with random characters
116
+ id := make ([]byte , length )
117
+ for i := range id {
118
+ id [i ] = charset [seededRand .Intn (len (charset ))]
119
+ }
120
+ return string (id )
121
+ }
122
+
123
+ // GenerateCryptoID generates a cryptographically secure random ID as a hexadecimal string.
124
+ // It uses 16 random bytes, which are then encoded to a hexadecimal string for easy representation.
125
+ //
126
+ // Returns:
127
+ // - A string representing a secure random hexadecimal ID of 32 characters (since 16 bytes are used, and each byte
128
+ // is represented by two hexadecimal characters).
129
+ //
130
+ // The function uses crypto/rand.Read to ensure cryptographic security in the generated ID, making it suitable for
131
+ // sensitive use cases such as API keys, session tokens, or any security-critical identifiers.
132
+ //
133
+ // Example:
134
+ //
135
+ // id := GenerateCryptoID()
136
+ // fmt.Println("Generated Crypto ID:", id)
137
+ //
138
+ // Notes:
139
+ // - This function is suitable for use cases where high security is required in the generated ID.
140
+ // - It is not recommended for use cases where deterministic or non-cryptographic IDs are preferred.
141
+ func GenerateCryptoID () string {
142
+ bytes := make ([]byte , 16 )
143
+ // Use crypto/rand.Read for cryptographically secure random byte generation.
144
+ if _ , err := cr .Read (bytes ); err != nil {
145
+ log .Fatalf ("Failed to generate secure random bytes: %v" , err )
146
+ return ""
147
+ }
148
+ return hex .EncodeToString (bytes )
149
+ }
150
+
151
+ // GenerateTimestampID generates a unique identifier based on the current Unix timestamp in nanoseconds,
152
+ // with an additional random integer to enhance uniqueness.
153
+ //
154
+ // This function captures the current time in nanoseconds since the Unix epoch and appends a random integer
155
+ // to ensure additional randomness and uniqueness, even if called in rapid succession. The result is returned
156
+ // as a string. This type of ID is well-suited for time-based ordering and can be useful for generating
157
+ // unique identifiers for logs, events, or non-cryptographic applications.
158
+ //
159
+ // Returns:
160
+ // - A string representing the current Unix timestamp in nanoseconds, concatenated with a random integer.
161
+ //
162
+ // Example:
163
+ //
164
+ // id := GenerateTimestampID()
165
+ // fmt.Println("Generated Timestamp ID:", id)
166
+ //
167
+ // Notes:
168
+ // - This function provides a unique, time-ordered identifier, but it is not suitable for cryptographic use.
169
+ // - The combination of the current time and a random integer is best suited for applications requiring
170
+ // uniqueness and ordering, rather than secure identifiers.
171
+ func GenerateTimestampID () string {
172
+ return fmt .Sprintf ("%d%d" , time .Now ().UnixNano (), nextInt ())
173
+ }
0 commit comments