Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Last active June 20, 2016 18:50
Show Gist options
  • Save ecounysis/f5e48cf3f39d321f7eaf8e226e8cc07a to your computer and use it in GitHub Desktop.
Save ecounysis/f5e48cf3f39d321f7eaf8e226e8cc07a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 65536
char pw[MAXSIZE];
void print_usage();
int randomizebuff(char *buff, int start, int end, int buffsize);
int number(char *st);
int main(int argc, char *argv[])
{
srand(time(NULL));
// have to do `srand` in main,
// else it will seed the same value each time through `randomize`
//and repeat the same random sequences
int thisnum=0;
int pwindex=0;
if (argc < 2) {
pwindex = 12;
randomizebuff(pw, 0, pwindex, MAXSIZE);
}
else if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0)) {
print_usage(argv[0]);
return 1;
}
else {
for (int i=1; i<argc; i++) {
if((thisnum=number(argv[i]))) {
if (!randomizebuff(pw, pwindex, pwindex+thisnum, MAXSIZE)) {
printf("oops\n");
return 1;
}
pwindex+=thisnum;
}
else {
for(int j=0; j<strlen(argv[i]); j++)
pw[pwindex++] = argv[i][j];
}
}
}
pw[pwindex]='\0';
printf("%s\n", pw);
return 0;
}
int number(char *buff) {
char c;
int i=0;
int val=0;
while((c = buff[i++]) != '\0') {
if (c >= '0' && c <= '9') {
val = val*10 + atoi(&c);
}
else {
return 0;
}
}
return val;
}
int randomizebuff(char *buff, int start, int end, int buffsize)
{
char vals[64] = "023456789abcdefghikmnopqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ!@#$%^&*";
if (end >= buffsize || start < 0) {
return 0; // not enough space
}
for (int i=start; i<end; i++)
{
buff[i] = vals[rand()%64];
}
return 1;
}
void print_usage(char *st)
{
printf("%s\n\t%s%s\n\t%s %s\n\t%s", "usage:", st, " num str num str ...", st,
"creates a random password by appending num random chars to str",
"For example, with inputs of 'asdf', '5', and 'zwr'\n\tit would output something like 'asdfc4q80zwr'");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment