-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buffer: auto random fill Buffer(num) and new Buffer(num) #11808
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,11 +22,14 @@ | |
'use strict'; | ||
|
||
const binding = process.binding('buffer'); | ||
const config = process.binding('config'); | ||
const { compare: compare_, compareOffset } = binding; | ||
const { isArrayBuffer, isSharedArrayBuffer, isUint8Array } = | ||
process.binding('util'); | ||
const bindingObj = {}; | ||
const internalUtil = require('internal/util'); | ||
const zeroFillBuffers = !!config.zeroFillBuffers; | ||
const randomFill = zeroFillBuffers ? 0 : Math.floor(Math.random() * 256); | ||
|
||
class FastBuffer extends Uint8Array { | ||
constructor(arg1, arg2, arg3) { | ||
|
@@ -102,7 +105,7 @@ function Buffer(arg, encodingOrOffset, length) { | |
'If encoding is specified then the first argument must be a string' | ||
); | ||
} | ||
return Buffer.allocUnsafe(arg); | ||
return Buffer.allocUnsafe(arg).fill(randomFill); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing this seems... slow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, it is. I went with this approach here because it requires the least amount of code. It is more performant to do a I maintain that the best thing to do would be to just use |
||
} | ||
return Buffer.from(arg, encodingOrOffset, length); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not gather
crypto.randomBytes()
ifcrypto
is available?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we only need a single byte.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(and there's no need for it to be cryptographically safe)