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