AnonymousNamefag

memgen.c

Nov 9th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. /****************************
  2.  * MemGen: Version 1.0      *
  3.  * Author: Michael Warren   *
  4.  * Date: November 8-9, 2018 *
  5.  ****************************/
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11.  
  12. void generate( void );
  13. void memorize( char * );
  14. void mode_switch();
  15. void command( char * );
  16. void nl_strip( char * );
  17. void input( void );
  18. void prompt( void );
  19.  
  20. int length;
  21.  
  22. char password[64];
  23.  
  24. #define MEM_MODE 0
  25. #define CMD_MODE 1
  26.  
  27. int mode = MEM_MODE;
  28.  
  29. /* Switch between Command and Memorization modes */
  30. void mode_switch(){
  31.     printf( "\e[2J\e[H" );
  32.     if( mode == MEM_MODE ){
  33.         printf( "Command Mode\nType \"help\" to see a list of available commands.\n" );
  34.         mode = CMD_MODE;
  35.         putchar( '\n' );
  36.     }
  37.     else{
  38.         mode = MEM_MODE;
  39.         putchar( '\n' );
  40.     }
  41. }
  42.  
  43. /* Replace newline character with null character */
  44. void nl_strip( char *str ){
  45.     int len = strlen( str );
  46.     for( int i = 0; i < len; i++ ){
  47.         if( str[i] == '\n' ){
  48.             str[i] = '\0';
  49.             break;
  50.         }
  51.     }
  52. }
  53.  
  54. int main( int argc, char **argv ){
  55.     length = (argv[1] && atoi( argv[1] )) ? atoi( argv[1] ) : 12;
  56.     generate();
  57.     input();
  58.     return 0;
  59. }
  60.  
  61. /* Generate truly random password */
  62. void generate(){
  63.     FILE *fp = fopen( "/dev/random", "r" );
  64.     unsigned int c;
  65.     for( int i = 0; i < length; i++ ){
  66.         fread( &c, 4, 1, fp );
  67.         c = c % 62;
  68.         if( c < 26 )
  69.             password[i] = 'A' + c;
  70.         else if( c < 52 )
  71.             password[i] = 'a' + c - 26;
  72.         else
  73.             password[i] = '0' + c - 52;
  74.         password[length] = '\0';
  75.     }
  76.     putchar( '\n' );
  77.     fclose( fp );
  78.     printf( "\e[2J\e[H" );
  79.     printf( "Generated password: %s\n", password );
  80.     puts( "Press Enter to continue..." );
  81.     getchar();
  82. }
  83.  
  84. /* Portion aids in memorizing a password */
  85. void memorize( char *buf ){
  86.     if( !strcmp( buf, ":command" ) ){
  87.         mode_switch();
  88.         goto end;
  89.     }
  90.     else if( !strcmp( buf, password ) ) printf( "Correct\n" );
  91.     else if( !strncmp( buf, password, strlen( buf ) ) ) printf( "Partially correct\n" );
  92.     else printf( "Incorrect\n" );
  93.     sleep( 1 );
  94. end:
  95.     sleep( 0 ); // Placeholder for label
  96. }
  97.  
  98. /* Print prompt based on what mode you're in */
  99. void prompt(){
  100.     if( mode == CMD_MODE ){
  101.         printf( "> " );
  102.     }
  103.     else{
  104.         printf( "\e[2J\e[H" );
  105.         printf( "Memorization Mode\nEnter :command to switch to Command Mode.\n" );
  106.         printf( "Enter password: " );
  107.     }
  108.     fflush( stdout );
  109. }
  110.  
  111. /* Input loop */
  112. void input(){
  113.     char buf[64];
  114.     buf[63] = '\0'; // Prevents buffer overflow attacks
  115.     for(;;){
  116.         prompt();
  117.         fgets( buf, 63, stdin );
  118.         nl_strip( buf );
  119.         if( mode == CMD_MODE ) command( buf );
  120.         else memorize( buf );
  121.     }
  122. }
  123.  
  124. /* Input loop for Command mode */
  125. void command( char *buf ){
  126.     if( !strcmp( buf, "generate" ) ){
  127.         generate();
  128.     }
  129.     else if( !strcmp( buf, "change-length" ) ){
  130.         printf( "Enter new length: " );
  131.         fflush( stdout );
  132.         fgets( buf, 63, stdin );
  133.         length = atoi( buf ) ? atoi( buf ) : 12;
  134.     }
  135.     else if( !strcmp( buf, "select-new" ) ){
  136.         printf( "Enter new password: " );
  137.         fflush( stdout );
  138.         fgets( buf, 63, stdin );
  139.         strncpy( password, buf, strlen( buf ) );
  140.         nl_strip( password );
  141.     }
  142.     else if( !strcmp( buf, "print" ) ){
  143.         puts( password );
  144.     }
  145.     else if( !strcmp( buf, "memorize" ) ){
  146.         mode_switch();
  147.     }
  148.     else if( !strcmp( buf, "exit" ) ){
  149.         exit( 0 );
  150.     }
  151.     else if( !strcmp( buf, "help" ) ){
  152.         FILE *help;
  153.         if( !(help = fopen( "memgen_help.txt", "r" )) ){
  154.             puts( "Could not locate help file." );
  155.         }
  156.         int c;
  157.         while( (c = fgetc( help )) != EOF ){
  158.             putchar( c );
  159.         }
  160.     }
  161. }
Add Comment
Please, Sign In to add comment