Module: util

This object contains utility functions

Source:

Requires

Members

(static) concat

Concat a list of Uint8Arrays, Strings or Streams The caller must not mix Uint8Arrays with Strings, but may mix Streams with non-Streams.

Source:

(static) concatUint8Array

Concat Uint8Arrays

Source:

Methods

(static) b64_to_Uint8Array(base64) → {Uint8Array}

Convert a Base-64 encoded string an array of 8-bit integer

Note: accepts both Radix-64 and URL-safe strings

Parameters:
Name Type Description
base64 String

Base-64 encoded string to convert

Source:
Returns:

An array of 8-bit integers

Type
Uint8Array

(static) calc_checksum(Uint8Array) → {Integer}

Calculates a 16bit sum of a Uint8Array by adding each character codes modulus 65535

Parameters:
Name Type Description
Uint8Array Uint8Array

to create a sum of

Source:
Returns:

An integer containing the sum of all character codes % 65535

Type
Integer

(static) canonicalizeEOL()

Normalize line endings to \r\n

Source:

(static) decode_utf8(utf8) → {String|ReadableStream}

Convert a Uint8Array of utf8 bytes to a native javascript string

Parameters:
Name Type Description
utf8 Uint8Array | ReadableStream

A valid squence of utf8 bytes

Source:
Returns:

A native javascript string

Type
String | ReadableStream

(static) detectNode()

Detect Node.js runtime.

Source:

(static) double(data)

If S[1] == 0, then double(S) == (S[2..128] || 0); otherwise, double(S) == (S[2..128] || 0) xor (zeros(120) || 10000111).

Both OCB and EAX (through CMAC) require this function to be constant-time.

Parameters:
Name Type Description
data Uint8Array
Source:

(static) encode_utf8(str) → {Uint8Array|ReadableStream}

Convert a native javascript string to a Uint8Array of utf8 bytes

Parameters:
Name Type Description
str String | ReadableStream

The string to convert

Source:
Returns:

A valid squence of utf8 bytes

Type
Uint8Array | ReadableStream

(static) encodeZBase32(data) → {String}

Encode input buffer using Z-Base32 encoding. See: https://tools.ietf.org/html/rfc6189#section-5.1.6

Parameters:
Name Type Description
data Uint8Array

The binary data to encode

Source:
Returns:

Binary data encoded using Z-Base32

Type
String

(static) equalsUint8Array(first, second) → {Boolean}

Check Uint8Array equality

Parameters:
Name Type Description
first Uint8Array

array

second Uint8Array

array

Source:
Returns:

equality

Type
Boolean

(static) formatUserId()

Format user id for internal use.

Source:

(static) getNodeBuffer() → {function}

Get native Node.js Buffer constructor. This should be used since Buffer is not available under browserify.

Source:
Returns:

The Buffer constructor or 'undefined'

Type
function

(static) getNodeCrypto() → {Object}

Get native Node.js crypto api. The default configuration is to use the api when available. But it can also be deactivated with config.use_native

Source:
Returns:

The crypto module or 'undefined'

Type
Object

(static) getTransferables(obj) → {Array.<ArrayBuffer>}

Get transferable objects to pass buffers with zero copy (similar to "pass by reference" in C++) See: https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage Also, convert ReadableStreams to MessagePorts

Parameters:
Name Type Description
obj Object

the options object to be passed to the web worker

Source:
Returns:

an array of binary data to be passed

Type
Array.<ArrayBuffer>

(static) getWebCrypto() → {Object}

Get native Web Cryptography api, only the current version of the spec. The default configuration is to use the api when available. But it can be deactivated with config.use_native

Source:
Returns:

The SubtleCrypto api or 'undefined'

Type
Object

(static) getWebCryptoAll() → {Object}

Get native Web Cryptography api for all browsers, including legacy implementations of the spec e.g IE11 and Safari 8/9. The default configuration is to use the api when available. But it can be deactivated with config.use_native

Source:
Returns:

The SubtleCrypto api or 'undefined'

