有没有人写过任何教程或者有关于如何使用GnuPGME的文档,所以我可以在C++中编写一个函数,如gpgSign(std::string fileToBeSigned, std::string outPutFileName)
?GnuPGME:GPG Signature C++
回答
下面是一个具有详细注释的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;
}
这个答案很可能来得太迟,但如果我是你,我宁愿使用Keyczar这是一个高层次的加密工具包,一个简单的API。 Java,C++,Python绑定可用。
GPGME对于需要加密功能而没有太多调整的人来说仍然是相当低级别的IMO。当然,加密专家需要这种复杂性。
就我个人而言,我试图避免需要我设置此引擎的库以及100行样板代码的上下文,然后才能做一些基本的工作......但我并不擅长任何事情。
有人没有给出任何理由就低估了我的答案。我想知道为什么:Keyczar符合最初要求的要求。我的另一点仍然是:我们大多数人需要简单的高级加密API。我们都非常感谢那些创建低级API的专家的工作,我非常感谢kylehuff解释了上述所有低级别的步骤。但是对于我们其他人来说,应该是一个函数'gpgSign(const std :: string&fileToBeSigned,const std :: string&outPutFileName)'。 – 2013-11-26 11:06:55
我不明白为什么人们低调回答这个问题。我真的很欣赏解释我写的东西有什么问题的评论。看到我的评论上面的一些澄清。谢谢。 – 2014-04-20 17:59:42
如果我不得不猜测,我会说这是因为你的回答没有提到问题的问题。所问的问题是关于GnuPG GPGME的具体问题,而不是对替代品的要求。这并不是说你的答案(和相关问题)没有价值;只是他们不适用于所问的问题。 – kylehuff 2014-05-08 21:58:39
- 1. Monodroid Signature Capture
- 2. Win32_DiskDrive Signature属性
- 3. Invalid Signature - XCode 4.4,Mac App Store
- 4. 错误? in codesign --remove-signature feature
- 5. 如何在C#中为Signature元素添加前缀
- 6. Facebook实时更新:验证C#中的X-Hub-Signature SHA1签名
- 7. Signature Invalid/Configured Certificate Mismatch for SSO with SFDC
- 8. Exchange Online Signature for new and replay message
- 9. SAML:为什么Signature中的证书?
- 10. Paypal API USER,PWD,SIGNATURE字段长度
- 11. Angular 2:Signature Pad需要单独实例
- 12. Chrome Web应用程序中的Topaz Signature Pad
- 13. 如何在Kong API Gateway的API中添加HMAC Signature Authentication?
- 14. 在Android上为Signature Capture编写自己的程序
- 15. 从模板创建信封时,Signature选项卡不可见
- 16. java.lang.VerifyError:(class:Main,method:main signature:([Ljava/lang/String;)V)堆栈大小太大
- 17. Xamarin新手,需要添加Signature Pad到ToolBox VS 2015
- 18. 带有列表视图的Android Material Signature元素
- 19. Sagepay Reporting API错误无法验证<signature>的值
- 20. 如何使用CoSign Signature Soap API添加图形图像签名
- 21. 如何通过JavaScript访问Topaz Signature Pad ActiveX对象?
- 22. Signature/Functor模式中的非抽象类型冗余
- 23. 当在AndroidStudio中生成已签名的apk时,签名版本-V1(Jar Signature)和V2(Full APK Signature)之间的区别?
- 24. 没有name ='getMessage'的静态方法signature ='()Ljava/lang/String;'在类Ljava.lang.Object中;
- 25. 当使用谷歌地图时,Ionic 3 - Signature Pad未在android kit-kat上打开
- 26. EL自定义函数,如何在<function-signature>中声明HashSet <Integer>?
- 27. 问题在C#中调用C DLL
- 28. XML到C#类
- 29. 如何解析电子邮件文本,如<salutation><body><signature><reply text>等?
- 30. 使用AWS Key和Signature属性在Amazon S3的浏览器中打开.pdf文件?
库本身是用C语言编写的,这意味着你可以在不费力的情况下在C++中使用它。你应该阅读关于使用C库与C++。 – v01d 2012-03-07 09:29:48
我知道我可以使用C库,但我完全失去了在哪里寻找甚至接近编写函数。 – allejo 2012-03-18 20:10:48