Skip to content

Commit 573eab9

Browse files
aduh95targos
authored andcommitted
crypto: refactor ArrayBuffer to bigint conversion utils
PR-URL: #45567 Refs: nodejs/performance#16 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fbd2d27 commit 573eab9

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎lib/internal/crypto/random.js‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
const{
44
Array,
5+
ArrayBufferPrototypeGetByteLength,
56
ArrayPrototypeForEach,
67
ArrayPrototypePush,
78
ArrayPrototypeShift,
89
ArrayPrototypeSplice,
910
BigInt,
11+
BigIntPrototypeToString,
12+
DataView,
13+
DataViewPrototypeGetUint8,
1014
FunctionPrototypeBind,
1115
FunctionPrototypeCall,
1216
MathMin,
1317
NumberIsNaN,
1418
NumberIsSafeInteger,
1519
NumberPrototypeToString,
20+
StringFromCharCodeApply,
1621
StringPrototypePadStart,
1722
}=primordials;
1823

@@ -493,17 +498,39 @@ function generatePrimeSync(size, options = kEmptyObject) {
493498
returnjob.result(prime);
494499
}
495500

496-
functionarrayBufferToUnsignedBigInt(arrayBuffer){
497-
returnBigInt(`0x${Buffer.from(arrayBuffer).toString('hex')}`);
501+
/**
502+
* 48 is the ASCII code for '0', 97 is the ASCII code for 'a'.
503+
* @param {number} number An integer between 0 and 15.
504+
* @returns {number} corresponding to the ASCII code of the hex representation
505+
* of the parameter.
506+
*/
507+
constnumberToHexCharCode=(number)=>(number<10 ? 48 : 87)+number;
508+
509+
/**
510+
* @param {ArrayBuffer} buf An ArrayBuffer.
511+
* @return {bigint}
512+
*/
513+
functionarrayBufferToUnsignedBigInt(buf){
514+
constlength=ArrayBufferPrototypeGetByteLength(buf);
515+
constchars=Array(length*2);
516+
constview=newDataView(buf);
517+
518+
for(leti=0;i<length;i++){
519+
constval=DataViewPrototypeGetUint8(view,i);
520+
chars[2*i]=numberToHexCharCode(val>>4);
521+
chars[2*i+1]=numberToHexCharCode(val&0xf);
522+
}
523+
524+
returnBigInt(`0x${StringFromCharCodeApply(chars)}`);
498525
}
499526

500527
functionunsignedBigIntToBuffer(bigint,name){
501528
if(bigint<0){
502529
thrownewERR_OUT_OF_RANGE(name,'>= 0',bigint);
503530
}
504531

505-
consthex=bigint.toString(16);
506-
constpadded=hex.padStart(hex.length+(hex.length%2),0);
532+
consthex=BigIntPrototypeToString(bigint,16);
533+
constpadded=StringPrototypePadStart(hex,hex.length+(hex.length%2),0);
507534
returnBuffer.from(padded,'hex');
508535
}
509536

‎lib/internal/per_context/primordials.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const varargsMethods = [
4545
'MathHypot',
4646
'MathMax',
4747
'MathMin',
48+
'StringFromCharCode',
49+
'StringFromCodePoint',
4850
'StringPrototypeConcat',
4951
'TypedArrayOf',
5052
];

‎typings/primordials.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ declare namespace primordials {
422422
exportconstStringName: typeofString.name
423423
exportconstStringPrototype: typeofString.prototype
424424
exportconstStringFromCharCode: typeofString.fromCharCode
425+
exportconstStringFromCharCodeApply: StaticApply<typeofString.fromCharCode>
425426
exportconstStringFromCodePoint: typeofString.fromCodePoint
427+
exportconstStringFromCodePointApply: StaticApply<typeofString.fromCodePoint>
426428
exportconstStringRaw: typeofString.raw
427429
exportconstStringPrototypeAnchor: UncurryThis<typeofString.prototype.anchor>
428430
exportconstStringPrototypeBig: UncurryThis<typeofString.prototype.big>

0 commit comments

Comments
 (0)