UNPKG

react-native-quick-crypto

Version:

A fast implementation of Node's `crypto` module written in C/C++ JSI

75 lines (73 loc) 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDefaultEncoding = getDefaultEncoding; exports.getUIntOption = getUIntOption; exports.normalizeEncoding = normalizeEncoding; exports.setDefaultEncoding = setDefaultEncoding; exports.validateEncoding = validateEncoding; // Mimics node behavior for default global encoding let defaultEncoding = 'buffer'; function setDefaultEncoding(encoding) { defaultEncoding = encoding; } function getDefaultEncoding() { return defaultEncoding; } function normalizeEncoding(enc) { if (!enc) return 'utf8'; let retried; while (true) { switch (enc) { case 'utf8': case 'utf-8': return 'utf8'; case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return 'utf16le'; case 'latin1': case 'binary': return 'latin1'; case 'base64': case 'ascii': case 'hex': return enc; default: if (retried) return; // undefined enc = ('' + enc).toLowerCase(); retried = true; } } } function validateEncoding(data, encoding) { const normalizedEncoding = normalizeEncoding(encoding); const length = data.length; if (normalizedEncoding === 'hex' && length % 2 !== 0) { throw new Error(`Encoding ${encoding} not valid for data length ${length}`); } } /** * Reads an unsigned-integer option from an options-like object. * * Returns `undefined` if the option is missing, `null`, or `undefined`. * Throws `RangeError` if the value is present but not a non-negative * 32-bit integer (NaN, Infinity, fractional, negative, or > 2^32 - 1). * * Replaces the previous `Record<string, any>` + sentinel-`-1` signature, * which defeated the type checker (audit Phase 1.4). Callers that used * `getUIntOption(opts ?? {}, key) !== -1 ? getUIntOption(...) : default` * collapse to `getUIntOption(opts, key) ?? default`. */ function getUIntOption(options, key) { if (options == null) return undefined; const value = options[key]; if (value == null) return undefined; if (typeof value !== 'number' || !Number.isFinite(value) || !Number.isInteger(value) || value < 0 || value > 0xffff_ffff) { throw new RangeError(`options.${key} must be a non-negative 32-bit integer, got ${String(value)}`); } return value; } //# sourceMappingURL=cipher.js.map