UNPKG

3.15 kBTypeScriptView Raw
1type SHA = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';
2type AES = 'AES-CTR' | 'AES-CBC' | 'AES-GCM' | 'AES-KW';
3type NIST = 'P-256' | 'P-384' | 'P-521';
4
5export function digest(algorithm: SHA, message: string): Promise<string>;
6
7export function SHA1(message: string): Promise<string>;
8export function SHA256(message: string): Promise<string>;
9export function SHA384(message: string): Promise<string>;
10export function SHA512(message: string): Promise<string>;
11
12export namespace Algorithms {
13 type Digest = SHA;
14
15 type Keying =
16 | { name: 'RSASSA-PKCS1-v1_5'; hash: SHA }
17 | { name: 'RSA-OAEP'; hash: SHA }
18 | { name: 'RSA-PSS'; hash: SHA }
19 | { name: 'ECDSA'; namedCurve: NIST }
20 | { name: 'ECDH'; namedCurve: NIST }
21 | { name: 'HMAC'; hash: SHA; length?: number }
22 | { name: AES } | AES | 'PBKDF2' | 'HKDF';
23
24 type Signing =
25 | { name: 'RSASSA-PKCS1-v1_5' } | 'RSASSA-PKCS1-v1_5'
26 | { name: 'RSA-PSS'; saltLength: number }
27 | { name: 'ECDSA'; hash: SHA }
28 | { name: 'HMAC' } | 'HMAC';
29}
30
31/**
32 * Generate a `CryptoKey` based on the raw `secret` value.
33 * @NOTE Method wraps `crypto.subtle.importKey` internally.
34 */
35export function keyload(algo: Algorithms.Keying, secret: string, scopes: KeyUsage[]): Promise<CryptoKey>;
36
37/**
38 * Generate a `CryptoKey` without a base value.
39 * @NOTE Method wraps `crypto.subtle.generateKey` internally.
40 */
41export function keygen(algo: Algorithms.Keying, scopes: KeyUsage[], extractable?: boolean): Promise<CryptoKey|CryptoKeyPair>;
42
43/**
44 * Generate a digital signature of a `payload` using the specified algorithm and cryptokey.
45 */
46export function sign(algo: Algorithms.Signing, key: CryptoKey, payload: string): Promise<ArrayBuffer>;
47
48/**
49 * Verify the digital signature of a `payload` using the specified algorithm and cryptokey.
50 */
51export function verify(algo: Algorithms.Signing, key: CryptoKey, payload: string, signature: ArrayBuffer): Promise<boolean>;
52
53/**
54 * Convenient alias for all TypedArray classes
55 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
56 */
57export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
58
59/**
60 * A constant-time check if `a` and `b` are equal.
61 * Does not leak timing information, which would allow an attacker to guess one of the values.
62 * @see https://nodejs.org/dist/latest-v14.x/docs/api/crypto.html#crypto_crypto_timingsafeequal_a_b
63 */
64export function timingSafeEqual<T extends TypedArray>(a: T, b: T): boolean;
65
66/**
67 * Apply the PBKDF2 function to an input using a salt and derivation parameters.
68 * @see https://en.wikipedia.org/wiki/PBKDF2
69 * @param {SHA} digest The hashing function to use.
70 * @param {string} password The input value to protect.
71 * @param {string} salt The cryptographic salt.
72 * @param {number} iters The number of hashing iterations.
73 * @param {number} length The desired number of bits to derive.
74 */
75export function PBKDF2(digest: Algorithms.Digest, password: string, salt: string, iters: number, length: number): Promise<ArrayBuffer>;