UNPKG

matterbridge

Version:
89 lines 3.9 kB
/** * This file contains the hex functions. * * @file hex.ts * @author Luca Liguori * @created 2025-05-06 * @version 1.0.0 * @license Apache-2.0 * * Copyright 2025, 2026, 2027 Luca Liguori. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Converts an ArrayBuffer or Uint8Array to a hexadecimal string. * * Accepts any {ArrayBufferLike} input: a raw ArrayBuffer (binary data storage) or * a Uint8Array (TypedArray view over an ArrayBuffer). While an ArrayBuffer holds * the raw memory, a Uint8Array is a typed view into that memory, letting you access * its bytes directly. This function normalizes both by creating a Uint8Array view * before conversion. * * @param {ArrayBufferLike} buffer - The buffer or typed-array view to convert. * @returns {string} A lowercase hex string representation of the buffer's bytes. * * @throws {TypeError} If the input is not an ArrayBuffer or ArrayBufferView. */ export declare function bufferToHex(buffer: ArrayBufferLike): string; /** * Converts a hexadecimal string to a Uint8Array. * * @param {string} hex - The hex string to convert. Can include uppercase or lowercase characters. * @returns {Uint8Array} A Uint8Array representing the corresponding binary data. * * @throws {TypeError} If the input is not a string. * @throws {Error} If the input length is odd or contains non-hex characters. */ export declare function hexToBuffer(hex: string): Uint8Array; /** * Converts a PEM (Privacy-Enhanced Mail) formatted string to a Uint8Array. * * PEM format is a base64-encoded format commonly used for cryptographic keys and certificates, * wrapped with header and footer lines like "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----". * This function extracts the base64 content and converts it to binary data using Node.js Buffer API. * * @param {string} pem - The PEM formatted string to convert. * @param {boolean} validate - Whether to validate the PEM content using Node.js crypto module. Default is false. * @returns {Uint8Array} A Uint8Array representing the decoded binary data. * * @throws {TypeError} If the input is not a string. * @throws {Error} If the PEM format is invalid, contains invalid base64 characters, or validation fails. */ export declare function pemToBuffer(pem: string, validate?: boolean): Uint8Array; /** * Extracts the raw 32-byte private key scalar from an EC private key PEM. * * This function parses an EC private key in PEM format and extracts the raw * 32-byte private scalar (the "d" value) used in elliptic curve cryptography. * This is the actual secret key material. * * @param {string} pemPrivateKey - The EC private key in PEM format * @returns {Uint8Array} The raw 32-byte private key scalar * @throws {Error} If the input is not a valid EC private key PEM * * @example * ```typescript * const pemKey = `-----BEGIN EC PRIVATE KEY----- * MHcCAQEEIFBxqFkUxMoN2JkUXLFeiZnLNUpftjLi0sKMbZ6uajHXoAoGCCqGSM49 * AwEHoUQDQgAEcJ3eH/rG3zf9WmqMjh5eBSkeXz3Cb4Fig1rTosfHmgyjx/WnLPEe * BF4SHvLo0G2COJEHa/VDE5EiKWO9ZR6AOQ== * -----END EC PRIVATE KEY-----`; * * const rawKey = extractPrivateKeyRaw(pemKey); * console.log(rawKey.length); // 32 * console.log(bufferToHex(rawKey)); // hex representation of the private scalar * ``` */ export declare function extractPrivateKeyRaw(pemPrivateKey: string): Uint8Array; //# sourceMappingURL=hex.d.ts.map