summaryrefslogtreecommitdiffabout
path: root/lib/secret.cc
Unidiff
Diffstat (limited to 'lib/secret.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/secret.cc61
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/secret.cc b/lib/secret.cc
new file mode 100644
index 0000000..ae8a3c5
--- a/dev/null
+++ b/lib/secret.cc
@@ -0,0 +1,61 @@
1#include <algorithm>
2#include <functional>
3#include <opkele/types.h>
4#include <opkele/exception.h>
5#include <mimetic/mimetic.h>
6
7namespace opkele {
8 using namespace std;
9
10 template<class __a1,class __a2,class __r>
11 struct bitwise_xor : public binary_function<__a1,__a2,__r> {
12 __r operator() (const __a1& a1,const __a2& a2) const {
13 return a1^a2;
14 }
15 };
16
17 void secret_t::enxor_to_base64(const unsigned char *key_sha1,string& rv) const {
18 if(size()!=20)
19 throw bad_input(OPKELE_CP_ "wrong secret size");
20 vector<unsigned char> tmp;
21 transform(
22 begin(), end(),
23 key_sha1,
24 back_insert_iterator<vector<unsigned char> >(tmp),
25 bitwise_xor<unsigned char,unsigned char,unsigned char>() );
26 mimetic::Base64::Encoder b(0);
27 mimetic::encode(
28 tmp.begin(),tmp.end(), b,
29 back_insert_iterator<string>(rv) );
30 }
31
32 void secret_t::enxor_from_base64(const unsigned char *key_sha1,const string& b64) {
33 mimetic::Base64::Decoder b;
34 clear();
35 mimetic::decode(
36 b64.begin(),b64.end(), b,
37 back_insert_iterator<secret_t>(*this) );
38 transform(
39 begin(), end(),
40 key_sha1,
41 begin(),
42 bitwise_xor<unsigned char,unsigned char,unsigned char>() );
43 }
44
45 void secret_t::to_base64(string& rv) const {
46 if(size()!=20)
47 throw bad_input(OPKELE_CP_ "wrong secret size");
48 mimetic::Base64::Encoder b(0);
49 mimetic::encode(
50 begin(),end(), b,
51 back_insert_iterator<string>(rv) );
52 }
53
54 void secret_t::from_base64(const string& b64) {
55 mimetic::Base64::Decoder b;
56 mimetic::decode(
57 b64.begin(),b64.end(), b,
58 back_insert_iterator<secret_t>(*this) );
59 }
60
61}