NAME

Crypt::URandom::Token - Generate secure strings for passwords, secrets and similar

SYNOPSIS

use Crypt::URandom::Token qw(urandom_token);

# generates a 44-character alphanumeric token (default)
my $token = urandom_token();

# generate a 6 digit numeric pin
my $pin = urandom_token(6, [0..9]);

# generate a 19 character lowercase alphanumeric password
my $password = urandom_token(19, [a..z, 0..9]);


# Object usage:
my $obj = Crypt::URandom::Token->new(
    length   => 44,
    alphabet => [ A..Z, a..z, 0..9 ],
);
my $token = $obj->get;

DESCRIPTION

This module provides a secure way to generate a random token for passwords and similar using Crypt::URandom as the source of random bits.

By default, it generates a 44 character alphanumeric token with more than 256 bits of entropy. A custom alphabet with between 2 and 256 elements can be provided.

Modulo reduction and rejection sampling is used to prevent modulus bias. Keep in mind that bias will be introduced if duplicate elements are provided in the alphabet.

FUNCTIONS

urandom_token($length = 44, $alphabet = [ A..Z, a..z, 0..9 ]);

Returns a string of $length random characters from $alphabet.

If $length is not provided, it defaults to 44.

If $alphabet is not provided, it defaults to uppercase letters, lowercase letters, and digits. You can provide either a token of characters or an arrayref.

METHODS

new

Creates a new token generator object. Accepts a hash or hashref with these paramters:

  • length - desired token length (defaults to 44)

  • alphabet - the set of characters to use. Can be a string of characters or an array reference. Defaults to [ A..Z, a..z, 0..9 ]

get

Generates and returns a random token as a token, using the object attributes for length and alphabet.

AUTHOR

Stig Palmquist <stig@stig.io>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.