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

  • SEARCH
  • Home
  • 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 6692329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:51:20+00:00 2026-05-26T05:51:20+00:00

I want to create a signature for a file using OpenSSL. I believe that

  • 0

I want to create a signature for a file using OpenSSL.
I believe that my result should match the testfile.sig generated by openssl this way:

$ openssl dgst -sha1 -binary < testfile > testfile.sha1
$ openssl rsautl -sign -in testfile.sha1 -inkey rsa_private.pem -keyform PEM -out testfile.sig

$ openssl rsautl -verify -inkey rsa_public.pem -pubin -in testfile.sig -hexdump
Loading 'screen' into random state - done
0000 - 9f 23 88 15 2b af d6 a6-9f 4f 1f 7e ee b0 90 dd   .#..+....O.~....
0010 - 69 99 2b 3d                                       i.+=

So, I used the code by caf that was posted here:
How to read key file for use with HMAC_Init_ex()
and just added a small piece to save the data after EVP_SignFinal() to a file, so then I
could just verify it with openssl and the public key:

FILE * fp = fopen("out.sig", "wb");
if (fp) {
    printf("Writing the signature in a file. # of bytes: %u.\n", sig_len);
    fwrite(sig, 1, sig_len, fp);
    fclose(fp);
}

And to my suprise, I got this:

$ openssl rsautl -verify -inkey rsa_public.pem -pubin -in out.sig -hexdump
Loading 'screen' into random state - done
0000 - 30 21 30 09 06 05 2b 0e-03 02 1a 05 00 04 14 9f   0!0...+.........
0010 - 23 88 15 2b af d6 a6 9f-4f 1f 7e ee b0 90 dd 69   #..+....O.~....i
0020 - 99 2b 3d                                          .+=

Now, if you observe the output of the verification of out.sig, you’ll see that the last 20
bytes match the data output of the verification of testfile.sig. And those 20 bytes are
indeed the SHA-1 hash of my file. But what are the first 15 bytes at the beginning?
Isn’t EVP_SignInit() / EVP_SignUpdate() / EVP_SignFinal() supposed to put only the hash in
the message, or am I missing something obvious?

If you need more info, please let me know.
Thanks

Full function that was uset to sign the file:

int do_evp_sign(FILE * rsa_pkey_file, FILE * in_file)
{
    OpenSSL_add_all_digests();
    ERR_load_crypto_strings();

    EVP_PKEY * pkey = PEM_read_PrivateKey(rsa_pkey_file, NULL, NULL, NULL);
    if (pkey == NULL) {
        ERR_print_errors_fp(stderr);
        return 1;
    }

    fseek(in_file, 0, SEEK_END);
    const size_t lSize = ftell(in_file);
    rewind(in_file);

    EVP_MD_CTX md_ctx;
    EVP_SignInit(&md_ctx, EVP_sha1());

    size_t len;
    unsigned char buffer[4096];

    size_t bytesLeft = lSize;
    while (bytesLeft > 0) {
        const size_t count = (bytesLeft > sizeof(buffer) ? sizeof(buffer) : bytesLeft);
        len = fread(buffer, 1, count, in_file);
        if (len != count) {
            fprintf(stderr, "Read error! len (%u) != count (%u).\n", len, count);
            EVP_PKEY_free(pkey);
            return 1;
        }
        if (!EVP_SignUpdate(&md_ctx, buffer, len))
        {
            ERR_print_errors_fp(stderr);
            EVP_PKEY_free(pkey);
            return 1;
        }
        bytesLeft -= len;
    }

    unsigned int sig_len;
    unsigned char * sig = (unsigned char *)malloc(EVP_PKEY_size(pkey));

    if (!sig) {
        fprintf(stderr, "Couldn't allocate %u bytes of memory.\n", EVP_PKEY_size(pkey));
        EVP_PKEY_free(pkey);
        return 1;
    }

    if (!EVP_SignFinal(&md_ctx, sig, &sig_len, pkey))
    {
        ERR_print_errors_fp(stderr);
        EVP_PKEY_free(pkey);
        free(sig);
        return 1;
    }

    FILE * fTemp = fopen("out.sig", "wb");
    if (fTemp) {
        printf("Writing the signature to a file. Number of bytes: %u.\n", sig_len);
        fwrite(sig, 1, sig_len, fTemp);
        fclose(fTemp);
    }

    printf("Signature: \n");
    for (unsigned int i = 0; i != sig_len; ++i)
    {
        printf("%02x", sig[i]);
        if (i % 16 == 15)
            printf("\n");
    }
    printf("\n");

    EVP_PKEY_free(pkey);
    free(sig);

    return 0;
}
  • 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-05-26T05:51:20+00:00Added an answer on May 26, 2026 at 5:51 am

    What you have there is an ASN.1 encoding of the hash value.

    0000 - 30 21 30 09 06 05 2b 0e-03 02 1a 05 00 04 14 9f   0!0...+.........
    0010 - 23 88 15 2b af d6 a6 9f-4f 1f 7e ee b0 90 dd 69   #..+....O.~....i
    0020 - 99 2b 3d                                          .+=
    
    30 - SEQUENCE
    21 - length (33 bytes)
     30 - SEQUENCE
     09 - length (9 bytes)
      06 - OID
      05 - length (5 bytes)
       2b 0e 03 02 1a = 1 2 14 3 2 26 = SHA-1
      05 - NULL
      00 - length (0 bytes)
     04 - Octet String
     14 - length (20 bytes)
      9f ... 3d - contents (the digest)
    

    How exactly did you generate the digest (in code)? It looks like whatever you used converted it to ASN.1 instead of leaving it “raw”, and that’s what you signed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a helper method that I can imagine has a signature
I want to create a file in the current directory (where the executable is
I want to create a stored procedure (on SQL Server 2005) that fetches a
I want to create conditional comments in XSLT. But when I use this: <!--
I created a ActiveX control using ATL, already package it with signature. I want
I want to create a self-signed IPA using XCode for an iOS app I
I want to create a custom button in ActionScript. This is my code: import
I want to create a function that takes in a required argument x, and
I'm trying to create a method signature that takes multiple properties of various type
I want to create an app for digital signature, and the standard seems to

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.