# man > crypt(3)

---
type: CommandReference
command: crypt
mode: man
section: 3
source: man-pages
---

## Quick Reference
- `crypt(phrase, setting)` — hash a passphrase with a given setting (salt + method)
- `crypt_r(phrase, setting, &data)` — reentrant version using pre-initialized `struct crypt_data`
- `crypt_rn(phrase, setting, &data, size)` — reentrant with explicit `size` of `data`
- `crypt_ra(phrase, setting, &data, &size)` — reentrant with automatic allocation of `data`

## Name
`crypt`, `crypt_r`, `crypt_rn`, `crypt_ra` — passphrase hashing

## Synopsis
c
#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);
## Options
No command-line options. The `setting` string controls the hashing method and salt. See `crypt(5)` for format details.

## Description
These functions irreversibly hash `phrase` for storage in the system password database. `setting` supplies the hashing method, parameters, and a random salt. The result is a printable ASCII string that can be used as `setting` in subsequent calls.

### `struct crypt_data`
c
struct crypt_data {
    char output[CRYPT_OUTPUT_SIZE];
    char setting[CRYPT_OUTPUT_SIZE];
    char phrase[CRYPT_MAX_PASSPHRASE_SIZE];
    char initialized;
};
- `initialized` must be set to zero before first use (recommend zeroing the entire struct).
- `output` receives the hashed passphrase on success.
- For `crypt_rn`, the entire `data` object (except `phrase`/`setting`) must be zeroed before first use.
- For `crypt_ra`, pass `*data = NULL` and `*size = 0` on first call; it allocates memory via `malloc(3)`.

## Return Values
- On success: return a pointer to the hashed passphrase string.
- On error: `crypt` returns a pointer to an invalid hash (starting with `*`). `crypt_r` and `crypt_rn` return either a null pointer or an invalid hash depending on configuration. `crypt_ra` returns a null pointer. All set `errno`.

## Errors
- `EINVAL` — `setting` invalid or unsupported method.
- `ERANGE` — `phrase` too long; or (`crypt_rn` only) `size` too small.
- `ENOMEM` — memory allocation failure (`crypt_ra` for `data`).
- `ENOSYS` / `EOPNOTSUPP` — hashing not supported (may not occur on this implementation).

## Thread Safety
| Function | Thread Safety |
|----------|---------------|
| `crypt` | MT-Unsafe race:crypt |
| `crypt_r`, `crypt_rn`, `crypt_ra` | MT-Safe (use separate `data` per thread) |

## Portability Notes
- `crypt` is in POSIX; `crypt_r`, `crypt_rn`, `crypt_ra` are not standardized.
- POSIX does not specify hashing methods; portability of hashes depends on support.
- On some systems, `crypt` may use thread-specific storage (making it MT-Safe).
- `crypt` is an optional POSIX component; applications should handle runtime failure.

## Examples
### Basic usage with `crypt`
c
#include <crypt.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *phrase = "my_password";
    const char *setting = "$5$rounds=5000$usesomesillystri$";  // SHA-256
    char *result = crypt(phrase, setting);
    if (result[0] == '*') {
        fprintf(stderr, "crypt failed\n");
        exit(1);
    }
    printf("Hash: %s\n", result);
    return 0;
}
### Reentrant usage with `crypt_r`
c
struct crypt_data data;
data.initialized = 0;
const char *phrase = "pass123";
const char *setting = "$6$saltstring$";  // SHA-512
char *result = crypt_r(phrase, setting, &data);
if (result == NULL || result[0] == '*') {
    // error handling
}
## See Also
- [crypt_gensalt(3)](https://www.chedong.com/phpMan.php/man/cryptgensalt/3/markdown)
- [crypt(5)](https://www.chedong.com/phpMan.php/man/crypt/5/markdown)
- [getpass(3)](https://www.chedong.com/phpMan.php/man/getpass/3/markdown)
- [shadow(5)](https://www.chedong.com/phpMan.php/man/shadow/5/markdown)
- [passwd(5)](https://www.chedong.com/phpMan.php/man/passwd/5/markdown)
- [pam(8)](https://www.chedong.com/phpMan.php/man/pam/8/markdown)

## Exit Codes
The functions set `errno` on failure. See [Errors](#errors) for values.