24 #include <openssl/err.h>
29 : ostream(this), out_buf_(
BUFF_SIZE), _pkey(pkey), _verify(verify), _sign_valid(false), _return_code(0)
32 setp(&out_buf_[0], &out_buf_[
BUFF_SIZE - 1]);
33 EVP_MD_CTX_init(&_ctx);
37 if (!EVP_SignInit_ex(&_ctx, EVP_sha256(), NULL))
40 ERR_print_errors_fp(stderr);
45 if (!EVP_VerifyInit_ex(&_ctx, EVP_sha256(), NULL))
48 ERR_print_errors_fp(stderr);
55 EVP_MD_CTX_cleanup(&_ctx);
60 EVP_MD_CTX_cleanup(&_ctx);
62 EVP_MD_CTX_init(&_ctx);
66 if (!EVP_SignInit_ex(&_ctx, EVP_sha256(), NULL))
69 ERR_print_errors_fp(stderr);
74 if (!EVP_VerifyInit_ex(&_ctx, EVP_sha256(), NULL))
77 ERR_print_errors_fp(stderr);
91 std::vector<unsigned char> sign(EVP_PKEY_size(_pkey));
92 unsigned int size = EVP_PKEY_size(_pkey);
94 _return_code = EVP_SignFinal(&_ctx, &sign[0], &size, _pkey);
96 _sign = std::string((
const char*)&sign[0], size);
100 return std::pair<const int, const std::string>(_return_code, _sign);
110 _return_code = EVP_VerifyFinal(&_ctx, reinterpret_cast<const unsigned char *>(their_sign.c_str()), static_cast<unsigned int>(their_sign.size()), _pkey);
118 int ret = std::char_traits<char>::eq_int_type(this->
overflow(
119 std::char_traits<char>::eof()), std::char_traits<char>::eof()) ? -1
127 char *ibegin = &out_buf_[0];
131 setp(&out_buf_[0], &out_buf_[
BUFF_SIZE - 1]);
133 if (!std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof()))
136 *iend++ = std::char_traits<char>::to_char_type(c);
140 if ((iend - ibegin) == 0)
142 return std::char_traits<char>::not_eof(c);
148 if (!EVP_SignUpdate(&_ctx, &out_buf_[0], iend - ibegin))
151 ERR_print_errors_fp(stderr);
156 if (!EVP_VerifyUpdate(&_ctx, &out_buf_[0], iend - ibegin))
159 ERR_print_errors_fp(stderr);
163 return std::char_traits<char>::not_eof(c);