forked from mtusi2026/mtusi2026lab4
feat: lab4 - S3 attachments, image validation, failover
This commit is contained in:
+35
-6
@@ -1,16 +1,45 @@
|
||||
// Package ident produces opaque identifiers: request ids, session tokens and UUIDv4 values.
|
||||
package ident
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const tokenBytes = 32
|
||||
const (
|
||||
tokenBytes = 32
|
||||
requestIDSize = 16
|
||||
)
|
||||
|
||||
// NewToken returns a 64 character hex session token (256 bits of entropy).
|
||||
func NewToken() string {
|
||||
return hex.EncodeToString(randomBytes(tokenBytes))
|
||||
}
|
||||
|
||||
// NewRequestID returns a short opaque request identifier.
|
||||
func NewRequestID() string {
|
||||
return hex.EncodeToString(randomBytes(requestIDSize))
|
||||
}
|
||||
|
||||
// NewUUID returns a RFC 4122 version 4 UUID string.
|
||||
func NewUUID() string {
|
||||
buf := randomBytes(16)
|
||||
buf[6] = (buf[6] & 0x0f) | 0x40
|
||||
buf[8] = (buf[8] & 0x3f) | 0x80
|
||||
return fmt.Sprintf("%x-%x-%x-%x-%x", buf[0:4], buf[4:6], buf[6:8], buf[8:10], buf[10:16])
|
||||
}
|
||||
|
||||
func randomBytes(size int) []byte {
|
||||
buf := make([]byte, size)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
panic("crypto/rand unavailable: " + err.Error())
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
func GenerateToken() string {
|
||||
buf := make([]byte, tokenBytes)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return hex.EncodeToString(buf)
|
||||
b := make([]byte, 32)
|
||||
rand.Read(b)
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user