Welcome! Log In Create A New Profile

Advanced

[njs] Crypto: added Hash.copy() method.

Dmitry Volyntsev
March 21, 2023 12:28AM
details: https://hg.nginx.org/njs/rev/edf1c2aef957
branches:
changeset: 2074:edf1c2aef957
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Mon Mar 20 21:25:47 2023 -0700
description:
Crypto: added Hash.copy() method.

This closes #625 issue on Github.

diffstat:

external/njs_crypto_module.c | 42 ++++++++++++++++++++++++++++++++++++++++++
src/test/njs_unit_test.c | 20 ++++++++++++++++++++
2 files changed, 62 insertions(+), 0 deletions(-)

diffs (96 lines):

diff -r fbd36220ea17 -r edf1c2aef957 external/njs_crypto_module.c
--- a/external/njs_crypto_module.c Mon Mar 20 21:09:58 2023 -0700
+++ b/external/njs_crypto_module.c Mon Mar 20 21:25:47 2023 -0700
@@ -67,6 +67,8 @@ static njs_int_t njs_hash_prototype_upda
njs_uint_t nargs, njs_index_t hmac);
static njs_int_t njs_hash_prototype_digest(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t hmac);
+static njs_int_t njs_hash_prototype_copy(njs_vm_t *vm, njs_value_t *args,
+ njs_uint_t nargs, njs_index_t hmac);
static njs_int_t njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused);

@@ -173,6 +175,16 @@ static njs_external_t njs_ext_crypto_ha

{
.flags = NJS_EXTERN_METHOD,
+ .name.string = njs_str("copy"),
+ .writable = 1,
+ .configurable = 1,
+ .u.method = {
+ .native = njs_hash_prototype_copy,
+ }
+ },
+
+ {
+ .flags = NJS_EXTERN_METHOD,
.name.string = njs_str("constructor"),
.writable = 1,
.configurable = 1,
@@ -471,6 +483,36 @@ exception:


static njs_int_t
+njs_hash_prototype_copy(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
+ njs_index_t unused)
+{
+ njs_digest_t *dgst, *copy;
+
+ dgst = njs_vm_external(vm, njs_crypto_hash_proto_id, njs_argument(args, 0));
+ if (njs_slow_path(dgst == NULL)) {
+ njs_type_error(vm, "\"this\" is not a hash object");
+ return NJS_ERROR;
+ }
+
+ if (njs_slow_path(dgst->alg == NULL)) {
+ njs_error(vm, "Digest already called");
+ return NJS_ERROR;
+ }
+
+ copy = njs_mp_alloc(njs_vm_memory_pool(vm), sizeof(njs_digest_t));
+ if (njs_slow_path(copy == NULL)) {
+ njs_memory_error(vm);
+ return NJS_ERROR;
+ }
+
+ memcpy(copy, dgst, sizeof(njs_digest_t));
+
+ return njs_vm_external_create(vm, njs_vm_retval(vm),
+ njs_crypto_hash_proto_id, copy, 0);
+}
+
+
+static njs_int_t
njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
diff -r fbd36220ea17 -r edf1c2aef957 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Mon Mar 20 21:09:58 2023 -0700
+++ b/src/test/njs_unit_test.c Mon Mar 20 21:25:47 2023 -0700
@@ -20360,6 +20360,26 @@ static njs_unit_test_t njs_crypto_modul
"OBZPvRdgPXP2lri01yZk1zW7anyIV3aH/SrjP9aWQVM=,"
"OBZPvRdgPXP2lri01yZk1zW7anyIV3aH_SrjP9aWQVM") },

+ { njs_str("const crypto = require('crypto');"
+ "let hash = crypto.createHash('sha256');"
+ "let digests = [];"
+ "hash.update('one');"
+ "digests.push(hash.copy().digest('hex'));"
+ "hash.update('two');"
+ "digests.push(hash.copy().digest('hex'));"
+ "hash.update('three');"
+ "digests.push(hash.copy().digest('hex'));"
+ "digests"),
+ njs_str("7692c3ad3540bb803c020b3aee66cd8887123234ea0c6e7143c0add73ff431ed,"
+ "25b6746d5172ed6352966a013d93ac846e1110d5a25e8f183b5931f4688842a1,"
+ "4592092e1061c7ea85af2aed194621cc17a2762bae33a79bf8ce33fd0168b801") },
+
+ { njs_str("const crypto = require('crypto');"
+ "let hash = crypto.createHash('sha256');"
+ "hash.update('one').digest();"
+ "hash.copy()"),
+ njs_str("Error: Digest already called") },
+
{ njs_str("var hash = require('crypto').createHash;"
"njs.dump(['', 'abc'.repeat(100)].map(v => {"
" return ['md5', 'sha1', 'sha256'].map(h => {"
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Crypto: added Hash.copy() method.

Dmitry Volyntsev 627 March 21, 2023 12:28AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 254
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready