Skip to content

Commit 1e5ae75

Browse files
committed
Fix generateEventID to use crypto/rand without modulo bias
Replace randomString (which had modulo bias from int(v)%36 over a 256-byte range) with hex.EncodeToString of 8 crypto/rand bytes. This produces a uniformly random 16-char hex suffix with no charset mapping needed. Co-authored-by: Isaac
1 parent d1c5f09 commit 1e5ae75

1 file changed

Lines changed: 4 additions & 12 deletions

File tree

telemetry/request.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package telemetry
22

33
import (
44
"crypto/rand"
5+
"encoding/hex"
56
"encoding/json"
67
"time"
78
)
@@ -219,18 +220,9 @@ func createTelemetryRequest(metrics []*telemetryMetric, driverVersion string) (*
219220
}, nil
220221
}
221222

222-
// generateEventID generates a unique event ID.
223+
// generateEventID generates a unique event ID using crypto/rand.
223224
func generateEventID() string {
224-
return time.Now().Format("20060102150405") + "-" + randomString(8)
225-
}
226-
227-
// randomString generates a random alphanumeric string using crypto/rand.
228-
func randomString(length int) string {
229-
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
230-
b := make([]byte, length)
225+
b := make([]byte, 8)
231226
_, _ = rand.Read(b)
232-
for i, v := range b {
233-
b[i] = charset[int(v)%len(charset)]
234-
}
235-
return string(b)
227+
return time.Now().Format("20060102150405") + "-" + hex.EncodeToString(b)
236228
}

0 commit comments

Comments
 (0)