Skip to content

Instantly share code, notes, and snippets.

@jotaki
Created January 6, 2020 16:47
Show Gist options
  • Save jotaki/bec5cb1808f077ef42eb6228e4c18ce5 to your computer and use it in GitHub Desktop.
Save jotaki/bec5cb1808f077ef42eb6228e4c18ce5 to your computer and use it in GitHub Desktop.
get password into memory
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <termios.h>
int main(void)
{
char buf[BUFSIZ];
register int input, len = 0;
struct termios t_orig, t_new;
int i;
tcgetattr(STDIN_FILENO, &t_orig);
t_new = t_orig;
t_new.c_lflag &= ~ECHO;
t_new.c_lflag &= ~ICANON;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new);
printf("Enter password: ");
fflush(stdout);
while((input = fgetc(stdin)) != '\n') {
buf[len++] = (input ^ 42);
if(len >= sizeof(buf)-1)
break;
}
printf("\n");
fflush(stdout);
buf[len] = '\0';
tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_orig);
for(i=0;i<len;++i) {
printf("0x%02x (%c)", buf[i], isprint(buf[i]) ? buf[i] : '.');
}
printf("\n");
for(i=0;i<len;++i) {
printf("%c", buf[i] ^ 42);
}
printf("\n");
return 0;
}
@jotaki
Copy link
Author

jotaki commented Jan 6, 2020

I realized that this doesn't really work, as fgetc() uses an internal buffer for ungetc()

@jotaki
Copy link
Author

jotaki commented Jan 6, 2020

and read() can't use registers directly. (uses pointer to store buffer, which can't be a register)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment