Skip to content

Instantly share code, notes, and snippets.

@dzmitryk
Created February 23, 2014 15:00
Show Gist options
  • Save dzmitryk/9172470 to your computer and use it in GitHub Desktop.
Save dzmitryk/9172470 to your computer and use it in GitHub Desktop.
Call UNIX crypt function from command line. Compile using: $gcc crypt.c -lcrypt -o crypt
#include <stdio.h>
#include <unistd.h>
#include <crypt.h>
int main(int argc, char **argv) {
int option;
int debug = 0;
const char *salt;
const char *pass;
while ((option = getopt(argc, argv, "p:s:d")) != -1) {
switch (option) {
case 'p':
pass = optarg;
break;
case 's':
salt = optarg;
break;
case 'd':
debug = 1;
break;
default:
fprintf(stderr, "unknown parameter\n");
return 1;
}
}
if (pass == NULL) {
fprintf(stderr, "password not specified\n");
return 1;
}
if (salt == NULL) {
fprintf(stderr, "salt not specified\n");
return 1;
}
if (debug) {
fprintf(stdout, "password:%s\nsalt:%s\n\n", pass, salt);
}
const char *out = crypt(pass, salt);
if (out != NULL) {
fprintf(stdout, "%s\n", out);
} else {
fprintf(stderr, "computation failed\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment