import 'node-forge/lib/util.js' ; import 'node-forge/lib/jsbn.js' ; import forge from 'node-forge/lib/forge.js' ; import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' ; import { toString as uint8ArrayToString } from 'uint8arrays/to-string' ; import { concat as uint8ArrayConcat } from 'uint8arrays/concat' ; export function bigIntegerToUintBase64url ( num , len ? ) { let buf = Uint8Array . from ( num . abs ( ) . toByteArray ( ) ) ; buf = buf [ 0 ] === 0 ? buf . slice ( 1 ) : buf ; if ( len != null ) { if ( buf . length > len ) throw new Error ( 'byte array longer than desired length' ) ; buf = uint8ArrayConcat ( [ new Uint8Array ( len - buf . length ) , buf ] ) ; } return uint8ArrayToString ( buf , 'base64url' ) ; } export function base64urlToBigInteger ( str ) { const buf = base64urlToBuffer ( str ) ; return new forge . jsbn . BigInteger ( uint8ArrayToString ( buf , 'base16' ) , 16 ) ; } export function base64urlToBuffer ( str , len ? ) { let buf = uint8ArrayFromString ( str , 'base64urlpad' ) ; if ( len != null ) { if ( buf . length > len ) throw new Error ( 'byte array longer than desired length' ) ; buf = uint8ArrayConcat ( [ new Uint8Array ( len - buf . length ) , buf ] ) ; } return buf ; }