# crypt(3) - man - phpman

- Encrypt a file:
  `crypt -e fileToEncrypt outputFile`
- Decrypt a file:
  `crypt -d fileToDecrypt outputFile`

*Source: cheat.sh*

---

[CRYPT(3)](https://www.chedong.com/phpMan.php/man/CRYPT/3/markdown)                 BSD Library Functions Manual                 [CRYPT(3)](https://www.chedong.com/phpMan.php/man/CRYPT/3/markdown)

## NAME
     **crypt**, **crypt**___**r**, **crypt**___**rn**, **crypt**___**ra** — passphrase hashing

## LIBRARY
     Crypt Library (libcrypt, -lcrypt)

## 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)](https://www.chedong.com/phpMan.php/man/shadow/5/markdown)) 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)](https://www.chedong.com/phpMan.php/man/crypt/5/markdown).

     _setting_ controls which hashing method to use, and also supplies various parameters to the cho‐
     sen 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 phrase[CRYPT_MAX_PASSPHRASE_SIZE];
               char initialized;
           };

     Upon a successful return from **crypt**___**r**, the hashed passphrase will be stored in _output_.  Appli‐
     cations are encouraged, but not required, to use the _phrase_ and _setting_ fields to store the
     strings that they will pass as _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 be‐
     fore storing anything in _setting_ and _phrase_.)

     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 (ex‐
     cept for the _phrase_ 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 initial‐
     ize a _struct_ _crypt_data_ object, using [malloc(3)](https://www.chedong.com/phpMan.php/man/malloc/3/markdown), and write its address and size into the vari‐
     ables pointed to by _data_ and _size_.  These can be reused in subsequent calls.  After the appli‐
     cation is done hashing passphrases, it should deallocate the _struct_ _crypt_data_ object using
     [free(3)](https://www.chedong.com/phpMan.php/man/free/3/markdown).

## 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)](https://www.chedong.com/phpMan.php/man/crypt/5/markdown) 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.

     **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.

     Upon error, **crypt**___**r**, **crypt**___**rn**, and **crypt**___**ra** write an _invalid_ hashed passphrase 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.

## 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 sys‐
                        tems.

## 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 por‐
     table 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 hashed passphrase 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 de‐
     fined 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 compatibil‐
     ity.  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)](https://www.chedong.com/phpMan.php/man/malloc/3/markdown).

## ATTRIBUTES
     For an explanation of the terms used in this section, see [attributes(7)](https://www.chedong.com/phpMan.php/man/attributes/7/markdown).
     ┌───────────────────┬───────────────┬──────────────────────┐
     │**Interface**          │ **Attribute**     │ **Value**                │
     ├───────────────────┼───────────────┼──────────────────────┤
     │**crypt**              │ Thread safety │ MT-Unsafe race:crypt │
     ├───────────────────┼───────────────┼──────────────────────┤
     │**crypt**___**r**, **crypt**___**rn**, │ Thread safety │ MT-Safe              │
     │**crypt**___**ra**           │               │                      │
     └───────────────────┴───────────────┴──────────────────────┘

## 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)](https://www.chedong.com/phpMan.php/man/cryptgensalt/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)

## Openwall Project               October 11, 2017               Openwall Project
