logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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

Author

       Stig Palmquist <stig@stig.io>

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
         my $token = urandom_token($length, $alphabet);

       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 string of characters or an arrayref.

License

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

perl v5.40.1                                       2025-05-23                         Crypt::URandom::Token(3pm)

Methods

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

       •   "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.

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 19 character lowercase alphanumeric password
         my $password = urandom_token(19, [a..z, 0..9]);

         # generate a 6 digit numeric pin
         my $pin = urandom_token(6, "0123456789");

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

See Also