Documentation
¶
Overview ¶
Package machid provides machine identification generation for Linux systems. It generates two types of machine IDs: - eMachID (Ephemeral Machine Identifier): A unique, one-time ID based on current time and salt - reMachID (Reconstructable Machine Identifier): A reproducible ID based on hardware identifiers
Index ¶
- Variables
- func ClearCache() error
- func ClearFallbackFiles() error
- func GenerateEMachID(salt string) (string, error)
- func GenerateReMachID(salt string) (string, error)
- func GenerateReMachIDWithInfo(salt string) (remachid string, usedFallback bool, err error)
- func GetOrGenerateEMachID(salt string) (emachid string, fromCache bool, err error)
- func GetOrGenerateReMachID(salt string) (remachid string, fromCache bool, err error)
- func HasFallbackFiles() bool
- func IncrementActionCount() (int, error)
- func IsStrictMode() bool
- func RotateEMachID(salt string) (string, error)
- func SaveCachedIDs(cache *CachedMachineIDs) error
- func SetLogger(logger func(msg string))
- func SetStrictMode(enabled bool)
- type CachedMachineIDs
- type MachIDInfo
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotRoot is returned when the library is called without root privileges ErrNotRoot = errors.New("machid: root privileges required (run with sudo)") // ErrEmptySalt is returned when an empty salt is provided for eMachID generation ErrEmptySalt = errors.New("machid: salt cannot be empty") // ErrNoHardwareID is returned when no hardware identifiers can be found ErrNoHardwareID = errors.New("machid: unable to retrieve hardware identifiers from sysfs or dmidecode") // ErrDmidecodeNotFound is returned when dmidecode is needed but not installed ErrDmidecodeNotFound = errors.New("machid: dmidecode not found, please install it (e.g., apt install dmidecode)") // ErrStrictModeNoHardwareID is returned in strict mode when hardware IDs are unavailable ErrStrictModeNoHardwareID = errors.New("machid: strict mode enabled - hardware identifiers unavailable and filesystem fallback is disabled") // ErrFallbackFileCreation is returned when fallback files cannot be created ErrFallbackFileCreation = errors.New("machid: failed to create filesystem fallback files") )
Error definitions
Functions ¶
func ClearFallbackFiles ¶
func ClearFallbackFiles() error
ClearFallbackFiles removes the filesystem fallback files if they exist. This can be used to regenerate new filesystem-based IDs.
Returns an error if the files exist but cannot be removed.
func GenerateEMachID ¶
GenerateEMachID generates an Ephemeral Machine Identifier. This ID is unique and can only be generated once (based on current Unix nanosecond time and salt).
Unlike GenerateReMachID, this function does NOT require root privileges since it only uses the current timestamp and salt, not hardware identifiers.
Parameters:
- salt: A non-empty string used to add entropy to the hash
Returns:
- The eMachID as a hex-encoded SHA-256 hash
- An error if salt is empty
Security: The salt and time values are cleared from memory after hashing.
func GenerateReMachID ¶
GenerateReMachID generates a Reconstructable Machine Identifier. This ID is reproducible - the same hardware will always generate the same ID.
The ID is generated from hardware identifiers found in:
- /sys/class/dmi/id/product_serial (or chassis_serial, board_serial)
- /sys/class/dmi/id/product_uuid
If sysfs is not available, it falls back to dmidecode. If no hardware identifiers are available and strict mode is disabled (default), it falls back to filesystem-based identifiers stored in /etc/.machid/
Parameters:
- salt: An optional string to add to the hash for additional uniqueness per application
Returns:
- The reMachID as a hex-encoded SHA-256 hash
- An error if root privileges are missing or hardware IDs cannot be retrieved
Security: All hardware identifiers are cleared from memory after hashing.
Note: If filesystem fallback is used, a warning will be logged to stdout. Use SetStrictMode(true) to disable the filesystem fallback.
func GenerateReMachIDWithInfo ¶
GenerateReMachIDWithInfo generates a Reconstructable Machine Identifier and returns additional information about how it was generated.
This is the same as GenerateReMachID but also returns whether the filesystem fallback was used.
Parameters:
- salt: An optional string to add to the hash for additional uniqueness per application
Returns:
- The reMachID as a hex-encoded SHA-256 hash
- usedFallback: true if filesystem fallback was used instead of hardware IDs
- An error if generation fails
func GetOrGenerateEMachID ¶
GetOrGenerateEMachID attempts to load the cached eMachID, or generates a new one. This function does NOT require sudo since eMachID generation uses timestamps only.
Parameters:
- salt: Salt for the machine ID
Returns:
- The eMachID
- Whether the ID was loaded from cache (true) or freshly generated (false)
- An error if generation fails
func GetOrGenerateReMachID ¶
GetOrGenerateReMachID attempts to load the cached reMachID, or generates a new one. If generation is needed, sudo is required. The generated ID is cached for future use.
Parameters:
- salt: Salt for the machine ID (must match previous runs for consistent IDs)
Returns:
- The reMachID
- Whether the ID was loaded from cache (true) or freshly generated (false)
- An error if generation fails (including if sudo is required but not available)
func HasFallbackFiles ¶
func HasFallbackFiles() bool
HasFallbackFiles returns true if the filesystem fallback files exist.
func IncrementActionCount ¶
IncrementActionCount increments the action counter in the cache. Returns the new action count.
func IsStrictMode ¶
func IsStrictMode() bool
IsStrictMode returns whether strict mode is currently enabled.
func RotateEMachID ¶
RotateEMachID generates a new eMachID and updates the cache. This should be called when you need to refresh the ephemeral ID. Does NOT require sudo.
Parameters:
- salt: Salt for the new eMachID
Returns:
- The new eMachID
- An error if generation or caching fails
func SaveCachedIDs ¶
func SaveCachedIDs(cache *CachedMachineIDs) error
SaveCachedIDs saves machine IDs to the cache file. When running with sudo, it fixes ownership so the real user can read the file.
func SetLogger ¶
func SetLogger(logger func(msg string))
SetLogger sets a custom logger function for warning messages. This is useful for integrating with existing logging frameworks.
Parameters:
- logger: A function that accepts a string message. Pass nil to disable logging.
func SetStrictMode ¶
func SetStrictMode(enabled bool)
SetStrictMode enables or disables strict mode. When strict mode is enabled, the library will NOT fall back to filesystem-based machine IDs when hardware identifiers are unavailable. Instead, it will return an error.
Parameters:
- enabled: true to enable strict mode, false to allow filesystem fallback
Types ¶
type CachedMachineIDs ¶
type CachedMachineIDs struct {
ReMachID string `json:"remach_id"`
EMachID string `json:"emach_id,omitempty"`
Salt string `json:"salt,omitempty"`
ActionCount int `json:"action_count"`
CreatedAt int64 `json:"created_at,omitempty"`
}
CachedMachineIDs holds cached machine identifiers
func GetOrGenerateBoth ¶
func GetOrGenerateBoth(salt string) (*CachedMachineIDs, error)
GetOrGenerateBoth loads or generates both machine IDs. For reMachID, sudo is required if not cached. For eMachID, sudo is never required.
Parameters:
- salt: Salt for both machine IDs
Returns:
- CachedMachineIDs containing both IDs
- An error if reMachID generation fails (typically if sudo is needed but not available)
func LoadCachedIDs ¶
func LoadCachedIDs() (*CachedMachineIDs, error)
LoadCachedIDs loads cached machine IDs from disk. Returns nil if no cache exists or cache is invalid.
type MachIDInfo ¶
type MachIDInfo struct {
EMachID string // Ephemeral Machine Identifier
ReMachID string // Reconstructable Machine Identifier
UsedFallback bool // True if filesystem fallback was used for reMachID
}
MachIDInfo contains both types of machine identifiers.
func GenerateBoth ¶
func GenerateBoth(salt string) (*MachIDInfo, error)
GenerateBoth generates both eMachID and reMachID in a single call.
Parameters:
- salt: A non-empty string used for both identifiers
Returns:
- A MachIDInfo struct containing both identifiers and fallback status
- An error if generation fails for either identifier