GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — crypto: improve random synchronous number generation performance · nodejs/node@4613bec

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/crypto
    • random.js
  • src/crypto
    • crypto_random.cc
  • test/parallel
    • test-crypto-randomfillsync-trace-sync-io.js
  • typings/internalBinding
    • crypto.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/crypto/random.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/random.js
+2-10Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const {
3535
CheckPrimeJob,
3636
kCryptoJobAsync,
3737
kCryptoJobSync,
38+
randomFillSync: randomFillSyncImpl,
3839
secureBuffer,
3940
} = internalBinding('crypto');
4041

@@ -184,16 +185,7 @@ function randomFillSync(buf, offset = 0, size) {
184185
if (size === 0)
185186
return buf;
186187

187-
const job = new RandomBytesJob(
188-
kCryptoJobSync,
189-
buf,
190-
offset,
191-
size);
192-
193-
const err = job.run()[0];
194-
if (err)
195-
throw err;
196-
188+
randomFillSyncImpl(buf, offset, size);
197189
return buf;
198190
}
199191

Collapse file

‎src/crypto/crypto_random.cc‎

Copy file name to clipboardExpand all lines: src/crypto/crypto_random.cc
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,46 @@ MaybeLocal<Value> CheckPrimeTraits::EncodeOutput(Environment* env,
211211
}
212212

213213
namespace Random {
214+
static void RandomFillSync(const FunctionCallbackInfo<Value>& args) {
215+
Environment* env = Environment::GetCurrent(args);
216+
CHECK(IsAnyBufferSource(args[0])); // Buffer to fill
217+
CHECK(args[1]->IsUint32()); // Offset
218+
CHECK(args[2]->IsUint32()); // Size
219+
220+
ArrayBufferOrViewContents<unsigned char> in(args[0]);
221+
222+
const uint32_t byte_offset = args[1].As<Uint32>()->Value();
223+
const uint32_t size = args[2].As<Uint32>()->Value();
224+
CHECK_GE(byte_offset + size, byte_offset); // Overflow check.
225+
CHECK_LE(byte_offset + size, in.size()); // Bounds check.
226+
227+
env->PrintSyncTrace();
228+
if (ERR_peek_error() != 0) ERR_clear_error();
229+
if (ncrypto::CSPRNG(in.data() + byte_offset, size)) {
230+
if (ERR_peek_error() != 0) ERR_clear_error();
231+
return;
232+
}
233+
234+
CryptoErrorStore errors;
235+
errors.Capture();
236+
if (errors.Empty()) {
237+
errors.Insert(NodeCryptoError::DERIVING_BITS_FAILED);
238+
}
239+
Local<Value> exception;
240+
if (errors.ToException(env).ToLocal(&exception)) {
241+
env->isolate()->ThrowException(exception);
242+
}
243+
}
244+
214245
void Initialize(Environment* env, Local<Object> target) {
246+
SetMethod(env->context(), target, "randomFillSync", RandomFillSync);
215247
RandomBytesJob::Initialize(env, target);
216248
RandomPrimeJob::Initialize(env, target);
217249
CheckPrimeJob::Initialize(env, target);
218250
}
219251

220252
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
253+
registry->Register(RandomFillSync);
221254
RandomBytesJob::RegisterExternalReferences(registry);
222255
RandomPrimeJob::RegisterExternalReferences(registry);
223256
CheckPrimeJob::RegisterExternalReferences(registry);
Collapse file

‎test/parallel/test-crypto-randomfillsync-trace-sync-io.js‎

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
const assert = require('assert');
7+
const { spawnSync } = require('child_process');
8+
9+
// randomFillSync() should be reported by --trace-sync-io when it runs after
10+
// the first event loop turn.
11+
12+
if (process.argv[2] === 'child') {
13+
setImmediate(() => {
14+
require('crypto').randomFillSync(Buffer.alloc(16));
15+
});
16+
return;
17+
}
18+
19+
const { stderr, status } = spawnSync(process.execPath,
20+
['--trace-sync-io', __filename, 'child'],
21+
{ encoding: 'utf8' });
22+
assert.strictEqual(status, 0);
23+
assert.match(stderr, /WARNING: Detected use of sync API[\s\S]*randomFillSync/);
Collapse file

‎typings/internalBinding/crypto.d.ts‎

Copy file name to clipboardExpand all lines: typings/internalBinding/crypto.d.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ export interface CryptoBinding {
10021002
privateEncrypt: InternalCryptoBinding.PublicKeyCipher;
10031003
publicDecrypt: InternalCryptoBinding.PublicKeyCipher;
10041004
publicEncrypt: InternalCryptoBinding.PublicKeyCipher;
1005+
randomFillSync(buf: ArrayBufferLike | ArrayBufferView, offset: number, size: number): void;
10051006
resetRootCertStore(): void;
10061007
secureBuffer(length: number): Uint8Array | undefined;
10071008
secureHeapUsed(): bigint | undefined;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.