GnuPGME:GPG签名C++

5

有人写过任何关于如何使用GnuPGME的教程或文档吗?这样我就能在C++中编写像gpgSign(std::string fileToBeSigned, std::string outPutFileName)这样的函数了。


该库本身是用C语言编写的,这意味着您可以在C++中轻松使用它。您只需要了解如何在C++中使用C库即可。 - v01d
我知道我可以使用C库,但我完全不知道在哪里寻找甚至接近编写函数的地方。 - allejo
2个回答

5
这个答案可能有些晚了,但如果我是你,我会选择使用Keyczar,它是一个高级加密工具包,具有简单的API。Java、C++和Python绑定都可用。
对于需要加密功能而不需要太多调整的人来说,我认为GPGME仍然相当低级。当然,加密专家需要这种复杂性。
个人而言,我尽量避免使用需要我在100行样板代码中设置这个引擎和那个上下文的库……但我不是任何方面的专家。

有人在没有给出任何理由的情况下对我的答案进行了负评。我想知道为什么:Keyczar很好地满足了最初提出的要求。我的另一个观点仍然存在:我们大多数人需要_简单_的高级加密API。我们所有人都欣赏那些创建低级API的专家的工作,我感谢kylehuff已经解释了所有低级步骤。但是对于我们其他人来说,应该有一个函数gpgSign(const std::string& fileToBeSigned, const std::string& outPutFileName) - András Aszódi
1
我不明白为什么人们一直在踩这个答案。我真的很希望有人能够评论并解释一下我写的有什么问题。请看我上面的评论,以获取一些澄清。谢谢。 - András Aszódi
如果我必须猜测的话,我会说这是因为你的回答没有回应所问的问题。所问的问题特别涉及GnuPG GPGME,而不是请求替代方案。这并不意味着你的回答(以及相关观点)没有价值;只是它们不适用于所问的问题。 - kylehuff
1
我知道我在挖掘一个坟墓,但我感到有必要至少补充一下这个答案。我更喜欢这个答案,因为使用一个经过验证的高级加密库(就像大多数高级库一样)可以避免重复发明轮子。这也意味着开发人员不需要手动重写与加密设置相关的许多相同的代码,那样可能会产生安全漏洞。 - Joep_H

5
以下是一段带有详细注释的C示例,它可以实现您要求的功能-虽然不是最简单的方法,但应该能说明如何完成文件签名。它并没有处理选择签署者,但GPGME文档应该能帮到您。
您可以保存文件,从命令行直接进行编辑和测试。要编译,只需将其保存为"gpgsign.c",并执行gcc gpgsign.c -lgpgme -o gpgsign(注意:您必须安装libgpgme)。然后您可以使用gpgsign <input file> <output file>来运行它。
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <errno.h>
#include <locale.h>

#include <gpgme.h>

#define fail_if_err(err)                                    \
    do {                                                    \
        if (err) {                                          \
            fprintf (stderr, "%s:%d: %s: %s\n",             \
                __FILE__, __LINE__, gpgme_strsource (err),  \
                gpgme_strerror (err));                      \
            exit (1);                                       \
        }                                                   \
    }                                                       \
    while (0)

void gpgSign(const char *fileToBeSigned, const char *outputFileName) {
    gpgme_ctx_t ctx;
    gpgme_error_t err;
    gpgme_data_t in, out;
    FILE *outputFile;
    int BUF_SIZE = 512;
    char buf[BUF_SIZE + 1];
    int ret;
    /* Set the GPGME signature mode
        GPGME_SIG_MODE_NORMAL : Signature with data
        GPGME_SIG_MODE_CLEAR  : Clear signed text
        GPGME_SIG_MODE_DETACH : Detached signature */
    gpgme_sig_mode_t sigMode = GPGME_SIG_MODE_CLEAR;

    /* Begin setup of GPGME */
    gpgme_check_version (NULL);
    setlocale (LC_ALL, "");
    gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
#ifndef HAVE_W32_SYSTEM
    gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
#endif
    /* End setup of GPGME */

    // Create the GPGME Context
    err = gpgme_new (&ctx);
    // Error handling
    fail_if_err (err);

    // Set the context to textmode
    gpgme_set_textmode (ctx, 1);
    // Enable ASCII armor on the context
    gpgme_set_armor (ctx, 1);

    // Create a data object pointing to the input file
    err = gpgme_data_new_from_file (&in, fileToBeSigned, 1);
    // Error handling
    fail_if_err (err);

    // Create a data object pointing to the out buffer
    err = gpgme_data_new (&out);
    // Error handling
    fail_if_err (err);

    // Sign the contents of "in" using the defined mode and place it into "out"
    err = gpgme_op_sign (ctx, in, out, sigMode);
    // Error handling
    fail_if_err (err);

    // Open the output file
    outputFile = fopen (outputFileName, "w+");

    // Rewind the "out" data object
    ret = gpgme_data_seek (out, 0, SEEK_SET);
    // Error handling
    if (ret)
        fail_if_err (gpgme_err_code_from_errno (errno));

    // Read the contents of "out" and place it into buf
    while ((ret = gpgme_data_read (out, buf, BUF_SIZE)) > 0) {
        // Write the contents of "buf" to "outputFile"
        fwrite (buf, ret, 1, outputFile);
    }

    // Error handling
    if (ret < 0)
        fail_if_err (gpgme_err_code_from_errno (errno));

    // Close "outputFile"
    fclose(outputFile);
    // Release the "in" data object
    gpgme_data_release (in);
    // Release the "out" data object
    gpgme_data_release (out);
    // Release the context
    gpgme_release (ctx);
}

int 
main (int argc, char **argv) {
    if (argc != 3) {
        printf("Usage: gpgsign <input file> <output file>\n");
        exit (1);
    }
    printf("Signing %s and placing the result into %s\n", argv[1], argv[2]);
    gpgSign(argv[1], argv[2]);
    return 0;
}

最后一个 printf 的参数应该是 argv[1]argv[2],而不是 0 和 1。 - Diti
@Diti 已修复。发现得好。 - kylehuff
你能否也添加签名验证? - Anton Andreev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接