UNPKG

@safeheron/crypto-utils

Version:
113 lines (112 loc) 3.02 kB
import { CryptoJSBytes } from "./exportTypes"; export declare class Hex { /** * Hex ===> Bytes: * "0100ff" => [0x01, 0x00, 0xff] * * @param hex */ static toBytes(hex: string): Uint8Array; /** * Bytes(Uint8Array) ===> Hex: * [0x01, 0x00, 0xff] => "0100ff" * * @param bytes */ static fromBytes(bytes: Uint8Array): string; /** * Hex ===> CryptoJSBytes * * "1234" ===> 0x1234 * "123" ===> 0x0123 * "120" ===> 0x0120 * * Use the function to avoid the bugs in "crypto-js" * "123" ===> 0x1203 * "120" ===> 0x1200 * * More details for the bugs in "crypto-js": * - Problem 1 * const a = CryptoJS.enc.Hex.parse('d6021ef5d7cccd55cda318fe2bd47334bac0b699e4a6676f8d941f7706d3820') * const expected_a = a.toString(CryptoJS.enc.Hex) * console.log('expected_a:', expected_a) * * The result is: * expected_a: d6021ef5d7cccd55cda318fe2bd47334bac0b699e4a6676f8d941f7706d38200 * * - Problem 2 * const a = CryptoJS.enc.Hex.parse('d6021ef5d7cccd55cda318fe2bd47334bac0b699e4a6676f8d941f7706d3821') * const expected_a = a.toString(CryptoJS.enc.Hex) * console.log('expected_a:', expected_a) * * The result is: * expected_a: d6021ef5d7cccd55cda318fe2bd47334bac0b699e4a6676f8d941f7706d38201 * @param hex */ static toCryptoJSBytes(hex: string): CryptoJSBytes; /** * Bytes ===> Hex * @param bytes */ static fromCryptoJSBytes(bytes: CryptoJSBytes): string; /** * Reverse hex string byte by byte. * "01234567" ===> "67452301" * @param hex */ static reverseHex(hex: string): string; /** * Pad the hex string to the specified length * @param hex. For example "01020304" * @param expectedLen. For example 16 * @returns {*} For example "0000000001020304" */ static padLength(hex: string, expectedLen: number): string; /** * Pad to 64 chars long * @param hex * @returns {*} */ static pad64(hex: string): string; /** * Pad to 32 chars long * @param hex * @returns {*} */ static pad32(hex: string): string; /** * Pad to 16 chars long * "12345678" => "0000000012345678" * @param hex * @returns {*} */ static pad16(hex: string): string; /** * Pad to 8 chars long * "1234567" => "01234567" * @param hex * @returns {*} */ static pad8(hex: string): string; /** * Pad to 4 chars long * "1" => "0001" * @param hex * @returns {*} */ static pad4(hex: string): string; /** * Pad to 2 chars long * "1" => "01" * @param hex * @returns {*} */ static pad2(hex: string): string; /** * Pad to even chars. * "123" => "0123" * @param hex * @returns {*} */ static padEven(hex: string): string; }