/*
 * makekey.8 - generate encrypted password, given a key and a salt,
 *    taken from standard input. Key is between zero to 8 characters,
 *    the salt is always the last 2 characters of a given input.
 *    Input of less than 2 characters are invalid and will output
 *    nothing.
 * 
 * This makekey is compatible with busybox's cryptpw/mkpasswd.
 * To compile: gcc -o makekey -lcrypt makekey.c
 * 
 * This implementation is Copyright (C) James Budiono 2015
 * License: GNU GPL Version 3 or later.
 * 
 * Why the need for this deprecated tool? xlockmore uses it.
 */
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void) {
	char input[8+2+1]; // 8 bytes for key + 2 bytes for salt + NUL
	char key[9];       // 8 bytes for key + NUL
	char salt[3];      // 2 bytes salt + NUL
	int len;
	
	fgets(input, sizeof(input), stdin);
	len = strlen(input);
	if (len && input[len-1]=='\n') {
		len--;
		input[len]=0;
	}
	
	if (len < 2) exit(1);
	strncpy(key, input, len-2);
	key[len-2]=0;
	strncpy(salt, input+len-2, sizeof(salt));
	salt[sizeof(salt)-1]=0;
	//printf("%s [%s] %s\n",input, keybuf, salt);
	
	printf("%s\n", crypt(key,salt));
	exit(0);
}
