Link to home
Start Free TrialLog in
Avatar of Bvm 18
Bvm 18

asked on

crypt() api returning NULL

Hi all,

In C,

crypt(userpassword, etc-password) always returning NULL in other application.

In my simple test application, working fine.

 What could be the reason?  How to fix this?

Thanks,
Bvm
Avatar of David Favor
David Favor
Flag of United States of America image

Provide your entire source file + build command sequence.
https://serverfault.com/questions/330069/how-to-create-an-sha-512-hashed-password-for-shadow provides... what looks to be... a working example of calling crypt() correctly.
Have you confirmed the data being passed into crypt is actually valid? Check errno as this is set when crypt fails. It will tell you why.
Avatar of Bvm 18
Bvm 18

ASKER

Arguments to crypt()  function is correct. crypt() api returning NULL. In which cases, it would return NULL.

How to get the errno if crypt returns NULL
http://man7.org/linux/man-pages/man3/errno.3.html

"The <errno.h> header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong."
You really want to print the textual representation of errno
#include <stdio.h>
#include <errno.h>
...
perror("crypt")
...

Open in new window

or, fancier but more informative
#include <stdio.h>
#include <errno.h>
#include <string.h>
...
fprintf(stderr, "%s. key=\"%s\", salt=\"%s\" (crypt)\n", strerror(errno), key, salt);
...

Open in new window

Substitute the variable names you use for key and salt
ASKER CERTIFIED SOLUTION
Avatar of MURUGESAN N
MURUGESAN N
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial