@justinwwolcott/ez-web-crypto
Version:
class for working with webcrypto in browser and node
76 lines (65 loc) • 4.16 kB
TypeScript
type Base64String = string;
type HexString = string;
interface CryptoContext {
crypto: Crypto;
CryptoKey: typeof CryptoKey | null;
}
interface AESEncryptResult {
ciphertext: Base64String;
iv: Base64String;
}
interface AESDecryptResult {
data: ArrayBuffer;
text?: string;
}
interface PasswordEncryptResult {
ciphertext: Base64String;
aes: Base64String;
}
interface ECKeyPairExportable {
publicKey: Base64String;
privateKey: Base64String;
jwkPublicKey: JsonWebKey;
jwkPrivateKey: JsonWebKey;
rawPublicKey: Base64String;
rawPublicKeyLite: Base64String;
}
interface ECKeyPairNonExportable {
publicKey: Base64String;
privateKey: CryptoKey;
rawPublicKey: Base64String;
rawPublicKeyLite: Base64String;
}
type ECKeyPair = ECKeyPairExportable | ECKeyPairNonExportable;
type HashAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';
interface HKDFEncryptResult {
ciphertext: Base64String;
salt: Base64String;
iv: Base64String;
}
interface ECSignatureKeyPair {
publicKey: Base64String;
privateKey: Base64String | CryptoKey;
}
declare const base64ToArray: (strng: Base64String) => Uint8Array;
declare const arrayToBase64: (utf8Bytes: Uint8Array) => Base64String;
declare const sleep: (duration: number) => Promise<void>;
declare const HMAC: (secret: string, data: string) => Promise<HexString>;
declare const HASH: (algo: HashAlgorithm, data: string, len?: number) => Promise<Base64String>;
declare const AESMakeKey: (exportable?: boolean) => Promise<Base64String | CryptoKey>;
declare const AESImportKey: (aes_key: Base64String | CryptoKey, exportable?: boolean) => Promise<CryptoKey>;
declare const AESEncrypt: (base_64_key: Base64String | CryptoKey, base_64_data: Base64String, base_64_nonce?: Base64String | false) => Promise<AESEncryptResult>;
declare const AESDecrypt: (base_64_key: Base64String | CryptoKey, base_64_nonce: Base64String, base_64_cipher: Base64String, returnText?: boolean) => Promise<string | ArrayBuffer>;
declare const PASSWORD_ENCRYPT: (password: string, base64data: Base64String) => Promise<Base64String>;
declare const PASSWORD_DECRYPT: (password: string, base64data: Base64String) => Promise<string>;
declare const EcMakeCryptKeys: (exportable?: boolean) => Promise<ECKeyPair>;
declare const EcEncrypt: (b64Private: Base64String | CryptoKey, b64Public: Base64String | CryptoKey, b64data: Base64String) => Promise<AESEncryptResult>;
declare const EcDecrypt: (b64Private: Base64String | CryptoKey, b64Public: Base64String | CryptoKey, b64Nonce: Base64String, b64data: Base64String, returnText?: boolean) => Promise<string | ArrayBuffer>;
declare const HKDFEncrypt: (b64Private: Base64String | CryptoKey, b64Public: Base64String | CryptoKey, b64data: Base64String, ivLength?: number, saltLength?: number) => Promise<HKDFEncryptResult>;
declare const HKDFDecrypt: (b64Private: Base64String | CryptoKey, b64Public: Base64String | CryptoKey, b64Salt: Base64String, b64iv: Base64String, b64data: Base64String, returnText?: boolean) => Promise<string | ArrayBuffer>;
declare const EcMakeSigKeys: (exportable?: boolean) => Promise<ECSignatureKeyPair>;
declare const EcSignData: (b64PrivateKey: Base64String | CryptoKey, b64data: Base64String) => Promise<Base64String>;
declare const EcVerifySig: (b64PublicKey: Base64String | CryptoKey, b64Signature: Base64String, b64data: Base64String) => Promise<boolean>;
declare const EcdhConvertKey: (unknown_key: Base64String | CryptoKey) => Promise<CryptoKey>;
declare const EcdsaConvertKey: (unknown_key: Base64String | CryptoKey) => Promise<CryptoKey>;
export { AESDecrypt, type AESDecryptResult, AESEncrypt, type AESEncryptResult, AESImportKey, AESMakeKey, type Base64String, type CryptoContext, type ECKeyPair, type ECKeyPairExportable, type ECKeyPairNonExportable, type ECSignatureKeyPair, EcDecrypt, EcEncrypt, EcMakeCryptKeys, EcMakeSigKeys, EcSignData, EcVerifySig, EcdhConvertKey, EcdsaConvertKey, HASH, HKDFDecrypt, HKDFEncrypt, type HKDFEncryptResult, HMAC, type HashAlgorithm, type HexString, PASSWORD_DECRYPT, PASSWORD_ENCRYPT, type PasswordEncryptResult, arrayToBase64, base64ToArray, sleep };