App-bmkpasswd

 view release on metacpan or  search on metacpan

README.mkdn  view on Meta::CPAN


    my $crypted = mkpasswd( $passwd => 
      +{
        type    => $type,
        cost    => $cost,
        strong  => $strongsalt,
        saltgen => $saltgenerator,
      }
    );

Generate hashed passwords.

By default, generates a bcrypted passwd with work-cost 08:

    $bcrypted = mkpasswd($passwd);

A different work-cost can be specified for bcrypt passwds:

    $bcrypted = mkpasswd($passwd, 'bcrypt', '10');

SHA-256 and SHA-512 are supported, in which case the work-cost value is ignored:

    $crypted = mkpasswd($passwd, 'sha256');
    $crypted = mkpasswd($passwd, 'sha512');

If a fourth boolean-true argument is specified, a strongly-random salt is
generated. This requires spare entropy, and will block if entropy-starved:

    $crypted = mkpasswd($passwd, 'bcrypt', '08', 'strong');
    $crypted = mkpasswd($passwd, 'sha512', 0, 'strong');

Options can be passed as a HASH, instead. This also lets you pass in a salt
generator coderef:

    $crypted = mkpasswd( $passwd => +{
        type => 'bcrypt',
        cost => '10',
        strong  => 0,
        saltgen => $saltgenerator,
      }
    );

The salt generator is passed the type (one of: `bcrypt`, `sha`, `md5`) and
the value of the **strong** option (default false).

    my $saltgenerator = sub {
      my ($type, $strongsalt) = @_;
      if ($type eq 'bcrypt') {
        # ...
      } elsif ($type eq 'sha') {
        # ...
      } else {
        die "Don't know how to create a salt for type '$type'!"
      }
    };

Most people want random salts, in which case the default salt generator
should be fine.

See ["mkpasswd\_forked"](#mkpasswd_forked) if your application loads this module before forking
or creating threads that generate passwords.

## mkpasswd\_available

    my @available = mkpasswd_available;

    if ( mkpasswd_available('sha512') ) { ... }

Given no arguments, returns the list of available hash types.

Given a type (see ["mkpasswd"](#mkpasswd)), returns boolean true if the method is available. ('bcrypt' is
always available.)

## mkpasswd\_forked

    # After a fork / new thread is created:
    mkpasswd_forked;

To retain secure salts after forking the process or creating a new thread, 
it's advisable to either only load this module after creating the new process
or call **mkpasswd\_forked** in the new process to reset the random seeds used
by salt generators.

Added in `v2.6.1`.

# AUTHOR

Jon Portnoy <jon@portnoy.me>



( run in 1.394 second using v1.01-cache-2.11-cpan-f56aa216473 )