First sucessfull comunication
[mnenc] / mnencd.hpp
1 //      mnencd.hpp
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 #ifndef _mnencd_h_included_
20 #define _mnencd_h_included_
21 #include <string>
22 #include <iostream>
23 #include <fstream>
24
25 std::string remove_char(std::string str, char c) {
26         std::string::size_type k = 0;
27         while((k=str.find(c,k))!=str.npos) {
28                 str.erase(k, 1);
29         }
30         return str;
31 }
32
33 std::string remove_chars(std::string str) {
34         std::string chars = " \t\n\b\a-?+\\{[]}'*'";
35         for(int i = 0; i < (signed) chars.size(); i++) {
36                 str = remove_char(str, chars[i]);
37         }
38         
39         return str;     
40 }
41
42 std::string make_filename(std::string user, std::string app) {
43         return remove_chars(app + user);
44 }
45 std::string get_password(std::string masterpasswd, std::string user, std::string app) {
46         mnenc menc = mnenc();
47         menc.genkey(masterpasswd);
48         std::string key = menc.get_key();
49         password pw = password("", "", key);
50         pw.from_file(make_filename(user, app));
51         return menc.decrypt(key, pw.get_enc());
52 }
53
54 void put_password(std::string masterpasswd, std::string passwd, std::string user, std::string app) {
55         mnenc menc = mnenc();
56         menc.genkey(masterpasswd);
57         std::string key = menc.get_key();
58         password pw = password(menc.encrypt(key, passwd ), "", key);
59         pw.to_file(make_filename(user, app));
60 }
61
62 std::string do_something(std::string str) {
63         if(str[0] == '0')       {
64                 return "201_Created\n";
65         } else {
66                 return "400_Bad_Request " + str + '\n';
67         }
68 }
69
70 std::string m_read() {
71         std::string str;
72         std::ifstream is("fife");
73         getline(is, str);
74         is.close();
75         return str;
76 }
77
78 void m_send(std::string message) {
79         std::ofstream os("fife");
80         os << message;
81         os.close();
82 }
83 #endif