你应该熟悉python的回调。
首先定义回调函数:
using namespace boost::asio;
// this function is called to obtain password info about an encrypted key
std::string my_password_callback(
std::size_t max_length, // the maximum length for a password
ssl::context::password_purpose purpose) // for_reading or for_writing
{
std::string password;
// security warning: !! DO NOT hard-code the password here !!
// read it from a SECURE location on your system
return password;
}
然后用set_password_callback()
设置回调:如果你想使用一个类方法,如回调
// set the callback before you load the protected key
ctx.set_password_callback(my_password_callback);
// ...
// this will call my_password_callback if a password is required
ctx.use_private_key_file("key.pem",ssl::context::pem);
,
class server {
std::string password_callback(); //NOTE: no parameters
// ...
};
你可以使用boost::bind()
来设置回调:
#include <boost/bind.hpp>
void server::startup() {
ctx_.set_password_callback(
boost::bind(&server::password_callback,this));
// ...
}
在这两种情况下,boost::system::system_error
异常(基于std::exception
)将被抛出 如果密钥无法解密,可能是因为密码错误或文件不能被发现。