man > crypt(5)

πŸ” crypt(3) β€” BSD Library Functions Manual

πŸ“› NAME

crypt, crypt_r, crypt_rn, crypt_ra β€” passphrase hashing

πŸš€ Quick Reference

Use Case Command Description
πŸ”’ Hash a passphrase crypt(phrase, setting) Irreversibly hash a passphrase for storage in the system password database.
🧡 Thread-safe hashing crypt_r(phrase, setting, &data) Re-entrant version using a pre-initialized crypt_data structure.
πŸ—„οΈ Dynamic memory hashing crypt_ra(phrase, setting, &data, &size) Allows the library to allocate the crypt_data structure dynamically via malloc(3).
βœ… Verify a passphrase crypt(phrase, existing_hash) Hash the candidate phrase with the salt from the existing hash and compare the results.
πŸ§‚ Generate a salt crypt_gensalt(prefix, ...) Generate a random salt string for a specific hashing method.

πŸ“š LIBRARY

Crypt Library (libcrypt, -lcrypt)

πŸ“ SYNOPSIS

πŸ“„ Header

#include <crypt.h>

πŸ”§ Functions

char *
crypt(const char *phrase, const char *setting);

char *
crypt_r(const char *phrase, const char *setting, struct crypt_data *data);

char *
crypt_rn(const char *phrase, const char *setting, struct crypt_data *data, int size);

char *
crypt_ra(const char *phrase, const char *setting, void **data, int *size);

πŸ“– DESCRIPTION

The crypt, crypt_r, crypt_rn, and crypt_ra functions irreversibly β€œhash” phrase for storage in the system password database (shadow(5)) using a cryptographic β€œhashing method.” The result of this operation is called a β€œhashed passphrase” or just a β€œhash.” Hashing methods are described in crypt(5).

setting controls which hashing method to use, and also supplies various parameters to the chosen method, most importantly a random β€œsalt” which ensures that no two stored hashes are the same, even if the phrase strings are the same.

The data argument to crypt_r is a structure of type struct crypt_data. It has at least these fields:

struct crypt_data {
    char output[CRYPT_OUTPUT_SIZE];
    char setting[CRYPT_OUTPUT_SIZE];
    char phrase[CRYPT_MAX_PASSPHRASE_SIZE];
    char initialized;
};

Upon a successful return from crypt_r, the hashed passphrase will be stored in output. Applications are encouraged, but not required, to use the phrase and setting fields to store the strings that they will pass as phrase and setting to crypt_r. This will make it easier to erase all sensitive data after it is no longer needed.

The initialized field must be set to zero before the first time a struct crypt_data object is first used in a call to crypt_r(). We recommend zeroing the entire object, not just initialized and not just the documented fields, before the first use. (Of course, do this before storing anything in setting and phrase.)

The data argument to crypt_rn should also point to a struct crypt_data object, and size should be the size of that object, cast to int. When used with crypt_rn, the entire data object (except for the phrase and setting fields) must be zeroed before its first use; this is not just a recommendation, as it is for crypt_r. Otherwise, the fields of the object have the same uses that they do for crypt_r.

On the first call to crypt_ra, data should be the address of a void * variable set to NULL, and size should be the address of an int variable set to zero. crypt_ra will allocate and initialize a struct crypt_data object, using malloc(3), and write its address and size into the variables pointed to by data and size. These can be reused in subsequent calls. After the application is done hashing passphrases, it should deallocate the struct crypt_data object using free(3).

πŸ“¨ RETURN VALUES

⚠️ ERRORS

🌐 PORTABILITY NOTES

crypt is included in POSIX, but crypt_r, crypt_rn, and crypt_ra are not part of any standard.

POSIX does not specify any hashing methods, and does not require hashed passphrases to be portable between systems. In practice, hashed passphrases are portable as long as both systems support the hashing method that was used. However, the set of supported hashing methods varies considerably from system to system.

The behavior of crypt on errors isn't well standardized. Some implementations simply can't fail (except by crashing the program), others return a null pointer or a fixed string. Most implementations don't set errno, but some do. POSIX specifies returning a null pointer and setting errno, but it defines only one possible error, ENOSYS, in the case where crypt is not supported at all. Some older applications are not prepared to handle null pointers returned by crypt. The behavior described above for this implementation, setting errno and returning an invalid hashed passphrase different from setting, is chosen to make these applications fail closed when an error occurs.

Due to historical restrictions on the export of cryptographic software from the USA, crypt is an optional POSIX component. Applications should therefore be prepared for crypt not to be available, or to always fail (setting errno to ENOSYS) at runtime.

POSIX specifies that crypt is declared in <unistd.h>, but only if the macro _XOPEN_CRYPT is defined and has a value greater than or equal to zero. Since libcrypt does not provide <unistd.h>, it declares crypt, crypt_r, crypt_rn, and crypt_ra in <crypt.h> instead.

On a minority of systems (notably recent versions of Solaris), crypt uses a thread-specific static storage buffer, which makes it safe to call from multiple threads simultaneously, but does not prevent each call within a thread from overwriting the results of the previous one.

πŸ› BUGS

Some implementations of crypt, upon error, return an invalid hash that is stored in a read-only location or only initialized once, which means that it is only safe to erase the buffer pointed to by the crypt return value if an error did not occur.

struct crypt_data may be quite large (32kB in this implementation of libcrypt; over 128kB in some other implementations). This is large enough that it may be unwise to allocate it on the stack.

Some recently designed hashing methods need even more scratch memory, but the crypt_r interface makes it impossible to change the size of struct crypt_data without breaking binary compatibility. The crypt_rn interface could accommodate larger allocations for specific hashing methods, but the caller of crypt_rn has no way of knowing how much memory to allocate. crypt_ra does the allocation itself, but can only make a single call to malloc(3).

🏷️ ATTRIBUTES

For an explanation of the terms used in this section, see attributes(7).

Interface Attribute Value
crypt Thread safety MT-Unsafe race:crypt
crypt_r, crypt_rn, crypt_ra Thread safety MT-Safe

πŸ“œ HISTORY

A rotor-based crypt function appeared in Version 6 AT&T UNIX. The β€œtraditional” DES-based crypt first appeared in Version 7 AT&T UNIX.

crypt_r originates with the GNU C Library. There's also a crypt_r function on HP-UX and MKS Toolkit, but the prototypes and semantics differ.

crypt_rn and crypt_ra originate with the Openwall project.

πŸ”— SEE ALSO

crypt_gensalt(3), getpass(3), getpwent(3), shadow(3), login(1), passwd(1), crypt(5), passwd(5), shadow(5), pam(8)

Openwall Project October 11, 2017 Openwall Project

crypt(5)
πŸ” crypt(3) β€” BSD Library Functions Manual πŸ“› NAME πŸš€ Quick Reference πŸ“š LIBRARY πŸ“ SYNOPSIS
πŸ“„ Header πŸ”§ Functions
πŸ“– DESCRIPTION πŸ“¨ RETURN VALUES ⚠️ ERRORS 🌐 PORTABILITY NOTES πŸ› BUGS 🏷️ ATTRIBUTES πŸ“œ HISTORY πŸ”— SEE ALSO

Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-17 22:56 @216.73.216.6
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^