Skip to content

Instantly share code, notes, and snippets.

@ErisianArchitect
Created February 26, 2012 20:56
Show Gist options
  • Save ErisianArchitect/1918947 to your computer and use it in GitHub Desktop.
Save ErisianArchitect/1918947 to your computer and use it in GitHub Desktop.
Serial Key Generation with a format.
int generateKey(char* dest,char* format,unsigned int seed)
{
if(dest && format)
{
srand(seed);
int len = strlen(format);
for(int i = 0; i < len; i++)
{
switch(format[i])
{
case '#':
dest[i] = (char)((rand() % 9) + '1');
break;
case 'A':
dest[i] = (char)((rand() % 26) + 'A');
break;
case 'a':
dest[i] = (char)((rand() % 26) + 'a');
break;
case '-':
dest[i] = '-';
break;
case '_':
if((rand() % 2) == 0)
{
dest[i] = (char)((rand() % 9) + '1');
}
else
{
dest[i] = (char)((rand() % 26) + 'A');
}
break;
default:
switch(rand() % 3)
{
case 0:
dest[i] = (char)((rand() % 9) + '1');
break;
case 1:
dest[i] = (char)((rand() % 26) + 'A');
break;
case 2:
dest[i] = (char)((rand() % 26) + 'a');
break;
}
break;
}
}
dest[len] = 0;
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment