Пример #1
0
int main(int argc, char **argv) {
	struct goh_state st;
	int opt;
	const char *prefixhash;
	char *append;
	size_t appendlen;
	enum decode appenddecode = DECODE_ECHO;
	size_t minlen = 0, maxlen = SHA_CBLOCK;


	/* Options parsing */
	goh_init(&st, opt_desc, ARRAY_LENGTH(opt_desc), argc, argv, 1);
	st.usagehelp = "[options] sha1(prefix) hex(append)\n";

	while ((opt = goh_nextoption(&st)) >= 0) {
		switch (opt) {
		case 'm':
			minlen = strtol(st.argval, NULL, 0);
			break;

		case 'M':
			maxlen = strtol(st.argval, NULL, 0);
			break;

		case 'd':
			if (strcmp(st.argval, "raw") == 0)
				appenddecode = DECODE_RAW;
			else if (strcmp(st.argval, "hex") == 0)
				appenddecode = DECODE_HEX;
			else if (strcmp(st.argval, "echo") == 0)
				appenddecode = DECODE_ECHO;
			else
				custom_error("%s not a valid value for --append-decode", st.argval);
			break;

		default:
			custom_error("Option declared but not handled");
		}
	}

	/* Common command line error */
	if (st.argidx + 2 != argc) {
		custom_warn("Missing mandatory arguments");
		goh_printhelp(&st, stderr);
		return EXIT_FAILURE;
	}

	prefixhash = argv[st.argidx];
	append = argv[st.argidx + 1];

	goh_fini(&st);


	/* Command line checking */
	if (strlen(prefixhash) != 2 * SHA_DIGEST_LENGTH)
		custom_error("sha1(prefix) must be %d hex characters long", 2 * SHA_DIGEST_LENGTH);

	/* Decode the append string */
	appendlen = decode_append(append, appenddecode);

	sha1append(prefixhash, append, appendlen, minlen, maxlen);

	return EXIT_SUCCESS;
}
Пример #2
0
int main(int argc, char **argv) {
	struct goh_state st;
	int opt;
	struct fs_ctx s;
	enum action action = ACTION_CRACK;
	char *key = NULL;
	const char *filenamein = "-";
	const char *filenameout = "-";
	char *text = NULL;
	struct charset cs;
	struct crack_args cka;

	cs_init(&cs);
	memset(&cka, 0, sizeof(cka));

	/* Parse the options. */
	goh_init(&st, opt_desc, ARRAY_LENGTH(opt_desc), argc, argv, 1);
	st.usagehelp = "[options]\n";

	while ((opt = goh_nextoption(&st)) >= 0) {
		switch (opt) {
		case 'i':
			filenamein = st.argval;
			break;

		case 'o':
			filenameout = st.argval;
			break;

		case 'e':
			action = ACTION_ENCRYPT;
			break;

		case 'd':
			action = ACTION_DECRYPT;
			break;

		case 'k':
			key = st.argval;
			break;

		case 'l':
			cka.klen = atoi(st.argval);
			break;

		case OPT_KASISKI_MIN_LENGTH:
			cka.ka_minlen = atoi(st.argval);
			break;

		case OPT_SHOW_KASISKI_TABLE:
			cka.ka_show_table = 1;
			break;

		case OPT_SHOW_KASISKI_LENGTH:
			cka.ka_show_length = 1;
			break;

		case 'c':
			cs_add(&cs, st.argval);
			break;

		default:
			custom_error("Unrecognized option (shouldn't happen)");
			break;
		}
	}


	/* Common command line mistake. */
	if (st.argidx != argc)
		custom_error("Useless argument %s", argv[st.argidx]);

	goh_fini(&st);

	/* Check for some invalid options combinations. */
	if (action != ACTION_CRACK && key == NULL)
		custom_error("Encryption and decryption take a --key");

	if (action == ACTION_CRACK && key != NULL)
		custom_error("--key need either --encrypt or --decrypt");

	if (cka.klen > 0 && key != NULL)
		custom_warn("Unnecessary key length option with an actual key");

	if (cka.klen > 0 && key != NULL && cka.klen != strlen(key))
		custom_error("Key length option doesn't match "
		             "the length of the key");

	if (cka.ka_minlen > 0 && action != ACTION_CRACK)
		custom_error("--kasiski-min-length can only be used in "
		             "cracking mode");

	if (cka.ka_minlen > 0 && cka.klen > 0)
		custom_warn("Useless option --kasiski-min-length when the key "
		            "length is given");

	if (cka.ka_show_table != 0 && action != ACTION_CRACK)
		custom_error("--show-kasiski-table can only be used in "
		             "cracking mode");

	if (cka.ka_show_table != 0 && cka.klen > 0)
		custom_warn("Option --show-kasiski-table ignored when a key "
		            "length is given");

	if (cka.ka_show_length != 0 && action != ACTION_CRACK)
		custom_error("--show-kasiski-length can only be used in "
		             "cracking mode");

	if (cka.ka_show_length != 0 && cka.klen > 0)
		custom_warn("Option --show-kasiski-length ignored when a key "
		            "length is given");

	/* Default charset. */
	if (cs.chars_size == 0) {
		cs_add(&cs, CHARSET_UPPER);
		cs_add(&cs, CHARSET_LOWER);
	}


	/* Start to do the job. */
	text = read_file(filenamein);
	fs_init(&s, text, &cs);
	cka.str = &s;

	if (action == ACTION_CRACK)
		crack(&cka);
	else
		simple_action(&s, key, action);

	write_file(filenameout, text);

	fs_fini(&s);
	free(text);
	cs_fini(&cs);


	return EXIT_SUCCESS;
}