using crypt

Hi

am attempting to use crypt to generate SHA512 hashes.
copied code from http://www.gnu.org/software/libc/manual/html_node/crypt.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <crypt.h>

int main() {
  unsigned long seed[2];
  char salt[] = "$6$........";
  const char *const seedchars =
  "./0123456789ABCDEFGHIJKLMNOPQRST"
  "UVWXYZabcdefghijklmnopqrstuvwxyz";
  char *password;
  int i;
  seed[0] = time(NULL);
  seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);

  for (i=0; i<8; i++)
    salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];

  password = crypt("Password1", salt);

  puts(password);
  return 0;
}

the following error is showing though:
c:25: undefined reference to `crypt'

crypt.h does exist though
less +/crypt /usr/include/crypt.h

any ideas?
Last edited on
That actually looks like a linker error. How are you compiling and linking?
Definitely a linker error. Add -lcrypt to your linker arguments.


Hi
I've only ever compiled as follows:
gcc -std=c99 -Wall -g -c -o file.o file.c

I've tried the following options:
1
2
3
4
5
6
7
8
9
10
11
12
$ gcc -std=c99 -Wall -g -l crypt  -c -o file.o file.c
gcc: -lcrypt: linker input file unused because linking not done
$ gcc -std=c99 -Wall -g -lcrypt  -c -o file.o file.c
gcc: -lcrypt: linker input file unused because linking not done
$ gcc -std=c99 -Wall -g -c -o -lcrypt file.o file.c
gcc: file.o: linker input file unused because linking not done
$ gcc -std=c99 -Wall -g -c -o file.o -lcrypt file.c
gcc: -lcrypt: linker input file unused because linking not done
$ gcc -std=c99 -Wall -g -c -o file.o -l crypt file.c
gcc: -lcrypt: linker input file unused because linking not done
$ gcc -std=c99 -Wall -g -c -o -lcrypt file.o file.c
gcc: file.o: linker input file unused because linking not done


Could you suggest a way to include linking for crypt.h?
thanks

not to worry, figured it out eventually (-_-)

gcc -std=c99 file.c -lcrypt -o file
Topic archived. No new replies allowed.