Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************
- * MemGen: Version 1.0 *
- * Author: Michael Warren *
- * Date: November 8-9, 2018 *
- ****************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- void generate( void );
- void memorize( char * );
- void mode_switch();
- void command( char * );
- void nl_strip( char * );
- void input( void );
- void prompt( void );
- int length;
- char password[64];
- #define MEM_MODE 0
- #define CMD_MODE 1
- int mode = MEM_MODE;
- /* Switch between Command and Memorization modes */
- void mode_switch(){
- printf( "\e[2J\e[H" );
- if( mode == MEM_MODE ){
- printf( "Command Mode\nType \"help\" to see a list of available commands.\n" );
- mode = CMD_MODE;
- putchar( '\n' );
- }
- else{
- mode = MEM_MODE;
- putchar( '\n' );
- }
- }
- /* Replace newline character with null character */
- void nl_strip( char *str ){
- int len = strlen( str );
- for( int i = 0; i < len; i++ ){
- if( str[i] == '\n' ){
- str[i] = '\0';
- break;
- }
- }
- }
- int main( int argc, char **argv ){
- length = (argv[1] && atoi( argv[1] )) ? atoi( argv[1] ) : 12;
- generate();
- input();
- return 0;
- }
- /* Generate truly random password */
- void generate(){
- FILE *fp = fopen( "/dev/random", "r" );
- unsigned int c;
- for( int i = 0; i < length; i++ ){
- fread( &c, 4, 1, fp );
- c = c % 62;
- if( c < 26 )
- password[i] = 'A' + c;
- else if( c < 52 )
- password[i] = 'a' + c - 26;
- else
- password[i] = '0' + c - 52;
- password[length] = '\0';
- }
- putchar( '\n' );
- fclose( fp );
- printf( "\e[2J\e[H" );
- printf( "Generated password: %s\n", password );
- puts( "Press Enter to continue..." );
- getchar();
- }
- /* Portion aids in memorizing a password */
- void memorize( char *buf ){
- if( !strcmp( buf, ":command" ) ){
- mode_switch();
- goto end;
- }
- else if( !strcmp( buf, password ) ) printf( "Correct\n" );
- else if( !strncmp( buf, password, strlen( buf ) ) ) printf( "Partially correct\n" );
- else printf( "Incorrect\n" );
- sleep( 1 );
- end:
- sleep( 0 ); // Placeholder for label
- }
- /* Print prompt based on what mode you're in */
- void prompt(){
- if( mode == CMD_MODE ){
- printf( "> " );
- }
- else{
- printf( "\e[2J\e[H" );
- printf( "Memorization Mode\nEnter :command to switch to Command Mode.\n" );
- printf( "Enter password: " );
- }
- fflush( stdout );
- }
- /* Input loop */
- void input(){
- char buf[64];
- buf[63] = '\0'; // Prevents buffer overflow attacks
- for(;;){
- prompt();
- fgets( buf, 63, stdin );
- nl_strip( buf );
- if( mode == CMD_MODE ) command( buf );
- else memorize( buf );
- }
- }
- /* Input loop for Command mode */
- void command( char *buf ){
- if( !strcmp( buf, "generate" ) ){
- generate();
- }
- else if( !strcmp( buf, "change-length" ) ){
- printf( "Enter new length: " );
- fflush( stdout );
- fgets( buf, 63, stdin );
- length = atoi( buf ) ? atoi( buf ) : 12;
- }
- else if( !strcmp( buf, "select-new" ) ){
- printf( "Enter new password: " );
- fflush( stdout );
- fgets( buf, 63, stdin );
- strncpy( password, buf, strlen( buf ) );
- nl_strip( password );
- }
- else if( !strcmp( buf, "print" ) ){
- puts( password );
- }
- else if( !strcmp( buf, "memorize" ) ){
- mode_switch();
- }
- else if( !strcmp( buf, "exit" ) ){
- exit( 0 );
- }
- else if( !strcmp( buf, "help" ) ){
- FILE *help;
- if( !(help = fopen( "memgen_help.txt", "r" )) ){
- puts( "Could not locate help file." );
- }
- int c;
- while( (c = fgetc( help )) != EOF ){
- putchar( c );
- }
- }
- }
Add Comment
Please, Sign In to add comment