man > crypt_gensalt(3)

πŸ“› NAME

crypt_gensalt, crypt_gensalt_rn, crypt_gensalt_ra β€” encode settings for passphrase hashing

πŸš€ Quick Reference

Use CaseCommandDescription
πŸ”‘ Generate salt for best available hash methodcrypt_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 costcrypt_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 saltcrypt_gensalt_ra(prefix, count, rbytes, nrbytes)Returns malloc‑ed string; must be freed with free()
πŸ”’ Provide explicit random bytescrypt_gensalt("$y$", 0, mybytes, 32)Uses provided 32‑byte random buffer as salt

πŸ“š LIBRARY

Crypt Library (libcrypt, -lcrypt)

πŸ“– SYNOPSIS

#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);

πŸ“ DESCRIPTION

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.

πŸ“¬ RETURN VALUES

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.

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.

⚠️ ERRORS

πŸ”¬ FEATURE TEST MACROS

The following macros are defined by <crypt.h>:

🌍 PORTABILITY NOTES

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.

🏷️ ATTRIBUTES

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

InterfaceAttributeValue
crypt_gensaltThread safetyMT-Unsafe race:crypt_gensalt
crypt_gensalt_rn, crypt_gensalt_raThread safetyMT-Safe

πŸ”— SEE ALSO

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

crypt_gensalt(3)
πŸ“› NAME πŸš€ Quick Reference πŸ“š LIBRARY πŸ“– SYNOPSIS πŸ“ DESCRIPTION πŸ“¬ RETURN VALUES ⚠️ ERRORS πŸ”¬ FEATURE TEST MACROS 🌍 PORTABILITY NOTES 🏷️ ATTRIBUTES πŸ”— 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:33 @216.73.216.26
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_^