UNPKG

js-crypto-key-utils

Version:

Universal Module for Cryptographic Key Utilities in JavaScript, including PEM-JWK converters

121 lines (120 loc) 4.72 kB
import { DER, OctetEC, PEM, KeyFormat, CurveTypes, KeyExportOptions, JwkThumbprintFormat, HashTypes } from './typedef'; /** * Key class to abstract public and private key objects in string or binary. * This class provides functions to interchangeably convert key formats, * and key objects will be used for the root package, js-crypto-utils, as inputs to exposed APIs. */ export declare class Key { private _type; private _jwk; private _der; private _oct; private _isEncrypted; private _current; /** * @constructor * @param {KeyFormat} format - Key format: 'jwk', 'der', 'pem' or 'oct' (only for ECC key). * @param {JsonWebKey|PEM|DER|OctetEC} key - Key object in the specified format. * @param {Object} [options={}] - Required if format='oct', and then it is {namedCurve: String}. * @throws {Error} - Throws if the input format and key are incompatible to the constructor. */ constructor(format: KeyFormat, key: JsonWebKey | PEM | DER | OctetEC, options?: { namedCurve?: CurveTypes; }); /** * Set a key in JWK to the Key object. * @param {JsonWebKey} jwkey - The Json Web Key. * @private */ private _setJwk; /** * Set a key in DER or PEM to the Key object. * @param {DER|PEM} asn1key - The DER key byte array or PEM key string. * @param {String} format - 'der' or 'pem' specifying the format. * @private */ private _setAsn1; /** * Set a key in SEC1 = Octet format to the Key Object. * @param {OctetEC} sec1key - The Octet SEC1 key byte array. * @param {CurveTypes} namedCurve - Name of curve like 'P-256'. * @private */ private _setSec1; /** * Set the current internal status. In particular, manage what the object is based on. * @private */ private _setCurrentStatus; /** * Convert the stored key and export the key in desired format. * Imported key must be basically decrypted except the case where the key is exported as-is. * @param {String} format - Intended format of exported key. 'jwk', 'pem', 'der' or 'oct' * @param {KeyExportOptions} [options={}] - Optional arguments. * @return {Promise<JsonWebKey|PEM|DER|OctetEC>} - Exported key object. */ export(format?: KeyFormat, options?: KeyExportOptions): Promise<JsonWebKey | PEM | DER | OctetEC>; /** * Encrypt stored key and set the encrypted key to this instance. * @param {String} passphrase - String passphrase. * @return {Promise<boolean>} - Always true otherwise thrown. * @throws {Error} - Throws if AlreadyEncrypted. */ encrypt(passphrase: string): Promise<boolean>; /** * Decrypted stored key and set the decrypted key in JWK to this instance. * @param {String} passphrase - String passphrase. * @return {Promise<boolean>} - Always true otherwise thrown. * @throws {Error} - Throws if NotEncrypted or FailedToDecrypt. */ decrypt(passphrase: string): Promise<boolean>; /** * Conpute JWK thumbprint specified in RFC7638 {@link https://tools.ietf.org/html/rfc7638}. * @param {HashTypes} [alg='SHA-256'] - Name of hash algorithm for thumbprint computation like 'SHA-256'. * @param {JwkThumbpirntFormat} [output='binary'] - Output format of JWK thumbprint. 'binary', 'hex' or 'base64'. * @return {Promise<Uint8Array|String>} - Computed thumbprint. * @throws {Error} - Throws if DecryptionRequired. */ getJwkThumbprint(alg?: HashTypes, output?: JwkThumbprintFormat): Promise<string | Uint8Array>; /** * Get keyType in JWK format * @return {Promise<String>} - 'RSA' or 'EC' * @throws {Error} - Throws if DecryptionRequired. */ get keyType(): Promise<string>; /** * Get jwkThumbprint of this key. * @return {Promise<Uint8Array>} - Returns binary thumbprint. */ get jwkThumbprint(): Promise<string | Uint8Array>; /** * Check if this is encrypted. * @return {boolean} */ get isEncrypted(): boolean; /** * Check if this is a private key. * @return {boolean} */ get isPrivate(): boolean; /** * Returns the key in DER format. * @return {Promise<DER>} */ get der(): Promise<OctetEC | JsonWebKey>; /** * Returns the key in PEM format. * @return {Promise<PEM>} */ get pem(): Promise<OctetEC | JsonWebKey>; /** * Returns the key in JWK format * @return {Promise<JsonWebKey>} */ get jwk(): Promise<OctetEC | JsonWebKey>; /** * Returns the 'EC' key in Octet SEC1 format. * @return {Promise<OctetEC>} */ get oct(): Promise<OctetEC | JsonWebKey>; }