有关 gpgme_passphrase_cb_t 的帮助

发布于 2024-10-10 01:33:59 字数 163 浏览 2 评论 0原文

我正在使用 GPGME...我需要一个关于使用 gpgme_passphrase_cb_tgpgme_set_passphrase_cb 函数的示例,因为我不明白如何创建 gpgme_passphrase_cb_t代码>.

I'm working with GPGME...I need an example on use of gpgme_passphrase_cb_t and gpgme_set_passphrase_cb function because I don't understand how to create a gpgme_passphrase_cb_t.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌酱 2024-10-17 01:33:59

这是来自 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 以指示用户中止。参数 useridHintpreviousWasBad 只是附加信息。

这是通用回调:

// 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;
}

给定 PassphraseProvider 接口的实现 pp,您可以将所有内容绑定在一起,如下所示:

gpgme_set_passphrase_cb( ctx, &passphrase_callback, pp );

This is the code from gpgme++ that wraps the callback-based interface into a C++ interface:

The interface:

class PassphraseProvider {
public:
  virtual ~PassphraseProvider() {}

  virtual char * getPassphrase( const char * useridHint,
                                const char * description,
                                bool previousWasBad,
                                bool & canceled ) = 0;
};

The function is supposed to display description as a prompt, and return the passphrase entered (the buffer must be malloc()ed). It may also set canceled to true to indicate that the user aborted. The parameters useridHint and previousWasBad are just additional information.

And this this the generic callback:

// 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;
}

Given an implementation pp of the PassphraseProvider interface, you'd tie everything together like this:

gpgme_set_passphrase_cb( ctx, &passphrase_callback, pp );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文