MCPcopy Index your code
hub / github.com/nodejs/node / truncateToBitLength

Function truncateToBitLength

lib/internal/crypto/util.js:570–591  ·  view source on GitHub ↗

* Copies `bytes` up to the byte length needed for `length` bits, then clears * unused least-significant bits in the final byte. * @param {number} length * @param {ArrayBuffer|ArrayBufferView} bytes * @returns {Uint8Array}

(length, bytes)

Source from the content-addressed store, hash-verified

568 * @returns {Uint8Array}
569 */
570function truncateToBitLength(length, bytes) {
571 const lengthBytes = numBitsToBytes(length);
572 const isView = ArrayBufferIsView(bytes);
573 const byteView = isView ?
574 new Uint8Array(
575 getDataViewOrTypedArrayBuffer(bytes),
576 getDataViewOrTypedArrayByteOffset(bytes),
577 getDataViewOrTypedArrayByteLength(bytes),
578 ) :
579 new Uint8Array(bytes, 0, ArrayBufferPrototypeGetByteLength(bytes));
580 const result = TypedArrayPrototypeSlice(
581 byteView,
582 0,
583 lengthBytes,
584 );
585
586 const remainder = length % 8;
587 if (remainder !== 0)
588 result[lengthBytes - 1] &= (0xff << (8 - remainder)) & 0xff;
589
590 return result;
591}
592
593let webidl;
594

Callers 3

normalizeKeyLengthFunction · 0.85
asyncDigestFunction · 0.85
ecdhDeriveBitsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…