summaryrefslogtreecommitdiffabout
path: root/lib/secret.cc
authorMichael Krelin <hacker@klever.net>2005-07-19 13:08:32 (UTC)
committer Michael Krelin <hacker@klever.net>2005-07-19 13:08:32 (UTC)
commit4c82851dd5d5644a89d4f269079bf901f763ee33 (patch) (side-by-side diff)
treeb64c8b3c9a1be88e2a9c3f762272e0b4509ba7d9 /lib/secret.cc
parent907343b0c973eb295bec8795902a6d49744e9174 (diff)
downloadlibopkele-4c82851dd5d5644a89d4f269079bf901f763ee33.zip
libopkele-4c82851dd5d5644a89d4f269079bf901f763ee33.tar.gz
libopkele-4c82851dd5d5644a89d4f269079bf901f763ee33.tar.bz2
initial commit of libopkele - OpenID support library
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 @@
+#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) );
+ }
+
+}