Authenticated encryption
@webbuf/acs2
AES+CBC encryption/decryption with SHA-256 HMAC
Install
npm install @webbuf/acs2 Usage
import { acs2Encrypt, acs2Decrypt } from "@webbuf/acs2";
import { WebBuf } from "@webbuf/webbuf";
import { FixedBuf } from "@webbuf/fixedbuf";
// 256-bit key
const key = FixedBuf.fromRandom<32>(32);
// Encrypt with authentication
const plaintext = WebBuf.fromUtf8("Secret message");
const ciphertext = acs2Encrypt(plaintext, key);
// Decrypt and verify
try {
const decrypted = acs2Decrypt(ciphertext, key);
console.log(decrypted.toUtf8()); // "Secret message"
} catch (e) {
console.error("Authentication failed!");
} API reference (2 exports)
Functions
acs2Decrypt
functionDecrypt data with AES + CBC mode and a SHA-256 HMAC. ACS2 = AES + CBC + SHA256 Hmac
acs2Decrypt(ciphertext: WebBuf, aesKey: FixedBuf<32>): WebBuf acs2Encrypt
functionEncrypt data with AES + CBC mode and a SHA-256 HMAC. Good for small amounts of data, such as a short text message. ACS2 = AES + CBC + SHA256 Hmac
acs2Encrypt(plaintext: WebBuf, aesKey: FixedBuf<32>, iv?: FixedBuf<16>): WebBuf