Documentation
¶
Overview ¶
Package rigid provides cryptographically secured ULIDs (Universally Unique Lexicographically Sortable Identifiers) with built-in HMAC-based integrity verification.
Rigid generates tamper-proof, time-ordered unique identifiers that include cryptographic signatures to ensure integrity and prevent forgery. It supports optional metadata binding and configurable signature lengths for different security requirements.
Basic Usage ¶
secretKey := []byte("your-secret-key") r, err := rigid.NewRigid(secretKey) if err != nil { log.Fatal(err) } // Generate a secure ULID rigidID, err := r.Generate() if err != nil { log.Fatal(err) } // Generate with metadata rigidWithMetadata, err := r.Generate("user:alice:role:admin") if err != nil { log.Fatal(err) } // Verify integrity result, err := r.Verify(rigidWithMetadata) if err != nil { log.Fatal(err) } fmt.Printf("Valid: %t, Metadata: %s\n", result.Valid, result.Metadata)
Security Features ¶
- HMAC-SHA256 cryptographic signatures prevent tampering and forgery - Constant-time verification resists timing attacks - Configurable signature lengths (4-32 bytes) for security/size trade-offs - Thread-safe concurrent generation with monotonic entropy
ID Format ¶
Rigid IDs follow the format: ULID-SIGNATURE or ULID-SIGNATURE-METADATA
Example: 01ARZ3NDEKTSV4RRFFQ69G5FAV-MFRGG2BA-user:session:12345
Compatibility ¶
This implementation is compatible with the Python rigid library when using the same secret key, signature length, and HMAC algorithm.
Index ¶
Constants ¶
const ( // DefaultSignatureLength is the default HMAC signature length in bytes. DefaultSignatureLength = 8 // MinSignatureLength is the minimum allowed signature length in bytes. MinSignatureLength = 4 // MaxSignatureLength is the maximum allowed signature length in bytes. MaxSignatureLength = 32 )
Constants defining signature length constraints.
Variables ¶
var ( // ErrInvalidFormat indicates the rigid ID format is invalid. ErrInvalidFormat = errors.New("invalid rigid format") // ErrInvalidULID indicates the ULID component is malformed. ErrInvalidULID = errors.New("invalid ULID") // ErrIntegrityFailure indicates the signature verification failed. ErrIntegrityFailure = errors.New("integrity verification failed") // ErrEmptySecretKey indicates the provided secret key is empty or nil. ErrEmptySecretKey = errors.New("secret key cannot be empty") // ErrInvalidSigLength indicates the signature length is outside valid range. ErrInvalidSigLength = errors.New("signature length must be positive") )
Error variables returned by rigid operations.
Functions ¶
This section is empty.
Types ¶
type Rigid ¶
type Rigid struct {
// contains filtered or unexported fields
}
Rigid is the main structure for generating and verifying cryptographically secured ULIDs. It maintains the secret key, signature configuration, and entropy source for ULID generation. All methods are thread-safe for concurrent use.
func NewRigid ¶
NewRigid creates a new Rigid instance with the provided secret key. The optional signatureLength parameter sets the HMAC signature length in bytes (4-32). If not provided, DefaultSignatureLength (8 bytes) is used. Returns an error if the secret key is empty or signature length is invalid.
func (*Rigid) ExtractTimestamp ¶
ExtractTimestamp extracts the timestamp from the ULID component of a rigid ID. Returns the embedded timestamp or an error if extraction fails.
func (*Rigid) ExtractULID ¶
ExtractULID extracts and parses the ULID component from a rigid ID. Returns the parsed ULID object or an error if extraction fails.
func (*Rigid) Generate ¶
Generate creates a new cryptographically secured ULID with optional metadata. The optional metadata parameter will be cryptographically bound to the ID. Only the first metadata parameter is used if multiple are provided. Returns the generated rigid ID string or an error if generation fails.
type VerifyResult ¶
type VerifyResult struct { // Valid indicates whether the rigid ID passed integrity verification. Valid bool // ULID contains the extracted ULID string. ULID string // Metadata contains the extracted metadata string, if any. Metadata string }
VerifyResult contains the results of a rigid ID verification operation.