Skip to content

Instantly share code, notes, and snippets.

@remko
Created January 15, 2012 15:25
Show Gist options
  • Save remko/1616161 to your computer and use it in GitHub Desktop.
Save remko/1616161 to your computer and use it in GitHub Desktop.
Native, command-line version of CryptoPass using Swiften
/*
* Native, command-line version of CryptoPass ( http://tinyurl.com/cryptopass )
* using the Swiften library ( http://swift.im/swiften )
*
* Copyright (c) 2012 Remko Tronçon
* Licensed under the GNU General Public License v3
*/
#include <Swiften/Base/Platform.h>
#include <Swiften/StringCodecs/PBKDF2.h>
#include <Swiften/StringCodecs/HMAC_SHA256.h>
#include <Swiften/StringCodecs/Base64.h>
#include <iostream>
#include <boost/lexical_cast.hpp>
#ifdef SWIFTEN_PLATFORM_WINDOWS
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif
using namespace Swift;
using namespace std;
int main(int argc, char* argv[]) {
string username, url, secret, password, lengthString;
cout << "Username: " << flush;
getline(cin, username);
cout << "URL: " << flush;
getline(cin, url);
#ifdef SWIFTEN_PLATFORM_WINDOWS
DWORD mode = 0; GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode & ~ENABLE_ECHO_INPUT);
#else termios attributes;
tcgetattr(STDIN_FILENO, &attributes);
termios newAttributes = attributes;
newAttributes.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newAttributes);
#endif
cout << "Secret: " << flush;
getline(cin, secret);
cout << endl;
#ifdef SWIFTEN_PLATFORM_WINDOWS
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode);
#else
tcsetattr(STDIN_FILENO, TCSANOW, &attributes);
#endif
cout << "Length (Default: 25): " << flush;
getline(cin, lengthString);
size_t length = 25;
if (!lengthString.empty()) {
length = boost::lexical_cast<size_t>(lengthString);
}
cout << "Result: " << Base64::encode(PBKDF2::encode<HMAC_SHA256>(createSafeByteArray(secret), createByteArray(username+"@"+url), 5000)).substr(0, length) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment