Which is the encryption method used on /etc/shadow
on GNU/Linux systems? I would like to write a small program for personal purpose that uses the same API, but at the moment I don't know where to start.
Thanks in advance
Which is the encryption method used on /etc/shadow
on GNU/Linux systems? I would like to write a small program for personal purpose that uses the same API, but at the moment I don't know where to start.
Thanks in advance
Use the crypt(3)
function. On glibc, the method used depends on the salt, if it starts with:
Multiple encryption methods are available in glibc, see man 3 crypt, the Glibc Notes section: http://manpages.courier-mta.org/htmlman3/crypt.3.html
When verifying an existing password, just pass the encrypted form as salt; only the initial $id$salt part will be used. When creating new password, initialize id with whatever you need and put some random characters in salt.
basic example with crypt()
#include <stdio.h>
#include <stdlib.h>
#define MAX_STR 256
#define MAX_SALT 12
int main(int argc, char *argv[]) {
char password[MAX_STR];
char salt[MAX_SALT];
printf("salt: ");
scanf("%s", salt);
printf("password: ");
scanf("%s", password);
printf("Encrypt '%s' : '%s'\n", password, crypt(password, salt));
return(EXIT_SUCCESS);
}
Compile program:
$ gcc -lcrypt test.c
I receive
undefined reference to `crypt'
so I think that you should compile with
$ gcc test.c -lcrypt