import { randBytes , randBytesSync } from './randBytes' ; param bitLength param forceLength throws returns export function randBits ( bitLength , forceLength = false ) { if ( bitLength < 1 ) throw new RangeError ( 'bitLength MUST be > 0' ) ; const byteLength = Math . ceil ( bitLength / 8 ) ; const bitLengthMod8 = bitLength % 8 ; return new Promise ( ( resolve , reject ) => { randBytes ( byteLength , false ) . then ( function ( rndBytes ) { if ( bitLengthMod8 !== 0 ) { rndBytes [ 0 ] = rndBytes [ 0 ] & ( 2 ** bitLengthMod8 - 1 ) ; } if ( forceLength ) { const mask = ( bitLengthMod8 !== 0 ) ? 2 ** ( bitLengthMod8 - 1 ) : 128 ; rndBytes [ 0 ] = rndBytes [ 0 ] | mask ; } resolve ( rndBytes ) ; } ) ; } ) ; } param bitLength param forceLength throws returns export function randBitsSync ( bitLength , forceLength = false ) { if ( bitLength < 1 ) throw new RangeError ( 'bitLength MUST be > 0' ) ; const byteLength = Math . ceil ( bitLength / 8 ) ; const rndBytes = randBytesSync ( byteLength , false ) ; const bitLengthMod8 = bitLength % 8 ; if ( bitLengthMod8 !== 0 ) { rndBytes [ 0 ] = rndBytes [ 0 ] & ( 2 ** bitLengthMod8 - 1 ) ; } if ( forceLength ) { const mask = ( bitLengthMod8 !== 0 ) ? 2 ** ( bitLengthMod8 - 1 ) : 128 ; rndBytes [ 0 ] = rndBytes [ 0 ] | mask ; } return rndBytes ; }