<commit_before>#include "ndk_hash.h"

// openssl hashes

#define     NDK_OPENSSL_HASH(type,ctxt_type,upper)          \
    u_char              md [ctxt_type ## _DIGEST_LENGTH];        \
    ngx_uint_t          i;                                  \
    ctxt_type ##_CTX    c;                                  \
                                                            \
    type ## _Init (&c);                                     \
    type ## _Update (&c, data, len);                        \
    type ## _Final (md, &c);                                \
                                                            \
    for (i=0; i<ctxt_type ## _DIGEST_LENGTH; i++) {              \
        p += sprintf (p, (upper ? "%02X" : "%02x"), md[i]); \
    }


// TODO : check to see if can do consistent printing using "%08X" or in single go

#ifdef NDK_MD5

void
ndk_md5_hash (char *p, char *data, size_t len)
{
    NDK_OPENSSL_HASH (MD5, MD5, 1);
}

void
ndk_md5_lower_hash (char *p, char *data, size_t len)
{
    NDK_OPENSSL_HASH (MD5, MD5, 0);
}

#endif
#ifdef NDK_SHA1

void
ndk_sha1_hash (char *p, char *data, size_t len)
{
    NDK_OPENSSL_HASH (SHA1, SHA, 1);
}

void
ndk_sha1_lower_hash (char *p, char *data, size_t len)
{<commit_msg>fixed a memory invalid write issue in the NDK_OPENSSL_HASH macro. thanks to the no-pool-nginx patch and valgrind!<commit_after>#include "ndk_hash.h"

// openssl hashes

#define     NDK_OPENSSL_HASH(type,ctxt_type,upper)          \
    u_char              md [ctxt_type ## _DIGEST_LENGTH];        \
    ngx_uint_t          i;                                  \
    ctxt_type ##_CTX    c;                                  \
                                                            \
    type ## _Init (&c);                                     \
    type ## _Update (&c, data, len);                        \
    type ## _Final (md, &c);                                \
                                                            \
    ngx_hex_dump((u_char *) p, (u_char *) md, ctxt_type ## _DIGEST_LENGTH);        \
    if (upper) { \
        for (i = 0; i < ctxt_type ## _DIGEST_LENGTH; i++) { \
            *p = ngx_toupper(md[i]); \
        } \
    }

// TODO : check to see if can do consistent printing using "%08X" or in single go

#ifdef NDK_MD5

void
ndk_md5_hash (char *p, char *data, size_t len)
{
    NDK_OPENSSL_HASH (MD5, MD5, 1);
}

void
ndk_md5_lower_hash (char *p, char *data, size_t len)
{
    NDK_OPENSSL_HASH (MD5, MD5, 0);
}

#endif
#ifdef NDK_SHA1

void
ndk_sha1_hash (char *p, char *data, size_t len)
{
    NDK_OPENSSL_HASH (SHA1, SHA, 1);
}

void
ndk_sha1_lower_hash (char *p, char *data, size_t len)
{
{'fraction_non_alphanumeric': 0.11031894934333959, 'fraction_numerical': 0.01425891181988743, 'mean_word_length': 1.332458442694663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7436604', 'n_tokens_mistral': 940, 'n_tokens_neox': 905, 'n_words': 227}