Skip to content

Instantly share code, notes, and snippets.

@bogado
Created April 27, 2010 11:52
Show Gist options
  • Save bogado/380657 to your computer and use it in GitHub Desktop.
Save bogado/380657 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <stdexcept>
#define _XOPEN_SOURCE
#include <unistd.h>
#include <termios.h>
#include <stdio.h>
/*
Usa a função "crypt" do posix para criar o hash usado no passwd ou no shadow
Uses the posix funciton "crypt" to create the same password hash used on passwd and/or shadow
*/
/** Classe para colocar o stdout no modo de entrada de password.
* Baseado no código exemplo da man page do getpass(3).
*
* this class puts the stduot in the password entry mode, based on the sample code on
* the getpass(3) man page.
*/
class Pass
{
public:
Pass()
{
tcgetattr (1, &old);
n = old;
n.c_lflag &= ~ECHO;
if (tcsetattr (1, TCSAFLUSH, &n) != 0)
throw std::runtime_error("could not set attr");
}
~Pass()
{
tcsetattr (1, TCSAFLUSH, &old);
}
private:
struct termios old, n;
};
int main()
{
std::string pass;
std::string salt;
std::cout << "passwd : ";
std::flush(std::cout);
{
Pass no_echo;
std::cin >> pass;
}
std::cout << "\nSalt : ";
std::flush(std::cout);
std::cin >> salt;
std::cout << crypt(pass.c_str(), salt.c_str()) << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment