JongAm Park wrote:
Hello.

Thank you for mentioning the book.
However, I could figure out by staring at its MAN page long time and looking up some sample codes in source code distribution of the OpenSSL.

may be of use...
from the mailing list and the web....

note that with a little modification you can have both function utilize the 
passed in value

$ cat sha256_EVP_example.c
/*
 * Compilation
 * $ gcc -lssl sha256_EVP_example.c
 *
 * $ ./a.out sha256
 *
 * Multiple String Digest is: 
e31318725541062f35791d14a9c63c30faed0896609527c108b511b65d6e1fdd
 * Single String Digest is:   
e31318725541062f35791d14a9c63c30faed0896609527c108b511b65d6e1fdd
 *
 */

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>

static int encode_buffer_append_test( char **argv);
static int encode_buffer_test();

main(int argc, char *argv[])
{
    /*
    * MODIFICATIONS REQUIRED FOR PRODUCTION USE
    * CHECK RETURN CODES ETC
    */

    /* buffer append encode example */
    (void)encode_buffer_append_test(argv);

    /* buffer encode example */
    (void)encode_buffer_test();
}


static int encode_buffer_append_test(char **argv)
{
    EVP_MD_CTX mdctx;
    const EVP_MD *md;
    char mess1[] = "Test Message";
    char mess2[] = "Hello World";
    unsigned char md_value[EVP_MAX_MD_SIZE];
    int md_len, i;


    OpenSSL_add_all_digests();
    if(!argv[1])
    {
        printf("Usage: mdtest digestname\n");
        exit(1);
    }

    md = EVP_get_digestbyname(argv[1]);
    if(!md)
    {
        printf("Unknown message digest %s\n", argv[1]);
        exit(1);
    }

    EVP_MD_CTX_init(&mdctx);
    EVP_DigestInit_ex(&mdctx, md, NULL);
    EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
    EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
    EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
    EVP_MD_CTX_cleanup(&mdctx);

    printf("Multiple String Digest is: ");
    for(i = 0; i < md_len; i++)
    {
        printf("%02x", md_value[i]);
    }
    printf("\n");

    return 0;
}

static int encode_buffer_test()
{
    int hashsz = 0;
    int i = 0;
    unsigned char digest[EVP_MAX_MD_SIZE] =
    {0xf5, 0x53, 0xcd, 0xb8, 0xcf, 0x1, 0xee, 0x17, 0x9b, 0x93, 0xc9, 0x68, 
0xc0, 0xea, 0x40, 0x91,
        0x6, 0xec, 0x8e, 0x11, 0x96, 0xc8, 0x5d, 0x1c, 0xaf, 0x64, 0x22, 0xe6, 
0x50, 0x4f, 0x47, 0x57};
    unsigned char str[] = "Test MessageHello World";
    unsigned char md[EVP_MAX_MD_SIZE];


/* int EVP_Digest (const void *data, size_t count, unsigned char *md, unsigned int *size, const EVP_MD *type, ENGINE *impl)
     * Parameters:
     * data    the data to update the context with
     * dsize   length of data
     * hash    output data of at least EVP_MD_size() length.
     * hsize   output length of hash.
     * md  message digest to use
     * engine  engine to use, NULL for default engine.
     *
     * Returns:
     * 1 on success.
     * */

    ERR_clear_error();
    if (!EVP_Digest(str, sizeof(str) - 1, md, &hashsz, EVP_sha256(), NULL))
    {
        printf("failed\n");

        return 0;
    }

    if (memcmp(md, digest, sizeof(md)))
    {
        printf("Single String Digest is:   ");
        for(i = 0; i < hashsz; i++)
        {
            printf("%02x", md[i]);
        }
        printf("\n");

        return 0;
    }
    else
    {
        printf("memcmp failed/digest doesn't match expected\n");

        return 1;
    }
}
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to