summaryrefslogtreecommitdiffabout
path: root/lib/secret.cc
blob: ae8a3c5518078487c67655a2cfc2fa4fdd87ef5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <algorithm>
#include <functional>
#include <opkele/types.h>
#include <opkele/exception.h>
#include <mimetic/mimetic.h>

namespace opkele {
    using namespace std;

    template<class __a1,class __a2,class __r>
	struct bitwise_xor : public binary_function<__a1,__a2,__r> {
	    __r operator() (const __a1& a1,const __a2& a2) const {
		return a1^a2;
	    }
	};

    void secret_t::enxor_to_base64(const unsigned char *key_sha1,string& rv) const {
	if(size()!=20)
	    throw bad_input(OPKELE_CP_ "wrong secret size");
	vector<unsigned char> tmp;
	transform(
		begin(), end(),
		key_sha1,
		back_insert_iterator<vector<unsigned char> >(tmp),
		bitwise_xor<unsigned char,unsigned char,unsigned char>() );
	mimetic::Base64::Encoder b(0);
	mimetic::encode(
		tmp.begin(),tmp.end(), b,
		back_insert_iterator<string>(rv) );
    }

    void secret_t::enxor_from_base64(const unsigned char *key_sha1,const string& b64) {
	mimetic::Base64::Decoder b;
	clear();
	mimetic::decode(
		b64.begin(),b64.end(), b,
		back_insert_iterator<secret_t>(*this) );
	transform(
		begin(), end(),
		key_sha1,
		begin(),
		bitwise_xor<unsigned char,unsigned char,unsigned char>() );
    }

    void secret_t::to_base64(string& rv) const {
	if(size()!=20)
	    throw bad_input(OPKELE_CP_ "wrong secret size");
	mimetic::Base64::Encoder b(0);
	mimetic::encode(
		begin(),end(), b,
		back_insert_iterator<string>(rv) );
    }

    void secret_t::from_base64(const string& b64) {
	mimetic::Base64::Decoder b;
	mimetic::decode(
		b64.begin(),b64.end(), b,
		back_insert_iterator<secret_t>(*this) );
    }

}