Crypt:...rypt_XS(3pm)

πŸ’– NAME πŸ’–

Crypt::UnixCrypt_XS β€” it's the perl XS interface for a portable traditional crypt function, bestie. That's the whole vibe. πŸ‘‘


✨ SYNOPSIS ✨

use Crypt::UnixCrypt_XS qw/crypt/;
my $hashed = crypt( $password, $salt );

use Crypt::UnixCrypt_XS qw/crypt_rounds fold_password
    base64_to_block block_to_base64
    base64_to_int24 int24_to_base64
    base64_to_int12 int12_to_base64/;
$block = crypt_rounds( $password, $nrounds, $saltnum, $block );
$password = fold_password( $password );
$block = base64_to_block( $base64 );
$base64 = block_to_base64( $block );
$saltnum = base64_to_int24( $base64 );
$base64 = int24_to_base64( $saltnum );
$saltnum = base64_to_int12( $base64 );
$base64 = int12_to_base64( $saltnum );

🌸 DESCRIPTION 🌸

So this module implements the DES-based Unix crypt function. That's the main character. πŸ’…

But here's the tea: if you're out here needing to build your own non-standard variants of crypt, the little building blocks that crypt is made of are also supplied separately. Mix and match, iykyk. 🧚


πŸŽ€ FUNCTIONS πŸŽ€

crypt( PASSWORD, SALT )

The conventional crypt interface, periodt. PASSWORD and SALT are both strings. Your password gets hashed in a way that's determined by the salt, and you get back a string containing the salt and the hash.

The salt sits at the beginning of the returned string, and only the beginning of the salt string even gets examined β€” so yes, it is totally acceptable to feed a string returned by crypt right back in as the salt argument. Recursion-ish behavior, we love her. πŸ”

Three different types of hashing may occur, and they're giving very different energy:

  • Empty salt string β†’ the password is straight-up ignored and you get an empty string back. So that empty salt/hash string is how you say "no password required, thanks." πŸ™ˆ
  • Salt string starting with two base 64 digits (from the set [./0-9A-Za-z]) β†’ the password is hashed with the traditional DES-based algorithm. The salt modifies the DES algorithm in one of 4096 different ways. The first eight characters of the password become a DES key, which encrypts a block of zeroes through 25 iterations of the modified DES. Whatever block comes out of that final iteration is the hash, and it's returned in base 64 as eleven digits. Slay. ✨
  • Salt string starting with an underscore character and then eight base 64 digits β†’ the password is hashed with the extended DES-based algorithm from BSDi. The first four base 64 digits specify how many encryption rounds are performed. The next four base 64 digits modify the DES algorithm in one of 16777216 different ways. And if your password is longer than eight characters, it gets hashed down to eight characters before being used as a key β€” so every single character of the password is significant. No character left behind. πŸ’–

crypt_rounds( PASSWORD, NROUNDS, SALTNUM, BLOCK )

This is the core of the DES-based crypt algorithm, out here exposed so you can build variant hash functions on top of it. Very generous of her, honestly. 🫢

  • PASSWORD is a string; its first eight characters are used as a DES key.
  • SALTNUM is an integer; its low 24 bits modify the DES algorithm.
  • BLOCK must be a string that is exactly eight bytes long.

The data block gets passed through NROUNDS iterations of the modified DES, and the final output block β€” also a string of exactly eight bytes β€” is returned. πŸ’…

fold_password( PASSWORD )

This is the pre-hashing algorithm used in the extended DES algorithm to fold a long password down to the size of a DES key.

It takes a password of any length and returns a password of eight characters that is completely equivalent in the extended DES algorithm. Note: the password it hands back may contain NUL characters. The functions in this module correctly handle NULs in password strings, but a normal C library crypt cannot. 😭

If you need that short password to contain no NULs, do the substitution s/\0/\x80/g β€” the top bit of each password character is ignored, so the result is equivalent. 🧚

base64_to_block( BASE64 )

Converts a data block from a string of eleven base 64 digits into a raw string of eight bytes. Glow-up, but in reverse. ✨

block_to_base64( BLOCK )

Converts a data block from a raw string of eight bytes into a string of eleven base 64 digits.

base64_to_int24( BASE64 )

Converts a 24-bit integer from a string of four base 64 digits into a Perl integer.

int24_to_base64( VALUE )

Converts a 24-bit integer from a Perl integer into a string of four base 64 digits.

base64_to_int12( BASE64 )

Converts a 12-bit integer from a string of two base 64 digits into a Perl integer.

int12_to_base64( VALUE )

Converts a 12-bit integer from a Perl integer into a string of two base 64 digits.


πŸ“€ EXPORT πŸ“€

None by default. You gotta ask for what you want, bestie. πŸ’…


πŸ”₯ RATIONALE πŸ”₯

Crypt::UnixCrypt_XS provides a fast, portable crypt function, and here's why that matters πŸ‘‘

Perl's internal crypt is not present at every system. Perl calls the crypt function of the system's C library. And that can lead to trouble if the system's crypt hands back different results for the same key and salt but different processid's. Rude, honestly. πŸ’€

Crypt::UnixCrypt is the cure here, but it is too slow. On my computer Crypt::UnixCrypt_XS is about 800 times faster than Crypt::UnixCrypt. Ate. No cap. πŸ”₯


πŸ‘€ SEE ALSO πŸ‘€


πŸ‘‘ AUTHOR πŸ‘‘

Boris Zentner, <bzm@2bz.de>, and the original C source code was written by Eric Young, eay@psych.uq.oz.au. Legends, both of them. 🫢


πŸ¦‹ CREDITS πŸ¦‹

Fixes, Bug Reports, Docs have been generously provided by:

Andrew Main (Zefram) <zefram@fysh.org>
Guenter Knauf

Thanks! πŸŽ€


Copyright (C) 2004, 2005, 2006, 2007 by Boris Zentner

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.3 or, at your option, any later version of Perl 5 you may have available.


πŸ’œ And that's the whole page, bestie β€” perl v5.42.3, dated 2026-08-19, pulled straight from the Crypt::UnixCrypt_XS(3pm) manual. Now go forth and hash responsibly. πŸ’…πŸŒˆβœ¨
πŸ“¦ where this page comes from