Generate a valid bcrypt password hash on a minimal Linux installation using standard Perl without installing any additional packages or Perl modules.
Standard Linux distributions (like Debian/Ubuntu) include libxcrypt/glibc, which natively support bcrypt through the system C library crypt(3).
Perl's built-in crypt() function directly delegates to this system library. By formatting a 22-character salt with the $2b$ prefix, Perl handles bcrypt natively out of the box.
Save this script as bcrypt-hash.sh:
#!/usr/bin/env bash
PASS='YOUR_PASSWORD_HERE'
# Generate bcrypt hash (Cost factor: 10, Salt length: 22 chars)
CRPASS=$(perl -e '
my @salt_alphabet = ("a".."z", "A".."Z", 0..9, ".", "/");
my $salt = join("", map { $salt_alphabet[rand 64] } 1..22);
print crypt($ARGV[0], "\$2b\$10\$" . $salt), "\n";
' "$PASS")
echo "Generated Hash: $CRPASS"
# Optional Verification (Requires htpasswd / apache2-utils)
# echo "tt:$CRPASS" > tt
# htpasswd -vbB tt tt "$PASS"
-
Prefix (
$2b$10$): Identifies the hash as standardbcryptwith a cost factor of$2^{10} = 1024$ iterations. -
Salt Generation: Generates 22 random base64-style characters required by the
bcryptspec ([a-zA-Z0-9./]). -
Native
crypt(): Calls the host system's native C library to process the hash without needingCrypt::Eksblowfish::Bcryptfrom CPAN.
- CSPRNG (Randomness Quality): Perl’s built-in rand is a pseudo-random number generator, not cryptographically secure. While suitable for generating quick salts, for production environments you can pull random bytes directly from /dev/urandom using sysread.
- OS Portability: This relies on the host C library (libxcrypt/glibc) supporting
$2b$ . macOS or older BSD-like systems might not support bcrypt directly inside native crypt(), but on virtually any modern Linux distro (Debian, Ubuntu, RHEL/Fedora, Arch), it works natively without installing additional packages.