Skip to content

Instantly share code, notes, and snippets.

@bwkam
Created December 29, 2025 20:25
Show Gist options
  • Select an option

  • Save bwkam/509e5da22369e1518df123b9e2a48139 to your computer and use it in GitHub Desktop.

Select an option

Save bwkam/509e5da22369e1518df123b9e2a48139 to your computer and use it in GitHub Desktop.
pass
#include <iostream>
#include <termios.h>
#include <unistd.h>
using namespace std;
// no conio on linux
// thanks AI
// This function manually implements the logic of getch() on Linux
char getch()
{
char buf = 0;
struct termios old = {0};
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON; // Disable buffered I/O
old.c_lflag &= ~ECHO; // Disable echo
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if (read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON; // Restore buffered I/O
old.c_lflag |= ECHO; // Restore echo
if (tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
return (buf);
}
int main()
{
cout << "Enter password: " << flush;
char *right_pass = "1234";
int right_len = 4;
char *pass = new char;
char c = 0;
int idx = 0;
while (true)
{
c = getch();
if (c == '\n')
{
break;
}
cout << "*" << flush;
pass[idx] = c;
idx++;
}
cout << endl;
int len = 0;
while (*pass != NULL)
{
pass++;
len++;
}
for (int i = len; i > 0; i--)
{
pass--;
}
bool verified = true;
// validate
if (right_len == len)
{
while (*pass != NULL)
{
if (*pass != *right_pass)
{
verified = false;
}
pass++;
right_pass++;
}
}
else
{
verified = false;
}
cout << "Verified: " << (verified ? "true" : "false") << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment