UNPKG

@sv443-network/coreutils

Version:

Cross-platform, general-purpose, JavaScript core library for Node, Deno and the browser. Intended to be used in conjunction with `@sv443-network/userutils` and `@sv443-network/djsutils`, but can be used independently as well.

35 lines (34 loc) 3.38 kB
/** * @module crypto * This module contains various cryptographic functions, like compression and Uint8Array encoding - [see the documentation for more info](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#crypto) */ import type { Stringifiable } from "./types.js"; /** Converts an Uint8Array to a base64-encoded (ASCII) string */ export declare function abtoa(buf: Uint8Array): string; /** Converts a base64-encoded (ASCII) string to an Uint8Array representation of its bytes */ export declare function atoab(str: string): Uint8Array; /** Compresses a string or an Uint8Array using the provided {@linkcode compressionFormat} and returns it as a base64 string */ export declare function compress(input: Stringifiable | Uint8Array, compressionFormat: CompressionFormat, outputType?: "string"): Promise<string>; /** Compresses a string or an Uint8Array using the provided {@linkcode compressionFormat} and returns it as an Uint8Array */ export declare function compress(input: Stringifiable | Uint8Array, compressionFormat: CompressionFormat, outputType: "arrayBuffer"): Promise<Uint8Array>; /** Decompresses a previously compressed base64 string or Uint8Array, with the format passed by {@linkcode compressionFormat}, converted to a string */ export declare function decompress(input: Stringifiable | Uint8Array, compressionFormat: CompressionFormat, outputType?: "string"): Promise<string>; /** Decompresses a previously compressed base64 string or Uint8Array, with the format passed by {@linkcode compressionFormat}, converted to an Uint8Array */ export declare function decompress(input: Stringifiable | Uint8Array, compressionFormat: CompressionFormat, outputType: "arrayBuffer"): Promise<Uint8Array>; /** * Creates a hash / checksum of the given {@linkcode input} string or Uint8Array using the specified {@linkcode algorithm} ("SHA-256" by default). * * - ⚠️ Uses the SubtleCrypto API so it needs to run in a secure context (HTTPS). * - ⚠️ If you use this for cryptography, make sure to use a secure algorithm (under no circumstances use SHA-1) and to [salt](https://en.wikipedia.org/wiki/Salt_(cryptography)) your input data. */ export declare function computeHash(input: string | Uint8Array, algorithm?: string): Promise<string>; /** * Generates a random ID with the specified length and radix (16 characters and hexadecimal by default) * * - ⚠️ Not suitable for generating anything related to cryptography! Use [SubtleCrypto's `generateKey()`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey) for that instead. * @param length The length of the ID to generate (defaults to 16) * @param radix The [radix](https://en.wikipedia.org/wiki/Radix) of each digit (defaults to 16 which is hexadecimal. Use 2 for binary, 10 for decimal, 36 for alphanumeric, etc.) * @param enhancedEntropy If set to true, uses [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) for better cryptographic randomness (this also makes it take longer to generate) * @param randomCase If set to false, the generated ID will be lowercase only - also makes use of the `enhancedEntropy` parameter unless the output doesn't contain letters */ export declare function randomId(length?: number, radix?: number, enhancedEntropy?: boolean, randomCase?: boolean): string;