« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Error.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/XML/Simple.pm »
| 1 | package Dwarf::Util::Password; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use Digest::SHA qw(sha512); | ||
| 4 | use MIME::Base64; | ||
| 5 | |||
| 6 | require Exporter; | ||
| 7 | our @ISA = qw(Exporter); | ||
| 8 | our @EXPORT = qw(is_valid gen_hashed_password gen_password); | ||
| 9 | |||
| 10 | sub SECRET_STRING { 'Jesus Bellido professor of guitar making' } | ||
| 11 | |||
| 12 | # パスワードが正しいかどうか | ||
| 13 | sub is_valid { | ||
| 14 | my ($hashed, $raw) = @_; | ||
| 15 | my $salt = substr($hashed, 0, 4); | ||
| 16 | return ($hashed eq gen_hashed_password($raw, $salt)); | ||
| 17 | } | ||
| 18 | |||
| 19 | # パスワードのハッシュ化 | ||
| 20 | sub gen_hashed_password { | ||
| 21 | my ($password, $salt) = @_; | ||
| 22 | $salt ||= gen_password(1, 4); | ||
| 23 | return $salt . encode_base64(sha512($salt . $password . SECRET_STRING), ''); | ||
| 24 | } | ||
| 25 | |||
| 26 | # パスワード生成 (0: 全て, 1: 英数字のみ, 2: 英字のみ, 3: 数字のみ) | ||
| 27 | sub gen_password { | ||
| 28 | my ($type, $length) = @_; | ||
| 29 | $type = 0 if not defined $type or $type !~ /^[0123]$/; | ||
| 30 | $length ||= 10; | ||
| 31 | |||
| 32 | my @num = ('0' .. '9'); | ||
| 33 | my @alpha = ('a' .. 'z', 'A' .. 'Z'); | ||
| 34 | my @symbol = ( | ||
| 35 | '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', | ||
| 36 | '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~' | ||
| 37 | ); | ||
| 38 | |||
| 39 | my @chars; | ||
| 40 | push @chars, @num if $type != 2; | ||
| 41 | push @chars, @alpha if $type < 3; | ||
| 42 | push @chars, @symbol if $type < 1; | ||
| 43 | |||
| 44 | return join('', map { $chars[ int(rand(scalar @chars)) ] } (1 .. $length)); | ||
| 45 | } | ||
| 46 | |||
| 47 | 1; |