---
name: "crypt"
section: "3"
platform: "linux"
package: "libxcrypt"
version: "4.5.2-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/crypt/3"
---

# crypt(3)

passphrase hashing

## NAME

**crypt**, **crypt\_r**, **crypt\_rn**, **crypt\_ra** – passphrase hashing

## LIBRARY

library “libcrypt”

## SYNOPSIS

**#include \<crypt.h\>**

*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)](/shadow/5.md)) 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)](/crypt/5.md).

*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 input[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 *input* and *setting* fields to store the strings that they will pass as *input*, *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 *input*.)

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 *input* 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)](/malloc/3.md), 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)](/free/3.md).

## RETURN VALUES

Upon successful completion, **crypt**, **crypt\_r**, **crypt\_rn**, and **crypt\_ra** return a pointer to a string which encodes both the hashed passphrase, and the settings that were used to encode it. This string is directly usable as *setting* in other calls to **crypt**, **crypt\_r**, **crypt\_rn**, and **crypt\_ra**, and as *prefix* in calls to **crypt\_gensalt**, **crypt\_gensalt\_rn**, and **crypt\_gensalt\_ra**. It will be entirely printable ASCII, and will not contain whitespace or the characters ‘:’, ‘;’, ‘\*’, ‘!’, or ‘\\’. See [crypt(5)](/crypt/5.md) for more detail on the format of hashed passphrases.

**crypt** places its result in a static storage area, which will be overwritten by subsequent calls to **crypt**. It is not safe to call **crypt** from multiple threads simultaneously. It's also not recommended to use the pointer returned as an argument for another call to **crypt**, as some implementations, including earlier releases of libxcrypt, may overwrite the underlying static output buffer before computing the hash.

**crypt\_r**, **crypt\_rn**, and **crypt\_ra** place their result in the *output* field of their *data* argument. It is safe to call them from multiple threads simultaneously, as long as a separate *data* object is used for each thread. It's also not recommended to use the pointer returned as an argument for another call to **crypt\_r**, **crypt\_rn**, and **crypt\_ra** using the same *data* object for subsequent calls, as some implementations, including earlier releases of libxcrypt, may overwrite the *output* field of the passed *data* argument before computing the hash. Calling **crypt\_ra** with the *phrase* and/or *setting* parameters located within a passed *data* object which is smaller than the size of *struct crypt\_data* is not recommended, as some implementations, including earlier releases of libxcrypt, may not preserve the passed literals, if the *data* object needs to be altered.

Upon error, **crypt\_r**, **crypt\_rn**, and **crypt\_ra** write an *invalid* hash to the *output* field of their *data* argument, and **crypt** writes an invalid hash to its static storage area. This string will be shorter than 13 characters, will begin with a ‘\*’, and will not compare equal to *setting*.

Upon error, **crypt\_rn** and **crypt\_ra** return a null pointer. **crypt\_r** and **crypt** may also return a null pointer, or they may return a pointer to the invalid hash, depending on how libcrypt was configured. (The option to return the invalid hash is for compatibility with old applications that assume that **crypt** cannot return a null pointer. See PORTABILITY NOTES below.)

All four functions set *errno* when they fail. When the functions succeed, the value of *errno* is unspecified and must not be relied upon.

## ERRORS

- EINVAL

  *setting* is invalid, or requests a hashing method that is not supported.

- ERANGE

  *phrase* is too long (more than CRYPT\_MAX\_PASSPHRASE\_SIZE characters; some hashing methods may have lower limits).\
   **crypt\_rn** only: *size* is too small for the hashing method requested by *setting*.

- ENOMEM

  Failed to allocate internal scratch memory.\
   **crypt\_ra** only: failed to allocate memory for *data*.

- ENOSYS or EOPNOTSUPP

  Hashing passphrases is not supported at all on this installation, or the hashing method requested by *setting* is not supported. These error codes are not used by this version of libcrypt, but may be encountered on other systems.

## 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 hash 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)](/malloc/3.md).

## ATTRIBUTES

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

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
|  | Thread safety | MT-Unsafe race:crypt |
|  | 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)](/crypt_gensalt/3.md), [getpass(3)](/getpass/3.md), [getpwent(3)](/getpwent/3.md), [shadow(3)](/shadow/3.md), [login(1)](/login/1.md), [passwd(1)](/passwd/1.md), [crypt(5)](/crypt/5.md), [passwd(5)](/passwd/5.md), [shadow(5)](/shadow/5.md), [pam(8)](/pam/8.md)
