From facf602fdc98b68de22be89bd99a019bdc39f9f2 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Mon, 21 Nov 2022 17:17:47 +0000 Subject: [PATCH] add randombytes_buf_deterministic --- randombytes.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/randombytes.js b/randombytes.js index f23eefe..b1da871 100644 --- a/randombytes.js +++ b/randombytes.js @@ -1,4 +1,12 @@ var assert = require('nanoassert') +const { + crypto_stream_chacha20_ietf, + crypto_stream_chacha20_ietf_KEYBYTES, + crypto_stream_chacha20_ietf_NONCEBYTES +} = require('./crypto_stream_chacha20') + +const randombytes_SEEDBYTES = 32 + var randombytes = (function () { var QUOTA = 65536 // limit for QuotaExceededException var crypto = typeof global !== 'undefined' ? crypto = (global.crypto || global.msCrypto) : null @@ -34,7 +42,21 @@ Object.defineProperty(module.exports, 'randombytes', { value: randombytes }) -module.exports.randombytes_buf = function (out) { +function randombytes_buf (out) { assert(out, 'out must be given') randombytes(out, out.length) } + +function randombytes_buf_deterministic (buf, seed) { + const nonce = Buffer.alloc(crypto_stream_chacha20_ietf_NONCEBYTES) + nonce.write('LibsodiumDRG') + + assert(randombytes_SEEDBYTES === crypto_stream_chacha20_ietf_KEYBYTES) + crypto_stream_chacha20_ietf(buf, nonce, seed) +} + +module.exports = { + randombytes_buf, + randombytes_buf_deterministic, + randombytes_SEEDBYTES +} \ No newline at end of file