Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the crypt() function declared in unistd.h or crypt.h?

Tags:

c

crypt

I'm using GCC 4.6.0 (on an otherwise unidentified platform).

I am using the crypt() function to encrypt a password.

I have never used that function before so I checked out the main page:

man 3 crypt

And it says to include the unistd.h header.

However, when I did that, I got an implicit warning for the crypt function.

warning: implicit declaration of function ‘crypt’ [-Wimplicit-function-declaration]

I did a bit of searching and I found that you have to include the crypt.h. However, how come it doesn't say that in the man page?

like image 359
ant2009 Avatar asked May 25 '11 16:05

ant2009


People also ask

What is crypt () in PHP?

The crypt() function returns a hashed string using DES, Blowfish, or MD5 algorithms. This function behaves different on different operating systems. PHP checks what algorithms are available and what algorithms to use when it is installed.

What does crypt() do?

crypt() is the password encryption function. It is based on the Data Encryption Standard algorithm with variations intended (among other things) to discourage use of hardware implementations of a key search. key is a user's typed password. salt is a two-character string chosen from the set [a-zA-Z0-9./].

How to use crypt function in c++?

Encoding PasswordsThe crypt() function generates an encoded version of each password. The first call to crypt() produces an encoded version of the old password; that encoded password is then compared to the password stored in the user database. The second call to crypt() encodes the new password before it is stored.

Is crypt secure?

crypt is considered to be cryptographically far too weak to withstand brute-force attacks by modern computing systems (Linux systems generally ship with GNU Privacy Guard which is considered to be reasonably secure by modern standards)


2 Answers

It also says #define _XOPEN_SOURCE (before including unistd.h) in my man page. So you should probably add it to expose the declaration of crypt.

EDIT

I just tried it. Including unistd.h and #define _XOPEN_SOURCE before it does the trick. Including it alone isn't enough.

Using

gcc version 4.6.0 20110429
GNU C Library stable release version 2.13

Looking into unistd.h:

/* XPG4.2 specifies that prototypes for the encryption functions must
   be defined here.  */
#ifdef  __USE_XOPEN
/* Encrypt at most 8 characters from KEY using salt to perturb DES.  */
extern char *crypt (__const char *__key, __const char *__salt)
     __THROW __nonnull ((1, 2));
like image 109
cnicutar Avatar answered Sep 28 '22 23:09

cnicutar


The POSIX standard for crypt() says that it should be declared in <unistd.h>, so that's what you need to include.

However, depending on what other compiler options you specify, you may or may not see it.

I currently use a header I call "posixver.h" which contains the code:

#ifndef JLSS_ID_POSIXVER_H
#define JLSS_ID_POSIXVER_H

/*
** Include this file before including system headers.  By default, with
** C99 support from the compiler, it requests POSIX 2001 support.  With
** C89 support only, it requests POSIX 1997 support.  Override the
** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
*/

/* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
/* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
/* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */

#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600   /* SUS v3, POSIX 1003.1 2004 (POSIX 2001 + Corrigenda) */
#else
#define _XOPEN_SOURCE 500   /* SUS v2, POSIX 1003.1 1997 */
#endif /* __STDC_VERSION__ */
#endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */

#endif /* JLSS_ID_POSIXVER_H */

On the systems where I work, setting _XOPEN_SOURCE to 700 would be an exercise in frustration and futility, however much I'd like to be able to do so. But these options normally make my code work correctly on Linux, HP-UX, MacOS X, AIX and Solaris - the Unix-like platforms I normally work on.

And this works when I set GCC into -std=c99 mode. If you use -std=gnu99, you probably don't need the header at all; it automatically enables C99 standard plus extensions.

Incidentally, I used to have this stanza at the top of individual source files. As the number of files containing the stanza grew (encroaching on hundreds of files), I realized that when I needed to adjust the settings, I had a monstrous editing job ahead of me. Now I have the one header and I'm retrofitting it into the files that have the stanza so I change one file (the header) to effect a change for all my code - once I've finished undoing the damage I did.

like image 22
Jonathan Leffler Avatar answered Sep 28 '22 22:09

Jonathan Leffler