diff options
author | Seppia <nonso@insicuri.net> | 2016-06-09 03:05:54 +0200 |
---|---|---|
committer | Seppia <nonso@insicuri.net> | 2016-06-09 03:05:54 +0200 |
commit | 6d851d490732b6423a523d452336583c262829fe (patch) | |
tree | c14078aee354be50b796b8d936d0ccb0eb0f5c5f | |
parent | a85a21d3084cdb6d10bb8dd6d833a4085344418a (diff) | |
download | onetimebluh-6d851d490732b6423a523d452336583c262829fe.tar.gz onetimebluh-6d851d490732b6423a523d452336583c262829fe.tar.bz2 onetimebluh-6d851d490732b6423a523d452336583c262829fe.zip |
added options input
-rw-r--r-- | keygen.c | 56 |
1 files changed, 47 insertions, 9 deletions
@@ -1,14 +1,52 @@ #include<stdio.h> #include<openssl/rand.h> +#include<unistd.h> int main(int argc, char* argv[]) { - long long int len = strtoll(argv[1], NULL, 10); - unsigned char key[len]; - RAND_bytes(key, len); - FILE* file = fopen(argv[2], "w"); - fwrite(key, len, 1, file); - fclose(file); - printf("Created key file %s of %lli bytes \n", argv[2], len); - - return 0; + + int opt; + int nbytes = 256; + char* output; + int defout = 0; + int defbyte = 0; + while ((opt = getopt(argc, argv, "b:o:")) != -1) { + switch (opt) { + case 'b': + nbytes = atoi(optarg); + defbyte = 1; + break; + case 'o': + output = argv[(optind-1)]; + defout = 1; + break; + default: + fprintf(stderr, "Usage: %s [-b nbytes] [-o output] \n", argv[0]); + exit(EXIT_FAILURE); + } + } + + if (nbytes < 0) { + printf("Negative byte value inserted! \n"); + printf("Exiting... \n"); + exit(EXIT_FAILURE); + } else if (nbytes != 0) { + if (defbyte == 0) { + printf("No byte number specified... using default value: 256 \n"); + } + unsigned char key[nbytes]; + RAND_bytes(key, nbytes); + if (defout == 0) { + output = "default.key"; + printf("No output name specified... using default value: default.key \n"); + } + FILE* file = fopen(output, "w"); + fwrite(key, nbytes, 1, file); + fclose(file); + printf("Created key file %s of %d bytes \n", output, nbytes); + } else { + printf("Byte number specified is 0. \n"); + printf("Doing nothing! \n"); + } + + exit(EXIT_SUCCESS); } |