Skip to content

Instantly share code, notes, and snippets.

@spyesx
Created September 23, 2026 12:46
Show Gist options
  • Select an option

  • Save spyesx/ce182014e091c70af30f55c42006edca to your computer and use it in GitHub Desktop.

Select an option

Save spyesx/ce182014e091c70af30f55c42006edca to your computer and use it in GitHub Desktop.
Minimal Bcrypt Hash Generation on Debian/Linux

Minimal Bcrypt Hash Generation on Debian/Linux

Generate a valid bcrypt password hash on a minimal Linux installation using standard Perl without installing any additional packages or Perl modules.

How It Works

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.

Script

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"

Explanation

  1. Prefix ($2b$10$): Identifies the hash as standard bcrypt with a cost factor of $2^{10} = 1024$ iterations.
  2. Salt Generation: Generates 22 random base64-style characters required by the bcrypt spec ([a-zA-Z0-9./]).
  3. Native crypt(): Calls the host system's native C library to process the hash without needing Crypt::Eksblowfish::Bcrypt from CPAN.

Notes & Portability

  • 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment