#include <njs.h>
-#include <njs_md5.h>
-#include <njs_sha1.h>
-#include <njs_sha2.h>
+#include <njs_hash.h>
#include <njs_string.h>
#include <njs_buffer.h>
-typedef void (*njs_hash_init)(void *ctx);
-typedef void (*njs_hash_update)(void *ctx, const void *data, size_t size);
-typedef void (*njs_hash_final)(u_char *result, void *ctx);
+typedef void (*njs_hash_init)(njs_hash_t *ctx);
+typedef void (*njs_hash_update)(njs_hash_t *ctx, const void *data, size_t size);
+typedef void (*njs_hash_final)(u_char result[32], njs_hash_t *ctx);
typedef njs_int_t (*njs_digest_encode)(njs_vm_t *vm, njs_value_t *value,
const njs_str_t *src);
} njs_hash_alg_t;
typedef struct {
- union {
- njs_md5_t md5;
- njs_sha1_t sha1;
- njs_sha2_t sha2;
- } u;
-
+ njs_hash_t ctx;
njs_hash_alg_t *alg;
} njs_digest_t;
typedef struct {
u_char opad[64];
-
- union {
- njs_md5_t md5;
- njs_sha1_t sha1;
- njs_sha2_t sha2;
- } u;
-
+ njs_hash_t ctx;
njs_hash_alg_t *alg;
} njs_hmac_t;
{
njs_str("md5"),
16,
- (njs_hash_init) njs_md5_init,
- (njs_hash_update) njs_md5_update,
- (njs_hash_final) njs_md5_final
+ njs_md5_init,
+ njs_md5_update,
+ njs_md5_final
},
{
njs_str("sha1"),
20,
- (njs_hash_init) njs_sha1_init,
- (njs_hash_update) njs_sha1_update,
- (njs_hash_final) njs_sha1_final
+ njs_sha1_init,
+ njs_sha1_update,
+ njs_sha1_final
},
{
njs_str("sha256"),
32,
- (njs_hash_init) njs_sha2_init,
- (njs_hash_update) njs_sha2_update,
- (njs_hash_final) njs_sha2_final
+ njs_sha2_init,
+ njs_sha2_update,
+ njs_sha2_final
},
{
dgst->alg = alg;
- alg->init(&dgst->u);
+ alg->init(&dgst->ctx);
return njs_vm_external_create(vm, retval, njs_crypto_hash_proto_id,
dgst, 0);
}
if (!hmac) {
- dgst->alg->update(&dgst->u, data.start, data.length);
+ dgst->alg->update(&dgst->ctx, data.start, data.length);
} else {
- ctx->alg->update(&ctx->u, data.start, data.length);
+ ctx->alg->update(&ctx->ctx, data.start, data.length);
}
njs_value_assign(retval, this);
if (!hmac) {
alg = dgst->alg;
- alg->final(digest, &dgst->u);
+ alg->final(digest, &dgst->ctx);
dgst->alg = NULL;
} else {
alg = ctx->alg;
- alg->final(hash1, &ctx->u);
+ alg->final(hash1, &ctx->ctx);
- alg->init(&ctx->u);
- alg->update(&ctx->u, ctx->opad, 64);
- alg->update(&ctx->u, hash1, alg->size);
- alg->final(digest, &ctx->u);
+ alg->init(&ctx->ctx);
+ alg->update(&ctx->ctx, ctx->opad, 64);
+ alg->update(&ctx->ctx, hash1, alg->size);
+ alg->final(digest, &ctx->ctx);
ctx->alg = NULL;
}
ctx->alg = alg;
if (key.length > sizeof(key_buf)) {
- alg->init(&ctx->u);
- alg->update(&ctx->u, key.start, key.length);
- alg->final(digest, &ctx->u);
+ alg->init(&ctx->ctx);
+ alg->update(&ctx->ctx, key.start, key.length);
+ alg->final(digest, &ctx->ctx);
memcpy(key_buf, digest, alg->size);
njs_explicit_memzero(key_buf + alg->size, sizeof(key_buf) - alg->size);
key_buf[i] ^= 0x36;
}
- alg->init(&ctx->u);
- alg->update(&ctx->u, key_buf, 64);
+ alg->init(&ctx->ctx);
+ alg->update(&ctx->ctx, key_buf, 64);
return njs_vm_external_create(vm, retval, njs_crypto_hmac_proto_id,
ctx, 0);
--- /dev/null
+
+/*
+ * Copyright (C) Dmitry Volyntsev
+ * Copyright (C) NGINX, Inc.
+ */
+
+
+#ifndef _NJS_HASH_H_INCLUDED_
+#define _NJS_HASH_H_INCLUDED_
+
+
+typedef struct {
+ uint64_t bytes;
+ uint32_t a, b, c, d, e, f, g, h;
+ u_char buffer[64];
+} njs_hash_t;
+
+
+NJS_EXPORT void njs_md5_init(njs_hash_t *ctx);
+NJS_EXPORT void njs_md5_update(njs_hash_t *ctx, const void *data, size_t size);
+NJS_EXPORT void njs_md5_final(u_char result[32], njs_hash_t *ctx);
+
+NJS_EXPORT void njs_sha1_init(njs_hash_t *ctx);
+NJS_EXPORT void njs_sha1_update(njs_hash_t *ctx, const void *data, size_t size);
+NJS_EXPORT void njs_sha1_final(u_char result[32], njs_hash_t *ctx);
+
+NJS_EXPORT void njs_sha2_init(njs_hash_t *ctx);
+NJS_EXPORT void njs_sha2_update(njs_hash_t *ctx, const void *data, size_t size);
+NJS_EXPORT void njs_sha2_final(u_char result[32], njs_hash_t *ctx);
+
+
+#endif /* _NJS_HASH_H_INCLUDED_ */
#include <njs_regex.h>
-#include <njs_md5.h>
-#include <njs_sha1.h>
-#include <njs_sha2.h>
+#include <njs_hash.h>
#include <njs.h>
#include <njs_value.h>
#include <njs_main.h>
-static const u_char *njs_md5_body(njs_md5_t *ctx, const u_char *data,
+static const u_char *njs_md5_body(njs_hash_t *ctx, const u_char *data,
size_t size);
void
-njs_md5_init(njs_md5_t *ctx)
+njs_md5_init(njs_hash_t *ctx)
{
ctx->a = 0x67452301;
ctx->b = 0xefcdab89;
void
-njs_md5_update(njs_md5_t *ctx, const void *data, size_t size)
+njs_md5_update(njs_hash_t *ctx, const void *data, size_t size)
{
size_t used, free;
void
-njs_md5_final(u_char result[16], njs_md5_t *ctx)
+njs_md5_final(u_char result[32], njs_hash_t *ctx)
{
size_t used, free;
*/
static const u_char *
-njs_md5_body(njs_md5_t *ctx, const u_char *data, size_t size)
+njs_md5_body(njs_hash_t *ctx, const u_char *data, size_t size)
{
uint32_t a, b, c, d;
uint32_t saved_a, saved_b, saved_c, saved_d;
+++ /dev/null
-
-/*
- * Copyright (C) Igor Sysoev
- * Copyright (C) Nginx, Inc.
- */
-
-
-#ifndef _NJS_MD5_H_INCLUDED_
-#define _NJS_MD5_H_INCLUDED_
-
-
-typedef struct {
- uint64_t bytes;
- uint32_t a, b, c, d;
- u_char buffer[64];
-} njs_md5_t;
-
-
-NJS_EXPORT void njs_md5_init(njs_md5_t *ctx);
-NJS_EXPORT void njs_md5_update(njs_md5_t *ctx, const void *data, size_t size);
-NJS_EXPORT void njs_md5_final(u_char result[16], njs_md5_t *ctx);
-
-#endif /* _NJS_MD5_H_INCLUDED_ */
#include <njs_main.h>
-static const u_char *njs_sha1_body(njs_sha1_t *ctx, const u_char *data,
+static const u_char *njs_sha1_body(njs_hash_t *ctx, const u_char *data,
size_t size);
void
-njs_sha1_init(njs_sha1_t *ctx)
+njs_sha1_init(njs_hash_t *ctx)
{
ctx->a = 0x67452301;
ctx->b = 0xefcdab89;
void
-njs_sha1_update(njs_sha1_t *ctx, const void *data, size_t size)
+njs_sha1_update(njs_hash_t *ctx, const void *data, size_t size)
{
size_t used, free;
void
-njs_sha1_final(u_char result[20], njs_sha1_t *ctx)
+njs_sha1_final(u_char result[32], njs_hash_t *ctx)
{
size_t used, free;
*/
static const u_char *
-njs_sha1_body(njs_sha1_t *ctx, const u_char *data, size_t size)
+njs_sha1_body(njs_hash_t *ctx, const u_char *data, size_t size)
{
uint32_t a, b, c, d, e, temp;
uint32_t saved_a, saved_b, saved_c, saved_d, saved_e;
+++ /dev/null
-
-/*
- * Copyright (C) Igor Sysoev
- * Copyright (C) NGINX, Inc.
- */
-
-
-#ifndef _NJS_SHA1_H_INCLUDED_
-#define _NJS_SHA1_H_INCLUDED_
-
-
-typedef struct {
- uint64_t bytes;
- uint32_t a, b, c, d, e;
- u_char buffer[64];
-} njs_sha1_t;
-
-
-NJS_EXPORT void njs_sha1_init(njs_sha1_t *ctx);
-NJS_EXPORT void njs_sha1_update(njs_sha1_t *ctx, const void *data, size_t size);
-NJS_EXPORT void njs_sha1_final(u_char result[20], njs_sha1_t *ctx);
-
-
-#endif /* _NJS_SHA1_H_INCLUDED_ */
#include <njs_main.h>
-static const u_char *njs_sha2_body(njs_sha2_t *ctx, const u_char *data,
+static const u_char *njs_sha2_body(njs_hash_t *ctx, const u_char *data,
size_t size);
void
-njs_sha2_init(njs_sha2_t *ctx)
+njs_sha2_init(njs_hash_t *ctx)
{
ctx->a = 0x6a09e667;
ctx->b = 0xbb67ae85;
void
-njs_sha2_update(njs_sha2_t *ctx, const void *data, size_t size)
+njs_sha2_update(njs_hash_t *ctx, const void *data, size_t size)
{
size_t used, free;
void
-njs_sha2_final(u_char result[32], njs_sha2_t *ctx)
+njs_sha2_final(u_char result[32], njs_hash_t *ctx)
{
size_t used, free;
*/
static const u_char *
-njs_sha2_body(njs_sha2_t *ctx, const u_char *data, size_t size)
+njs_sha2_body(njs_hash_t *ctx, const u_char *data, size_t size)
{
uint32_t a, b, c, d, e, f, g, h, s0, s1, temp1, temp2;
uint32_t saved_a, saved_b, saved_c, saved_d, saved_e, saved_f,
+++ /dev/null
-
-/*
- * Copyright (C) Dmitry Volyntsev
- * Copyright (C) NGINX, Inc.
- */
-
-
-#ifndef _NJS_SHA2_H_INCLUDED_
-#define _NJS_SHA2_H_INCLUDED_
-
-
-typedef struct {
- uint64_t bytes;
- uint32_t a, b, c, d, e, f, g, h;
- u_char buffer[64];
-} njs_sha2_t;
-
-
-NJS_EXPORT void njs_sha2_init(njs_sha2_t *ctx);
-NJS_EXPORT void njs_sha2_update(njs_sha2_t *ctx, const void *data, size_t size);
-NJS_EXPORT void njs_sha2_final(u_char result[32], njs_sha2_t *ctx);
-
-
-#endif /* _NJS_SHA2_H_INCLUDED_ */