summaryrefslogtreecommitdiffabout
path: root/src/eyetil.cc
Unidiff
Diffstat (limited to 'src/eyetil.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--src/eyetil.cc17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/eyetil.cc b/src/eyetil.cc
index fe816a6..7669cb6 100644
--- a/src/eyetil.cc
+++ b/src/eyetil.cc
@@ -1,50 +1,67 @@
1#include <stdlib.h> 1#include <stdlib.h>
2#include <sys/stat.h> 2#include <sys/stat.h>
3#include <syslog.h> 3#include <syslog.h>
4#include <iostream> 4#include <iostream>
5#include <cassert> 5#include <cassert>
6#include <stdexcept> 6#include <stdexcept>
7#include <algorithm> 7#include <algorithm>
8#include <numeric> 8#include <numeric>
9#include <openssl/md5.h> 9#include <openssl/md5.h>
10#include "eyetil.h" 10#include "eyetil.h"
11 11
12#include "config.h"
13#ifdef HAVE_LIBUUID
14# include <uuid/uuid.h>
15#endif
16
12binary_t& binary_t::from_hex(const std::string& h) { 17binary_t& binary_t::from_hex(const std::string& h) {
13 std::string::size_type hs = h.length(); 18 std::string::size_type hs = h.length();
14 if(hs&1) 19 if(hs&1)
15 throw std::runtime_error("odd number of characters in hexadecimal number"); 20 throw std::runtime_error("odd number of characters in hexadecimal number");
16 int rvs = hs>>1; 21 int rvs = hs>>1;
17 resize(rvs); 22 resize(rvs);
18 const unsigned char *hp = (const unsigned char*)h.data(); 23 const unsigned char *hp = (const unsigned char*)h.data();
19 iterator oi=begin(); 24 iterator oi=begin();
20 char t[3] = { 0,0,0 }; 25 char t[3] = { 0,0,0 };
21 for(int i=0;i<rvs;++i) { 26 for(int i=0;i<rvs;++i) {
22 t[0]=*(hp++); t[1]=*(hp++); 27 t[0]=*(hp++); t[1]=*(hp++);
23 *(oi++) = strtol(t,0,16); 28 *(oi++) = strtol(t,0,16);
24 } 29 }
25 return *this; 30 return *this;
26} 31}
27 32
28binary_t& binary_t::from_data(const void *d,size_t s) { 33binary_t& binary_t::from_data(const void *d,size_t s) {
29 resize(s); 34 resize(s);
30 std::copy((const unsigned char*)d,(const unsigned char *)d+s, 35 std::copy((const unsigned char*)d,(const unsigned char *)d+s,
31 begin() ); 36 begin() );
32 return *this; 37 return *this;
33} 38}
34 39
40binary_t& binary_t::make_nonce() {
41#ifdef HAVE_LIBUUID
42 uuid_t uuid;
43 uuid_generate(uuid);
44 from_data((unsigned char*)uuid,sizeof(uuid));
45#else
46 resize(16);
47 std::generate_n(begin(),16,rand);
48#endif /* HAVE_LIBUUID */
49 return *this;
50}
51
35std::string binary_t::hex() const { 52std::string binary_t::hex() const {
36 std::string rv; 53 std::string rv;
37 rv.reserve((size()<<1)+1); 54 rv.reserve((size()<<1)+1);
38 char t[3] = {0,0,0}; 55 char t[3] = {0,0,0};
39 for(const_iterator i=begin(),ie=end();i!=ie;++i) { 56 for(const_iterator i=begin(),ie=end();i!=ie;++i) {
40 int rc = snprintf(t,sizeof(t),"%02x",*i); 57 int rc = snprintf(t,sizeof(t),"%02x",*i);
41 assert(rc<sizeof(t)); 58 assert(rc<sizeof(t));
42 rv += t; 59 rv += t;
43 } 60 }
44 return rv; 61 return rv;
45} 62}
46 63
47binary_t binary_t::md5() const { 64binary_t binary_t::md5() const {
48 binary_t rv(MD5_DIGEST_LENGTH); 65 binary_t rv(MD5_DIGEST_LENGTH);
49 if(!MD5( 66 if(!MD5(
50 (const unsigned char*)&(front()),size(), 67 (const unsigned char*)&(front()),size(),