crypt is a POSIX C library function. It is typically used to compute the hash of user account passwords. The function outputs a text string which also encodes the salt (usually the first two characters are the salt itself and the rest is the hashed result), and identifies the hash algorithm used (defaulting to the "traditional" one explained below). This output string forms a password record, which is usually stored in a text file. More formally, crypt provides cryptographic key derivation functions for password validation and storage on Unix systems.
Relationship to Unix crypt utility There is an unrelated crypt utility in Unix, which is often confused with the C library function. To distinguish between the two, writers often refer to the utility program as crypt(1), because it is documented in section 1 of the Unix manual pages, and refer to the C library function as crypt(3), because its documentation is in manual section 3.
Details This same crypt function is used both to generate a new hash for storage and also to hash a proffered password with a recorded salt for comparison. Modern Unix implementations of the crypt library routine support a variety of hash schemes. The particular hash algorithm used can be identified by a unique code prefix in the resulting hashtext, following a de facto standard called Modular Crypt Format. The crypt() library function is also included in the Perl, PHP, Pike, Python (although it is now deprecated as of 3.11), and Ruby programming languages.
Key derivation functions supported by crypt Over time various algorithms have been introduced. To enable backward compatibility, each scheme started using some convention of serializing the password hashes that was later called the Modular Crypt Format (MCF). Old crypt(3) hashes generated before the de facto MCF standard may vary from scheme to scheme. A well-defined subset of the Modular Crypt Format was created during the Password Hashing Competition (PHC). The format is defined as: $<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]] where
id: an identifier representing the hashing algorithm (such as 1 for MD5, 5 for SHA-256 etc.) param name and its value: hash complexity parameters, like rounds/iterations count salt: salt following the radix-64 alphabet (DES uses the decoded value) hash: radix-64 encoded result of hashing the password and salt The radix-64 encoding in crypt is called B64 and uses the alphabet ./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz which is different than the more common RFC 4648 base64
The PHC subset covers a majority of MCF hashes. A number of extra application-defined methods exist.
Original implementation using the password as a key The original implementation of the crypt() library function in Third Edition Unix mimicked the M-209 cipher machine. Rather than encrypting the password with a key, which would have allowed the password to be recovered from the encrypted value and the key, it used the password itself as a key, and the password database contained the result of encrypting the password with this key.
Traditional DES-based scheme The original password encryption scheme was found to be too fast and thus subject to brute force enumeration of the most likely passwords. In Seventh Edition Unix, the scheme was changed to a modified form of the DES algorithm. A goal of this change was to make encryption slower. In addition, the algorithm incorporated a 12-bit salt in order to ensure that an attacker would be forced to crack each password independently as opposed to being able to target the entire password database simultaneously. In detail, the user's password is truncated to eight characters, and those are coerced down to only 7-bits each; this forms the 56-bit DES key. That key is then used to encrypt an all-bits-zero block, and then the ciphertext is encrypted again with the same key, and so on for a total of 25 DES encryptions. A 12-bit salt is used to perturb the encryption algorithm, so standard DES implementations can't be used to implement crypt(). The salt and the final ciphertext are encoded into a printable string in a form of base64. This is technically not encryption since the data (all bits zero) is not being kept secret; it's widely known to all in advance. However, one of the properties of DES is that it was designed to be resistant to key recovery even in the face of known plaintext situations. It is theoretically possible that two different passwords could result in exactly the same hash. Thus the password is never "decrypted": it is merely used to compute a result, and the matching results are presumed to be proof that the passwords were "the same." The advantages of this method have been that the hashtext can be stored and copied among Unix systems without exposing the corresponding plaintext password to the system administrators or other users. This portability has worked for over 30 years across many generations of computing architecture, and across many versions of Unix from many vendors.
… excerpt ends here. Continue reading the full article.