Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8161117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:25:47+00:00 2026-06-06T18:25:47+00:00

Has anyone written any tutorials or have any documentation on how to use GnuPGME

  • 0

Has anyone written any tutorials or have any documentation on how to use GnuPGME so I would be able to write a function such as gpgSign(std::string fileToBeSigned, std::string outPutFileName) in C++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:25:48+00:00Added an answer on June 6, 2026 at 6:25 pm

    Below is a C example with verbose comments that does what you are looking for – it is not the most direct approach, but should illustrate the how to accomplish signing a file. It does not handle selection of signers, but the GPGME docs should help you there.

    You can save the file and make edits and test directly from the command line. To compile, just save as “gpgsign.c”, and execute gcc gpgsign.c -lgpgme -o gpgsign (NOTE: you must have libgpgme installed). Then you can execute the using 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

has anyone written a generic LaunchForm function? For all the menu items I have
Has anyone come across a script / cl app written in any language that
I am curious if anyone has written any code to reflect into a class
Has anyone written any code where the application in its lifetime learn and improve
Has anyone written a macro that will remove and sort your usings in an
has anyone an idea why I have the following problem with passing locals to
Has anyone else come across this at all? I used to be able to
Has anyone tried coding in managed C++? I have a few questions : How
This may be Compass 101, but has anyone written a mixin which sets the
Has anyone tried to store Lucene index in JBoss Cache? Are there any good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.