# man > crypt_gensalt(3)

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

## Quick Reference

- `crypt_gensalt(NULL, 0, NULL, 0)` – generate salt for best available method with default cost
- `crypt_gensalt("$6$", 0, NULL, 0)` – generate salt for SHA-512 with default cost
- `crypt_gensalt(NULL, 5000, NULL, 0)` – generate salt with specified cost factor
- `crypt_gensalt_rn(prefix, count, rbytes, nrbytes, output, output_size)` – thread-safe version with supplied buffer
- `crypt_gensalt_ra(prefix, count, rbytes, nrbytes)` – thread-safe version that allocates memory
- `crypt_gensalt(NULL, 0, random_bytes, 16)` – supply your own random bytes
- `crypt_gensalt("", 0, NULL, 0)` – select traditional DES (weak, avoid)
- `crypt_gensalt_rn(NULL, 0, NULL, 0, buf, CRYPT_GENSALT_OUTPUT_SIZE)` – safe usage with buffer

## Name

`crypt_gensalt`, `crypt_gensalt_rn`, `crypt_gensalt_ra` — encode settings for passphrase hashing

## Synopsis

c
#include <crypt.h>

char *crypt_gensalt(const char *prefix, unsigned long count,
                    const char *rbytes, int nrbytes);

char *crypt_gensalt_rn(const char *prefix, unsigned long count,
                       const char *rbytes, int nrbytes,
                       char *output, int output_size);

char *crypt_gensalt_ra(const char *prefix, unsigned long count,
                       const char *rbytes, int nrbytes);
## Parameters

- `prefix` – selects hashing method; NULL for best available, "" for DES (weak)
- `count` – CPU time cost factor; 0 for default low cost
- `rbytes` – pointer to cryptographically random bytes; NULL to auto-generate from OS
- `nrbytes` – number of random bytes; ignored if rbytes is NULL
- `output` – (for `_rn` variant) buffer for result string
- `output_size` – (for `_rn` variant) size of output buffer; must be >= CRYPT_GENSALT_OUTPUT_SIZE

## Return Values

All three functions return a pointer to an encoded setting string (printable ASCII, no whitespace, colons, semicolons, asterisks, exclamation marks, or backslashes). On error, they return NULL and set `errno`.

- `crypt_gensalt` – result in static storage; overwritten by subsequent calls, not thread-safe
- `crypt_gensalt_rn` – result in supplied `output` buffer
- `crypt_gensalt_ra` – result in malloc'd memory; must be freed with `free(3)`

On error, `crypt_gensalt` and `crypt_gensalt_rn` write an invalid setting string (starting with '*') to the output buffer if space permits.

## Errors

- `EINVAL` – invalid or unsupported prefix; invalid count for prefix; insufficient `nrbytes` for smallest valid salt
- `ERANGE` – (`_rn` only) `output_size` too small
- `ENOMEM` – failed to allocate internal scratch memory; (`_ra` only) failed to allocate result string
- `ENOSYS`, `EACCES`, `EIO`, etc. – failed to obtain random bytes from OS (only when `rbytes` is NULL)

## Feature Test Macros

Defined in `<crypt.h>`:

- `CRYPT_GENSALT_IMPLEMENTS_DEFAULT_PREFIX` – NULL prefix is supported
- `CRYPT_GENSALT_IMPLEMENTS_AUTO_ENTROPY` – NULL `rbytes` is supported

## Portability Notes

These functions are not part of any standard; they originate from the Openwall project. A similar `crypt_gensalt` exists on Solaris 10+ but with different prototype and semantics. Default prefix and auto entropy available since libxcrypt 4.0.0. Supported hashing methods vary by system.

## Attributes

- `crypt_gensalt` – MT-Unsafe race:crypt_gensalt
- `crypt_gensalt_rn`, `crypt_gensalt_ra` – MT-Safe

## See Also

[crypt(3)](https://www.chedong.com/phpMan.php/man/crypt/3/markdown), [getpass(3)](https://www.chedong.com/phpMan.php/man/getpass/3/markdown), [getpwent(3)](https://www.chedong.com/phpMan.php/man/getpwent/3/markdown), [shadow(3)](https://www.chedong.com/phpMan.php/man/shadow/3/markdown), [login(1)](https://www.chedong.com/phpMan.php/man/login/1/markdown), [passwd(1)](https://www.chedong.com/phpMan.php/man/passwd/1/markdown), [crypt(5)](https://www.chedong.com/phpMan.php/man/crypt/5/markdown), [passwd(5)](https://www.chedong.com/phpMan.php/man/passwd/5/markdown), [shadow(5)](https://www.chedong.com/phpMan.php/man/shadow/5/markdown), [pam(8)](https://www.chedong.com/phpMan.php/man/pam/8/markdown)