crypt_gensalt, crypt_gensalt_rn, crypt_gensalt_ra β encode settings for passphrase hashing
| Use Case | Command | Description |
|---|---|---|
| π Generate salt for best available hash method | crypt_gensalt(NULL, 0, NULL, 0) | Auto-selects prefix and obtains random bytes from OS |
| π Generate salt with specific prefix (e.g., SHAβ512) | crypt_gensalt("$6$", 0, NULL, 0) | Uses $6$ (SHAβ512) with default cost |
| βοΈ Generate salt with custom cost | crypt_gensalt("$6$", 5000, NULL, 0) | Sets CPU cost to 5000 rounds |
| π Threadβsafe version (reentrant) | crypt_gensalt_rn(prefix, count, rbytes, nrbytes, output, output_size) | Writes to userβsupplied buffer; requires CRYPT_GENSALT_OUTPUT_SIZE |
| π§© Dynamically allocated salt | crypt_gensalt_ra(prefix, count, rbytes, nrbytes) | Returns mallocβed string; must be freed with free() |
| π Provide explicit random bytes | crypt_gensalt("$y$", 0, mybytes, 32) | Uses provided 32βbyte random buffer as salt |
Crypt Library (libcrypt, -lcrypt)
#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);
The crypt_gensalt, crypt_gensalt_rn, and crypt_gensalt_ra functions compile a string for use as the setting argument to crypt, crypt_r, crypt_rn, and crypt_ra. prefix selects the hashing method to use. count controls the CPU time cost of the hash; the valid range for count and the exact meaning of βCPU time costβ depends on the hashing method, but larger numbers correspond to more costly hashes. rbytes should point to nrbytes cryptographically random bytes for use as βsalt.β
If prefix is a null pointer, the best available hashing method will be selected. (β οΈ CAUTION: if prefix is an empty string, the βtraditionalβ DESβbased hashing method will be selected; this method is unacceptably weak by modern standards.) If count is 0, a low default cost will be selected. If rbytes is a null pointer, an appropriate number of random bytes will be obtained from the operating system, and nrbytes is ignored.
See crypt(5) for other strings that can be used as prefix, and valid values of count for each.
crypt_gensalt, crypt_gensalt_rn, and crypt_gensalt_ra return a pointer to an encoded setting string. This string will be entirely printable ASCII, and will not contain whitespace or the characters β:β, β;β, β*β, β!β, or β\β. See crypt(5) for more detail on the format of this string. Upon error, they return a null pointer and set errno to an appropriate error code.
CRYPT_GENSALT_OUTPUT_SIZE.Upon error, in addition to returning a null pointer, crypt_gensalt and crypt_gensalt_rn will write an invalid setting string to their output buffer, if there is enough space; this string will begin with a β*β and will not be equal to prefix.
The following macros are defined by <crypt.h>:
The functions crypt_gensalt, crypt_gensalt_rn, and crypt_gensalt_ra are not part of any standard. They originate with the Openwall project. A function with the name crypt_gensalt also exists on Solaris 10 and newer, but its prototype and semantics differ.
The default prefix and auto entropy features are available since libxcrypt version 4.0.0. Portable software can use feature test macros to find out whether null pointers can be used for the prefix and rbytes arguments.
The set of supported hashing methods varies considerably from system to system.
For an explanation of the terms used in this section, see attributes(7).
| Interface | Attribute | Value |
|---|---|---|
| crypt_gensalt | Thread safety | MT-Unsafe race:crypt_gensalt |
| crypt_gensalt_rn, crypt_gensalt_ra | Thread safety | MT-Safe |
crypt(3), getpass(3), getpwent(3), shadow(3), login(1), passwd(1), crypt(5), passwd(5), shadow(5), pam(8)
Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-17 22:33 @216.73.216.26
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)