| 1 | # ABSTRACT: generate PostgreSQl password hashes | ||||||||||||||
| 2 | =head1 NAME | ||||||||||||||
| 3 | Crypt::PostgreSQL - Module for generating encrypted password for PostgreSQL | ||||||||||||||
| 4 | |||||||||||||||
| 5 | |||||||||||||||
| 6 | =head1 VERSION | ||||||||||||||
| 7 | |||||||||||||||
| 8 | version 0.03 | ||||||||||||||
| 9 | |||||||||||||||
| 10 | =cut | ||||||||||||||
| 11 | |||||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | package Crypt::PostgreSQL; | ||||||||||||||
| 15 | 1 | $Crypt::PostgreSQL::VERSION = '0.03'; | |||||||||||||
| 16 | 1 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 1 | ▶ | use Exporter (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 1 | ▶ | use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 1 | @ISA = qw (Exporter); | |||||||||||||
| 20 | 1 | @EXPORT = qw (); | |||||||||||||
| 21 | 1 | @EXPORT_OK = qw (); | |||||||||||||
| 22 | 1 | %EXPORT_TAGS = (); | |||||||||||||
| 23 | } | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 1 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 1 | ▶ | use Crypt::URandom; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 1 | ▶ | use Crypt::KeyDerivation qw(pbkdf2); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 1 | ▶ | use Crypt::Mac::HMAC qw(hmac hmac_b64); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 1 | ▶ | use Crypt::Digest::SHA256 qw(sha256_b64); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 1 | ▶ | use MIME::Base64; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 1 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 SYNOPSIS | ||||||||||||||
| 34 | |||||||||||||||
| 35 | use Crypt::PostgreSQL; | ||||||||||||||
| 36 | |||||||||||||||
| 37 | print Crypt::PostgreSQL::encrypt_md5('my password', 'myuser'); | ||||||||||||||
| 38 | |||||||||||||||
| 39 | my $scram_hash = Crypt::PostgreSQL::encrypt_scram('my password'); | ||||||||||||||
| 40 | my DBI; | ||||||||||||||
| 41 | my $dbh = DBI->connect("dbi:Pg:dbname=...", '', '', {AutoCommit => 0}); | ||||||||||||||
| 42 | $dbh->do(q{ | ||||||||||||||
| 43 | ALTER USER my_user SET ENCRYPTION PASSWORD '$scram_hash'; | ||||||||||||||
| 44 | }); | ||||||||||||||
| 45 | |||||||||||||||
| 46 | |||||||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||||||
| 48 | |||||||||||||||
| 49 | This module is for generating password suitable to generate password hashes in PostgreSQL format, | ||||||||||||||
| 50 | using one of the two encrypted formats: scram_sha_256 and md5 | ||||||||||||||
| 51 | |||||||||||||||
| 52 | |||||||||||||||
| 53 | =head2 encrypt_md5 | ||||||||||||||
| 54 | |||||||||||||||
| 55 | The 1st argument is the password to encrypted | ||||||||||||||
| 56 | |||||||||||||||
| 57 | The 2th argument is the postgresgl user name | ||||||||||||||
| 58 | |||||||||||||||
| 59 | The function returns hash string suitable to use with ALTER USER SQL command. | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =cut | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 1 | ▶ | sub encrypt_md5 { | ||||||||||||
|
Subroutine
encrypt_md5: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||
| 64 | 1 | my($password, $user) = @_; | |||||||||||||
| 65 | 1 | ▶ | if(!length $user){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 0 | croak 'The 2nd parameter with the user is missing!'; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | 1 | return 'md5'.md5_hex($password.$user); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =head2 encrypt_scram | ||||||||||||||
| 72 | |||||||||||||||
| 73 | The 1st argument is the password to encrypted | ||||||||||||||
| 74 | |||||||||||||||
| 75 | The 2nd argument, can define salt (use only for test!) | ||||||||||||||
| 76 | |||||||||||||||
| 77 | The function returns hash string suitalbe to use with ALTER USER SQL command. | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =cut | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | ▶ | sub encrypt_scram { | ||||||||||||
|
Subroutine
encrypt_scram: called CC=3 SCAR=11.5
Pod
documented
| |||||||||||||||
| 82 | 1 | my($password, $salt) = @_; | |||||||||||||
| 83 | 1 | ▶ | if(!defined $salt){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 84 | 0 | $salt = Crypt::URandom::urandom(16); | |||||||||||||
| 85 | }elsif(length($salt) != 16){ | ||||||||||||||
| 86 | 0 | croak 'The salt length must be 16!'; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | 1 | my $iterations = 4096; | |||||||||||||
| 89 | 1 | my $digest_key = pbkdf2($password, $salt, $iterations, 'SHA256', 32); | |||||||||||||
| 90 | 1 | my $client_key = hmac('SHA256', $digest_key ,'Client Key'); | |||||||||||||
| 91 | 1 | my $b64_client_key = sha256_b64($client_key); | |||||||||||||
| 92 | 1 | my $b64_server_key = hmac_b64('SHA256', $digest_key, 'Server Key'); | |||||||||||||
| 93 | 1 | my $b64_salt = encode_base64($salt, ''); | |||||||||||||
| 94 | 1 | return "SCRAM-SHA-256\$$iterations:$b64_salt\$$b64_client_key:$b64_server_key"; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | |||||||||||||||
| 98 | =head1 SECURITY | ||||||||||||||
| 99 | |||||||||||||||
| 100 | Report security vulnerabilities **privately** to the maintainer at | ||||||||||||||
| 101 | E<lt>gdo@leader.itE<gt>. See the F<SECURITY.md> file in this distribution's | ||||||||||||||
| 102 | root directory for the full policy (coordinated disclosure, optional CC to the | ||||||||||||||
| 103 | L<CPAN Security Group|https://security.metacpan.org/> at | ||||||||||||||
| 104 | E<lt>cpan-security@security.metacpan.orgE<gt>). Do not file security issues on | ||||||||||||||
| 105 | public bug trackers before coordination. | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head1 BUGS | ||||||||||||||
| 108 | |||||||||||||||
| 109 | Please let the author know if any are caught | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =head1 AUTHOR | ||||||||||||||
| 112 | |||||||||||||||
| 113 | Guido Brugnara | ||||||||||||||
| 114 | gdo@leader.it | ||||||||||||||
| 115 | |||||||||||||||
| 116 | |||||||||||||||
| 117 | =head1 COPYRIGHT | ||||||||||||||
| 118 | |||||||||||||||
| 119 | This program is free software; you can redistribute | ||||||||||||||
| 120 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 121 | |||||||||||||||
| 122 | The full text of the license can be found in the | ||||||||||||||
| 123 | LICENSE file included with this module. | ||||||||||||||
| 124 | |||||||||||||||
| 125 | |||||||||||||||
| 126 | =head1 SEE ALSO | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =over | ||||||||||||||
| 129 | |||||||||||||||
| 130 | =item L<https://www.postgresql.org/docs/current/auth-password.html> | ||||||||||||||
| 131 | |||||||||||||||
| 132 | PostgreSQL documentation: 20.5. Password Authentication | ||||||||||||||
| 133 | |||||||||||||||
| 134 | =item L<https://www.leader.it/Blog/PostgreSQL_SCRAM-SHA-256_authentication> | ||||||||||||||
| 135 | |||||||||||||||
| 136 | Blog article: PostgreSQL SCRAM-SHA-256 authentication with credits ... | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =back | ||||||||||||||
| 139 | |||||||||||||||
| 140 | =cut | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 1 | 1; | |||||||||||||