WebEncryption Library Documentation
📚 Introduction
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.
🚀 Installation
Using npm:
npm install web-secure-encryption
Using yarn:
yarn add web-secure-encryption
🛠️ Methods Overview
✅ AES Encryption and Decryption
generateAESKey
Generates an AES-GCM encryption key.
generateAESKey(keySize: number = 256): Promise<string>
- keySize: AES key size (128, 192, 256)
- Returns: Base64-encoded AES Key.
encryptAES
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.
decryptAES
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.
✅ Hashing
hashSHA256
Generates a SHA-256 hash.
hashSHA256(input: string): Promise<string>
- input: String to hash.
- Returns: Base64-encoded SHA-256 hash.
hashSHA512
Generates a SHA-512 hash.
hashSHA512(input: string): Promise<string>
- input: String to hash.
- Returns: Base64-encoded SHA-512 hash.
✅ HMAC Signatures
generateHMACKey
Generates an HMAC key.
generateHMACKey(keySize: number): Promise<string>
- keySize: Size of the key (256 or 512 bits).
- Returns: Base64-encoded HMAC key.
hmacSHA256
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.
hmacSHA512
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.
✅ RSA Encryption and Decryption
generateRSAKeyPair
Generates an RSA Key Pair.
generateRSAKeyPair(): Promise<Keypair>
- Returns: Object containing Base64-encoded public and private keys.
encryptRSA
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.
decryptRSA
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.
importRSAPublicKey
Imports an RSA public key.
importRSAPublicKey(publicKeyBase64: string): Promise<CryptoKey>
- publicKeyBase64: Base64-encoded RSA public key.
- Returns: Imported RSA public key.
importRSAPrivateKey
Imports an RSA private key.
importRSAPrivateKey(privateKeyBase64: string): Promise<CryptoKey>
- privateKeyBase64: Base64-encoded RSA private key.
- Returns: Imported RSA private key.
✅ ECDSA (Elliptic Curve Digital Signature Algorithm)
generateECDSAKeyPair
Generates an ECDSA Key Pair.
generateECDSAKeyPair(): Promise<Keypair>
- Returns: Object containing Base64-encoded public and private keys.
getPublicECDSAKey
Extracts the public key from a private ECDSA key.
getPublicECDSAKey(privateKeyBase64: string): Promise<string>
- privateKeyBase64: Base64-encoded private key.
- Returns: Base64-encoded public key.
signDataECDSA
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.
verifySignatureECDSA
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.
✅ Utilities
generateRandomString
Generates a random string of a specified length.
generateRandomString(length: number): string
- length: Length of the random string.
- Returns: Random string.
base64Encode
Encodes a string in Base64.
base64Encode(input: string): string
- input: String to encode.
- Returns: Base64-encoded string.
base64Decode
Decodes a Base64-encoded string.
base64Decode(input: string): string
- input: Base64-encoded string.
- Returns: Decoded string.
📝 License
MIT