WebEncryption is a JavaScript/TypeScript library designed for cryptographic operations, including AES encryption, RSA encryption, ECDSA key pair generation, HMAC signature generation, and hashing. This library leverages the Web Crypto API to provide secure and performant encryption functionalities.
Using npm:
npm install web-secure-encryption
Using yarn:
yarn add web-secure-encryption
Generates an AES-GCM encryption key.
generateAESKey(keySize: number = 256): Promise<string>
- keySize: AES key size (128, 192, 256)
- Returns: Base64-encoded AES Key.
Encrypts a string using AES-GCM.
encryptAES(data: string, keyBase64: string): Promise<string>
- data: Plaintext to encrypt.
- keyBase64: Base64-encoded AES key.
- Returns: Base64-encoded IV + Ciphertext.
Decrypts a Base64-encoded ciphertext using AES-GCM.
decryptAES(encryptedData: string, keyBase64: string): Promise<string>
- encryptedData: Base64-encoded ciphertext.
- keyBase64: Base64-encoded AES key.
- Returns: Decrypted plaintext.
Generates a SHA-256 hash.
hashSHA256(input: string): Promise<string>
- input: String to hash.
- Returns: Base64-encoded SHA-256 hash.
Generates a SHA-512 hash.
hashSHA512(input: string): Promise<string>
- input: String to hash.
- Returns: Base64-encoded SHA-512 hash.
Generates an HMAC key.
generateHMACKey(keySize: number): Promise<string>
- keySize: Size of the key (256 or 512 bits).
- Returns: Base64-encoded HMAC key.
Generates an HMAC-SHA256 signature.
hmacSHA256(data: string, key: string): Promise<string>
- data: Data to sign.
- key: Base64-encoded HMAC key.
- Returns: Base64-encoded HMAC signature.
Generates an HMAC-SHA512 signature.
hmacSHA512(data: string, key: string): Promise<string>
- data: Data to sign.
- key: Base64-encoded HMAC key.
- Returns: Base64-encoded HMAC signature.
Generates an RSA Key Pair.
generateRSAKeyPair(): Promise<Keypair>
- Returns: Object containing Base64-encoded public and private keys.
Encrypts data using an RSA public key.
encryptRSA(data: string, publicKeyBase64: string): Promise<string>
- data: Data to encrypt.
- publicKeyBase64: Base64-encoded RSA public key.
- Returns: Base64-encoded encrypted data.
Decrypts data using an RSA private key.
decryptRSA(data: string, privateKeyBase64: string): Promise<string>
- data: Encrypted data.
- privateKeyBase64: Base64-encoded RSA private key.
- Returns: Decrypted plaintext.
Imports an RSA public key.
importRSAPublicKey(publicKeyBase64: string): Promise<CryptoKey>
- publicKeyBase64: Base64-encoded RSA public key.
- Returns: Imported RSA public key.
Imports an RSA private key.
importRSAPrivateKey(privateKeyBase64: string): Promise<CryptoKey>
- privateKeyBase64: Base64-encoded RSA private key.
- Returns: Imported RSA private key.
Generates an ECDSA Key Pair.
generateECDSAKeyPair(): Promise<Keypair>
- Returns: Object containing Base64-encoded public and private keys.
Extracts the public key from a private ECDSA key.
getPublicECDSAKey(privateKeyBase64: string): Promise<string>
- privateKeyBase64: Base64-encoded private key.
- Returns: Base64-encoded public key.
Signs data using an ECDSA private key.
signDataECDSA(data: string, privateKeyBase64: string): Promise<string>
- data: Data to sign.
- privateKeyBase64: Base64-encoded private key.
- Returns: Base64-encoded signature.
Verifies an ECDSA signature.
verifySignatureECDSA(data: string, signatureBase64: string, publicKeyBase64: string): Promise<boolean>
- data: Original data.
- signatureBase64: Base64-encoded signature.
- publicKeyBase64: Base64-encoded public key.
- Returns:
true
if the signature is valid.
Generates a random string of a specified length.
generateRandomString(length: number): string
- length: Length of the random string.
- Returns: Random string.
Encodes a string in Base64.
base64Encode(input: string): string
- input: String to encode.
- Returns: Base64-encoded string.
Decodes a Base64-encoded string.
base64Decode(input: string): string
- input: Base64-encoded string.
- Returns: Decoded string.
MIT