Type
Object

(static) hex_to_str(str) → {String}

Create binary string from a hex encoded string

Parameters:
Name Type Description
str String

Hex string to convert

Source:
Returns:
Type
String

(static) hex_to_Uint8Array(hex) → {Uint8Array}

Convert a hex string to an array of 8-bit integers

Parameters:
Name Type Description
hex String

A hex string to convert

Source:
Returns:

An array of 8-bit integers

Type
Uint8Array

(static) nativeEOL()

Convert line endings from canonicalized \r\n to native \n

Source:

(static) nodeRequire(The) → {Object}

Get native Node.js module

Parameters:
Name Type Description
The String

module to require

Source:
Returns:

The required module or 'undefined'

Type
Object

(static) parseUserId()

Parse user id.

Source:

(static) print_debug(str)

Helper function to print a debug message. Debug messages are only printed if

Parameters:
Name Type Description
str String

String of the debug message

Source:

(static) print_debug_error(str)

Helper function to print a debug error. Debug messages are only printed if

Parameters:
Name Type Description
str String

String of the debug message

Source:

(static) print_debug_hexarray_dump(str)

Helper function to print a debug message. Debug messages are only printed if

Parameters:
Name Type Description
str String

String of the debug message

Source:

(static) print_debug_hexstr_dump(str)

Helper function to print a debug message. Debug messages are only printed if

Parameters:
Name Type Description
str String

String of the debug message

Source:

(static) print_entire_stream(str, input, concat)

Read a stream to the end and print it to the console when it's closed.

Parameters:
Name Type Description
str String

String of the debug message

input ReadableStream | Uint8array | String

Stream to print

concat function

Function to concatenate chunks of the stream (defaults to util.concat).

Source:

(static) removeTrailingSpaces()

Remove trailing spaces and tabs from each line

Source:

(static) restoreStreams(obj) → {Object}

Convert MessagePorts back to ReadableStreams

Parameters:
Name Type Description
obj Object
Source:
Returns:
Type
Object

(static) shiftRight(array, bits) → {String}

Shift a Uint8Array to the right by n bits

Parameters:
Name Type Description
array Uint8Array

The array to shift

bits Integer

Amount of bits to shift (MUST be smaller than 8)

Source:
Returns:

Resulting array.

Type
String

(static) str_to_hex(str) → {String}

Create hex string from a binary

Parameters:
Name Type Description
str String

String to convert

Source:
Returns:

String containing the hexadecimal values

Type
String

(static) str_to_Uint8Array(str) → {Uint8Array}

Convert a string to an array of 8-bit integers

Parameters:
Name Type Description
str String

String to convert

Source:
Returns:

An array of 8-bit integers

Type
Uint8Array

(static) Uint8Array_to_b64(bytes, url) → {String}

Convert an array of 8-bit integer to a Base-64 encoded string

Parameters:
Name Type Description
bytes Uint8Array

An array of 8-bit integers to convert

url bool

If true, output is URL-safe

Source:
Returns:

Base-64 encoded string

Type
String

(static) Uint8Array_to_hex(bytes) → {String}

Convert an array of 8-bit integers to a hex string

Parameters:
Name Type Description
bytes Uint8Array

Array of 8-bit integers to convert

Source:
Returns:

Hexadecimal representation of the array

Type
String

(static) Uint8Array_to_MPI(bin) → {Uint8Array}

Convert a Uint8Array to an MPI-formatted Uint8Array. Note: the output is not an MPI object.

Parameters:
Name Type Description
bin Uint8Array

An array of 8-bit integers to convert

Source:
See:
  • module:type/mpi/MPI.fromUint8Array
  • module:type/mpi/MPI.toUint8Array
Returns:

MPI-formatted Uint8Array

Type
Uint8Array

(static) Uint8Array_to_str(bytes) → {String}

Convert an array of 8-bit integers to a string

Parameters:
Name Type Description
bytes Uint8Array

An array of 8-bit integers to convert

Source:
Returns:

String representation of the array

Type
String