author | Michael Krelin <hacker@klever.net> | 2007-06-18 22:02:02 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2007-06-18 22:02:02 (UTC) |
commit | 9af3fae2d53a34003af405b68923061c01584bc6 (patch) (unidiff) | |
tree | dd63e5bc3515c47ab074c564c51879b7c9652ab2 /lib/util.cc | |
parent | 3b404dd029a2aba05efc2edadcc7f67c59746cf7 (diff) | |
download | libopkele-9af3fae2d53a34003af405b68923061c01584bc6.zip libopkele-9af3fae2d53a34003af405b68923061c01584bc6.tar.gz libopkele-9af3fae2d53a34003af405b68923061c01584bc6.tar.bz2 |
reworked zero-padding machinery and added one more instance of zero-padding
-rw-r--r-- | lib/util.cc | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/util.cc b/lib/util.cc index d9abca7..94f6f53 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -57,67 +57,71 @@ namespace opkele { | |||
57 | unsigned char tmp[512]; | 57 | unsigned char tmp[512]; |
58 | size_t rb = 0; | 58 | size_t rb = 0; |
59 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 59 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
60 | rv.insert(rv.end(),tmp,&tmp[rb]); | 60 | rv.insert(rv.end(),tmp,&tmp[rb]); |
61 | BIO_free_all(b64); | 61 | BIO_free_all(b64); |
62 | }catch(...) { | 62 | }catch(...) { |
63 | if(b64) BIO_free_all(b64); | 63 | if(b64) BIO_free_all(b64); |
64 | throw; | 64 | throw; |
65 | } | 65 | } |
66 | } | 66 | } |
67 | 67 | ||
68 | /* | 68 | /* |
69 | * big numerics | 69 | * big numerics |
70 | */ | 70 | */ |
71 | 71 | ||
72 | BIGNUM *base64_to_bignum(const string& b64) { | 72 | BIGNUM *base64_to_bignum(const string& b64) { |
73 | vector<unsigned char> bin; | 73 | vector<unsigned char> bin; |
74 | decode_base64(b64,bin); | 74 | decode_base64(b64,bin); |
75 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 75 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
76 | if(!rv) | 76 | if(!rv) |
77 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 77 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
78 | return rv; | 78 | return rv; |
79 | } | 79 | } |
80 | 80 | ||
81 | BIGNUM *dec_to_bignum(const string& dec) { | 81 | BIGNUM *dec_to_bignum(const string& dec) { |
82 | BIGNUM *rv = 0; | 82 | BIGNUM *rv = 0; |
83 | if(!BN_dec2bn(&rv,dec.c_str())) | 83 | if(!BN_dec2bn(&rv,dec.c_str())) |
84 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 84 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
85 | return rv; | 85 | return rv; |
86 | } | 86 | } |
87 | 87 | ||
88 | string bignum_to_base64(const BIGNUM *bn) { | 88 | string bignum_to_base64(const BIGNUM *bn) { |
89 | vector<unsigned char> bin(BN_num_bytes(bn)); | 89 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
90 | int l = BN_bn2bin(bn,&(bin.front())); | 90 | unsigned char *binptr = &(bin.front())+1; |
91 | return encode_base64(&(bin.front()),l); | 91 | int l = BN_bn2bin(bn,binptr); |
92 | if(l && (*binptr)&0x80){ | ||
93 | (*(--binptr)) = 0; ++l; | ||
94 | } | ||
95 | return encode_base64(binptr,l); | ||
92 | } | 96 | } |
93 | 97 | ||
94 | /* | 98 | /* |
95 | * w3c times | 99 | * w3c times |
96 | */ | 100 | */ |
97 | 101 | ||
98 | string time_to_w3c(time_t t) { | 102 | string time_to_w3c(time_t t) { |
99 | struct tm tm_t; | 103 | struct tm tm_t; |
100 | if(!gmtime_r(&t,&tm_t)) | 104 | if(!gmtime_r(&t,&tm_t)) |
101 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 105 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
102 | char rv[25]; | 106 | char rv[25]; |
103 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 107 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
104 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 108 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
105 | return rv; | 109 | return rv; |
106 | } | 110 | } |
107 | 111 | ||
108 | time_t w3c_to_time(const string& w) { | 112 | time_t w3c_to_time(const string& w) { |
109 | struct tm tm_t; | 113 | struct tm tm_t; |
110 | memset(&tm_t,0,sizeof(tm_t)); | 114 | memset(&tm_t,0,sizeof(tm_t)); |
111 | if( | 115 | if( |
112 | sscanf( | 116 | sscanf( |
113 | w.c_str(), | 117 | w.c_str(), |
114 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 118 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
115 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 119 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
116 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 120 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
117 | ) != 6 ) | 121 | ) != 6 ) |
118 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 122 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
119 | tm_t.tm_mon--; | 123 | tm_t.tm_mon--; |
120 | tm_t.tm_year-=1900; | 124 | tm_t.tm_year-=1900; |
121 | time_t rv = mktime(&tm_t); | 125 | time_t rv = mktime(&tm_t); |
122 | if(rv==(time_t)-1) | 126 | if(rv==(time_t)-1) |
123 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 127 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |