sabpaisa-encryption-package-gcm
v2.0.9
Published
AES-256-GCM encryption/decryption for browser and Node.js
Readme
sabpaisa-encryption-package-gcm
AES-256-GCM encryption/decryption with HMAC-SHA384 for Node.js, React (bundlers), and direct browser usage.
Installation
npm install sabpaisa-encryption-package-gcmUsage
1. Node.js
import { encrypt, decrypt } from "sabpaisa-encryption-package-gcm";
// or, for CommonJS:
// const { encrypt, decrypt } = require('sabpaisa-encryption-package-gcm');
const plaintext = "This is a secret message!";
const auth_key = "base64-encoded-32-byte-key==";
const auth_iv = "base64-encoded-32-byte-iv==";
// Encrypt
const encrypted = encrypt(plaintext, auth_key, auth_iv);
console.log("Encrypted:", encrypted);
// Decrypt
const decrypted = decrypt(encrypted, auth_key, auth_iv);
console.log("Decrypted:", decrypted);2. React / Bundlers (Webpack, Vite, etc.)
import { encrypt, decrypt } from "sabpaisa-encryption-package-gcm";
// Note: In the browser, these are async!
const plaintext = "This is a secret message!";
const auth_key = "base64-encoded-32-byte-key==";
const auth_iv = "base64-encoded-32-byte-iv==";
const encrypted = await encrypt(plaintext, auth_key, auth_iv);
console.log("Encrypted:", encrypted);
const decrypted = await decrypt(encrypted, auth_key, auth_iv);
console.log("Decrypted:", decrypted);- Use
awaitor.then()because encryption/decryption are async in the browser.
3. Direct Browser <script> Usage
- Include the browser global file (copy
src/encryption.browser.jsand remove theexportline, or use a dedicated UMD/global build):
<script src="path/to/encryption.browser.global.js"></script>
<script>
(async function () {
const plaintext = "This is a secret message!";
const auth_key = "base64-encoded-32-byte-key==";
const auth_iv = "base64-encoded-32-byte-iv==";
const encrypted = await sabpaisaEncryption.encrypt(
plaintext,
auth_key,
auth_iv,
);
console.log("Encrypted:", encrypted);
const decrypted = await sabpaisaEncryption.decrypt(
encrypted,
auth_key,
auth_iv,
);
console.log("Decrypted:", decrypted);
})();
</script>Key & IV Requirements
auth_keymust be a base64-encoded 32-byte (256-bit) key.auth_ivmust be a base64-encoded 32-byte value (only the first 12 bytes are used as IV).
Notes
- Node.js: Synchronous API.
- Browser/React: Asynchronous API (returns Promise).
- Direct
<script>: UsesabpaisaEncryption.encryptandsabpaisaEncryption.decrypton the global object.
License
MIT
Author
Abhishek Verma
LinkedIn
