我正在使用GPGME ...我需要使用gpgme_passphrase_cb_t
和gpgme_set_passphrase_cb
函数的示例,因为我不了解如何创建gpgme_passphrase_cb_t
。
答案 0 :(得分:2)
这是gpgme ++中的代码,它将基于回调的接口包装到C ++接口中:
界面:
class PassphraseProvider {
public:
virtual ~PassphraseProvider() {}
virtual char * getPassphrase( const char * useridHint,
const char * description,
bool previousWasBad,
bool & canceled ) = 0;
};
该函数应显示description
作为提示,并返回输入的密码(缓冲区必须为malloc()
ed)。它还可以将canceled
设置为true
以指示用户已中止。参数useridHint
和previousWasBad
只是其他信息。
这就是通用回调:
// Code taken from gpgme++, license: LGPLv2+
static
gpgme_error_t passphrase_callback( void * opaque, const char * uid_hint, const char * desc,
int prev_was_bad, int fd ) {
PassphraseProvider * provider = static_cast<PassphraseProvider*>( opaque );
bool canceled = false;
gpgme_error_t err = GPG_ERR_NO_ERROR;
char * passphrase = provider ? provider->getPassphrase( uid_hint, desc, prev_was_bad, canceled ) : 0 ;
if ( canceled )
err = make_error( GPG_ERR_CANCELED );
else
if ( passphrase && *passphrase ) {
size_t passphrase_length = std::strlen( passphrase );
size_t written = 0;
do {
#ifdef HAVE_GPGME_IO_READWRITE
ssize_t now_written = gpgme_io_write( fd, passphrase + written, passphrase_length - written );
#else
ssize_t now_written = write( fd, passphrase + written, passphrase_length - written );
#endif
if ( now_written < 0 ) {
err = make_err_from_syserror();
break;
}
written += now_written;
} while ( written < passphrase_length );
}
free( passphrase );
#ifdef HAVE_GPGME_IO_READWRITE
gpgme_io_write( fd, "\n", 1 );
#else
write( fd, "\n", 1 );
#endif
return err;
}
鉴于pp
接口的实现PassphraseProvider
,您可以将所有内容绑定在一起:
gpgme_set_passphrase_cb( ctx, &passphrase_callback, pp );