/*
  Author: Ben Carpenter (www.bencarpenter.co.uk/software)
  Date:   24th May 2005

  'Secure' Password Generator (8 alpha-numeric characters), starting with a 
  letter and containing a minimum of one number. Uses the ASCII character set,
  with a built-in pause such that the same password cannot be generated on a
  subsequent run of the program.
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int Binary() { return rand()%2; }
int Number() { return rand()%10; }
char Letter() { return (char)(rand()%26 + 97); }

int main()
{
  printf("\n RANDOM 8-CHARACTER ALPHA-NUMERIC PASSWORDS\n");

  time_t start, now;
  int i, j;
  int secure;       // reads zero if false, else reads non-zero (true)
  int N = 8;        // Number of Password Characters
  int Code[N-1];
  srand( (unsigned int)time(&start) );

  printf(" Please choose from:\n\n");
  for(i=0; i<3; ++i) // give three passwords
  {
    printf("\t");
    secure = 0;
    printf("%c", Letter());   // always start with a letter
    for(j=0; j<N-2; ++j)
    {
      Code[j] = Binary();
      if(Code[j] == 0) printf("%c", Letter());
      else printf("%d", Number());
      secure += Code[j];
    }
    if(secure == 0) printf("%d", Number()); // if we only have letters, give a number
    else if(Binary() == 0) printf("%c", Letter());
         else printf("%d", Number());
    printf("\n");
  }

  do    // hold for 1 second
  {
    time(&now);
  } while( now < start+1 );

  return 0;
}
