| 1 |
NAME |
| 2 |
Digest::Bcrypt - Perl interface to the bcrypt digest algorithm |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
#!/usr/bin/env perl |
| 6 |
use strict; |
| 7 |
use warnings; |
| 8 |
use utf8; |
| 9 |
use Digest; # via the Digest module (recommended) |
| 10 |
|
| 11 |
my $bcrypt = Digest->new('Bcrypt', cost => 12, salt => 'abcdefgh♥stuff'); |
| 12 |
# You can forego the cost and salt in favor of settings strings: |
| 13 |
my $bcrypt = Digest->new('Bcrypt', settings => '$2a$20$GA.eY03tb02ea0DqbA.eG.'); |
| 14 |
|
| 15 |
# $cost is an integer between 5 and 31 |
| 16 |
$bcrypt->cost(12); |
| 17 |
|
| 18 |
# $type is a selection between 2a, 2b, 2x, and 2y |
| 19 |
$bcrypt->type('2b'); |
| 20 |
|
| 21 |
# $salt must be exactly 16 octets long |
| 22 |
$bcrypt->salt('abcdefgh♥stuff'); |
| 23 |
# OR, for good, random salts: |
| 24 |
use Data::Entropy::Algorithms qw(rand_bits); |
| 25 |
$bcrypt->salt(rand_bits(16*8)); # 16 octets |
| 26 |
|
| 27 |
# You can forego the cost and salt in favor of settings strings: |
| 28 |
$bcrypt->settings('$2a$20$GA.eY03tb02ea0DqbA.eG.'); |
| 29 |
|
| 30 |
# add some strings we want to make a secret of |
| 31 |
$bcrypt->add('some stuff', 'here and', 'here'); |
| 32 |
|
| 33 |
my $digest = $bcrypt->digest; |
| 34 |
$digest = $bcrypt->hexdigest; |
| 35 |
$digest = $bcrypt->b64digest; |
| 36 |
|
| 37 |
# bcrypt's own non-standard base64 dictionary |
| 38 |
$digest = $bcrypt->bcrypt_b64digest; |
| 39 |
|
| 40 |
# Now, let's create a password hash and check it later: |
| 41 |
use Data::Entropy::Algorithms qw(rand_bits); |
| 42 |
my $bcrypt = Digest->new('Bcrypt', type => '2b', cost => 20, salt => rand_bits(16*8)); |
| 43 |
my $settings = $bcrypt->settings(); # save for later checks. |
| 44 |
my $pass_hash = $bcrypt->add('Some secret password')->digest; |
| 45 |
|
| 46 |
# much later, we can check a password against our hash via: |
| 47 |
my $bcrypt = Digest->new('Bcrypt', settings => $settings); |
| 48 |
if ($bcrypt->add($value_from_user)->digest eq $known_pass_hash) { |
| 49 |
say "Your password matched"; |
| 50 |
} |
| 51 |
else { |
| 52 |
say "Try again!"; |
| 53 |
} |
| 54 |
|
| 55 |
# Now that you've seen how cumbersome/silly that is, |
| 56 |
# please use Crypt::Bcrypt instead of this module. |
| 57 |
|
| 58 |
NOTICE |
| 59 |
While maintenance for Digest::Bcrypt will continue, there's no reason to |
| 60 |
use Digest::Bcrypt when Crypt::Bcrypt already exists. We strongly |
| 61 |
suggest that you use Crypt::Bcrypt instead. |
| 62 |
|
| 63 |
This "Digest::Bcrypt" interface is crufty and laborious to use when |
| 64 |
compared to that of Crypt::Bcrypt. |
| 65 |
|
| 66 |
DESCRIPTION |
| 67 |
Digest::Bcrypt provides a Digest-based interface to the Crypt::Bcrypt |
| 68 |
library. |
| 69 |
|
| 70 |
Please note that you must set a "salt" of exactly 16 octets in length, |
| 71 |
and you must provide a "cost" in the range 1..31. |
| 72 |
|
| 73 |
ATTRIBUTES |
| 74 |
Digest::Bcrypt implements the following attributes. |
| 75 |
|
| 76 |
cost |
| 77 |
$bcrypt = $bcrypt->cost(20); # allows for method chaining |
| 78 |
my $cost = $bcrypt->cost(); |
| 79 |
|
| 80 |
An integer in the range 5..31, this is required. |
| 81 |
|
| 82 |
See Crypt::Eksblowfish::Bcrypt for a detailed description of "cost" in |
| 83 |
the context of the bcrypt algorithm. |
| 84 |
|
| 85 |
When called with no arguments, it will return the current cost. |
| 86 |
|
| 87 |
salt |
| 88 |
$bcrypt = $bcrypt->salt('abcdefgh♥stuff'); # allows for method chaining |
| 89 |
my $salt = $bcrypt->salt(); |
| 90 |
|
| 91 |
# OR, for good, random salts: |
| 92 |
use Data::Entropy::Algorithms qw(rand_bits); |
| 93 |
$bcrypt->salt(rand_bits(16*8)); # 16 octets |
| 94 |
|
| 95 |
Sets the value to be used as a salt. Bcrypt requires exactly 16 octets |
| 96 |
of salt. |
| 97 |
|
| 98 |
It is recommenced that you use a module like Data::Entropy::Algorithms |
| 99 |
to provide a truly randomized salt. |
| 100 |
|
| 101 |
When called with no arguments, it will return the current salt. |
| 102 |
|
| 103 |
settings |
| 104 |
$bcrypt = $bcrypt->settings('$2a$20$GA.eY03tb02ea0DqbA.eG.'); # allows for method chaining |
| 105 |
my $settings = $bcrypt->settings(); |
| 106 |
|
| 107 |
A "settings" string can be used to set the "salt" in Digest::Bcrypt and |
| 108 |
"cost" in Digest::Bcrypt automatically. Setting the "settings" will |
| 109 |
override any current values in your "cost" and "salt" attributes. |
| 110 |
|
| 111 |
For details on the "settings" string requirements, please see |
| 112 |
Crypt::Eksblowfish::Bcrypt. |
| 113 |
|
| 114 |
When called with no arguments, it will return the current settings |
| 115 |
string. |
| 116 |
|
| 117 |
type |
| 118 |
$bcrypt = $bcrypt->type('2b'); |
| 119 |
# method chaining on mutations |
| 120 |
say $bcrypt->type(); # 2b |
| 121 |
|
| 122 |
This sets the subtype of bcrypt used. These subtypes are as defined in |
| 123 |
Crypt::Bcrypt. The available types are: "2b" which is the current |
| 124 |
standard, "2a" which is older; it's the one used in Crypt::Eksblowfish, |
| 125 |
"2y" which is considered equivalent to "2b" and used in PHP. "2x" which |
| 126 |
is very broken and only needed to work with ancient PHP versions. |
| 127 |
|
| 128 |
METHODS |
| 129 |
Digest::Bcrypt inherits all methods from Digest::base and |
| 130 |
implements/overrides the following methods as well. |
| 131 |
|
| 132 |
new |
| 133 |
my $bcrypt = Digest->new('Bcrypt', %params); |
| 134 |
my $bcrypt = Digest::Bcrypt->new(%params); |
| 135 |
my $bcrypt = Digest->new('Bcrypt', \%params); |
| 136 |
my $bcrypt = Digest::Bcrypt->new(\%params); |
| 137 |
|
| 138 |
Creates a new "Digest::Bcrypt" object. It is recommended that you use |
| 139 |
the Digest module in the first example rather than using Digest::Bcrypt |
| 140 |
directly. |
| 141 |
|
| 142 |
Any of the "ATTRIBUTES" in Digest::Bcrypt above can be passed in as a |
| 143 |
parameter. |
| 144 |
|
| 145 |
add |
| 146 |
$bcrypt->add("a"); $bcrypt->add("b"); $bcrypt->add("c"); |
| 147 |
$bcrypt->add("a")->add("b")->add("c"); |
| 148 |
$bcrypt->add("a", "b", "c"); |
| 149 |
$bcrypt->add("abc"); |
| 150 |
|
| 151 |
Adds data to the message we are calculating the digest for. All the |
| 152 |
above examples have the same effect. |
| 153 |
|
| 154 |
b64digest |
| 155 |
my $digest = $bcrypt->b64digest; |
| 156 |
|
| 157 |
Same as "digest", but will return the digest base64 encoded. |
| 158 |
|
| 159 |
The "length" of the returned string will be 31 and will only contain |
| 160 |
characters from the ranges '0'..'9', 'A'..'Z', 'a'..'z', '+', and '/' |
| 161 |
|
| 162 |
The base64 encoded string returned is not padded to be a multiple of 4 |
| 163 |
bytes long. |
| 164 |
|
| 165 |
bcrypt_b64digest |
| 166 |
my $digest = $bcrypt->bcrypt_b64digest; |
| 167 |
|
| 168 |
Same as "digest", but will return the digest base64 encoded using the |
| 169 |
alphabet that is commonly used with bcrypt. |
| 170 |
|
| 171 |
The "length" of the returned string will be 31 and will only contain |
| 172 |
characters from the ranges '0'..'9', 'A'..'Z', 'a'..'z', '+', and '.' |
| 173 |
|
| 174 |
The base64 encoded string returned is not padded to be a multiple of 4 |
| 175 |
bytes long. |
| 176 |
|
| 177 |
*Note:* This is bcrypt's own non-standard base64 alphabet, It is not |
| 178 |
compatible with the standard MIME base64 encoding. |
| 179 |
|
| 180 |
clone |
| 181 |
my $clone = $bcrypt->clone; |
| 182 |
|
| 183 |
Creates a clone of the "Digest::Bcrypt" object, and returns it. |
| 184 |
|
| 185 |
digest |
| 186 |
my $digest = $bcrypt->digest; |
| 187 |
|
| 188 |
Returns the binary digest for the message. The returned string will be |
| 189 |
23 bytes long. |
| 190 |
|
| 191 |
hexdigest |
| 192 |
my $digest = $bcrypt->hexdigest; |
| 193 |
|
| 194 |
Same as "digest", but will return the digest in hexadecimal form. |
| 195 |
|
| 196 |
The "length" of the returned string will be 46 and will only contain |
| 197 |
characters from the ranges '0'..'9' and 'a'..'f'. |
| 198 |
|
| 199 |
reset |
| 200 |
$bcrypt->reset; |
| 201 |
|
| 202 |
Resets the object to the same internal state it was in when it was |
| 203 |
constructed. |
| 204 |
|
| 205 |
SEE ALSO |
| 206 |
Digest, Crypt::Eksblowfish::Bcrypt, Data::Entropy::Algorithms |
| 207 |
|
| 208 |
AUTHOR |
| 209 |
James Aitken "[email protected]" |
| 210 |
|
| 211 |
CONTRIBUTORS |
| 212 |
* Chase Whitener "[email protected]" |
| 213 |
|
| 214 |
COPYRIGHT AND LICENSE |
| 215 |
This software is copyright (c) 2012 by James Aitken. |
| 216 |
|
| 217 |
This is free software; you can redistribute it and/or modify it under |
| 218 |
the same terms as the Perl 5 programming language system itself. |
| 219 |
|