Digests

QNX SDP8.0QNX OS System Security GuideAPIConfiguration

The qcrypto library API includes cryptographic digest (hashing) functions.

See the library reference for detailed information on the following functions:

Digest examples

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <qcrypto/qcrypto.h>

int main(int argc, char* argv[])
{
    const char *sha256 = "sha256";
    int ret;
    qcrypto_ctx_t *ctx = NULL;
    size_t dsize;
    const char *input_buffer = "hello world";
    size_t input_size = strlen(input_buffer);
    uint8_t *output_buffer = NULL;

    /* Initializing the qcrypto library */
    ret = qcrypto_init(QCRYPTO_INIT_LAZY, NULL);
    if (ret != QCRYPTO_R_EOK) {
        fprintf(stderr, "qcrypto_init() failed (%d:%s)\n", ret, qcrypto_strerror(ret));
        goto done;
    }

    /* Checking if a digest is supported */
    ret = qcrypto_digest_supported(sha256, NULL, 0);
    if (ret != QCRYPTO_R_EOK) {
        fprintf(stderr, "qcrypto_digest_supported() failed (%d:%s)\n", ret, qcrypto_strerror(ret));
        goto done;
    }

    /* Requesting the digest */
    ret = qcrypto_digest_request(sha256, NULL, 0, &ctx);
    if (ret != QCRYPTO_R_EOK) {
        fprintf(stderr, "qcrypto_digest_request() failed (%d:%s)\n", ret, qcrypto_strerror(ret));
        goto done;
    }

    /* Querying the digest size */
    dsize = qcrypto_digestsize(ctx);
    printf("digest size =  %ld\n", dsize);

    output_buffer = malloc(dsize);
    if (output_buffer == NULL) {
        fprintf(stderr,"output size not defined (%d:%s)\n", ret, qcrypto_strerror(ret));
        goto done;
    }

     /* Initializing, updating, and finalizing the digest */
    ret = qcrypto_digest(ctx, (const uint8_t*)input_buffer, input_size, output_buffer, &dsize);
    if (ret != QCRYPTO_R_EOK) {
        fprintf(stderr, "qcrypto_digest() failed (%d:%s)\n", ret, qcrypto_strerror(ret));
        goto done;
    }

    printf("sha256(\"hello world\") = ");
    for (int i=0; i < dsize; i++) {
        printf("%02x", output_buffer[i]);
    }
    printf("\n");

done:
    /* free the allocated memory */
    free(output_buffer);

    /* Releasing the context */
    qcrypto_release_ctx(ctx);

    /* Uninitializing the qcrypto library */
    qcrypto_uninit();

    return ret;
}
Page updated: