initial commit
[mnenc] / password.cpp
1 //      password.cpp
2 //      
3 //      Copyright 2010 Micke Nordin <mickewiki@gmail.com>
4 //      
5 //      This program is free software; you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation; either version 3 of the License, or
8 //      (at your option) any later version.
9 //      
10 //      This program is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14 //      
15 //      You should have received a copy of the GNU General Public License
16 //      along with this program; if not, write to the Free Software
17 //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 //      MA 02110-1301, USA.
19
20 #include "password.hpp"
21 #include <iostream>
22 #include <fstream>
23 password::password(std::string enc, std::string dec, std::string k) {
24         encryptedpw = enc;
25         decryptedpw = dec;
26         key = k;
27 }
28 void password::to_string() {
29         std::cout << "Encrypted: " << encryptedpw << std::endl;
30         std::cout << "Decrypted: " << decryptedpw << std::endl;
31 }
32
33 bool password::to_file(std::string filename) {
34         bool worked = false;
35         std::ofstream keyfile;
36         if(keyfile.open(filename.c_str())) {
37                 keyfile << encryptedpw;
38                 keyfile.close();
39                 worked = true;
40         }
41         return worked;
42 }
43
44 bool password::from_file(std::string filename) {
45         bool worked = false;
46         std::ifstream keyfile(filename.c_str());
47         std::string k = "";
48         std::string line = "";
49         if (keyfile.is_open()) {
50                 while (! keyfile.eof() ) {
51                         getline (keyfile,line);
52                         k += line;
53                 }
54                 keyfile.close();
55                 worked = true;
56         }
57         key = k;
58         return worked;
59 }
60