@li0ard/sm4
SM4 (GB/T 32907-2016) cipher implementation in pure TypeScript
docs
# from NPM
npm i @li0ard/sm4
# from JSR
bunx jsr i @li0ard/sm4- Electronic Codebook (ECB)
- Cipher Block Chaining (CBC)
- Output Feedback (OFB)
- Counter (CTR)
- Cipher Feedback (CFB)
- Galois/Counter Mode (GCM)
- Provides simple and modern API
- Most of the APIs are strictly typed
- Fully complies with GB/T 32907-2016 standard
- Supports Bun, Node.js, Deno, Browsers
import { encryptECB, decryptECB } from "@li0ard/sm4";
const key = hexToBytes("0123456789ABCDEFFEDCBA9876543210");
const plaintext = hexToBytes("AAAA....AA");
const ciphertext = encryptECB(key, plaintext);
console.log(ciphertext);
console.log(decryptECB(key, ciphertext));import { encryptCBC, decryptCBC } from "@li0ard/sm4";
const key = hexToBytes("0123456789ABCDEFFEDCBA9876543210");
const key = hexToBytes("000102030405060708090A0B0C0D0E0F");
const plaintext = hexToBytes("AAAA....AA");
const ciphertext = encryptCBC(key, plaintext, iv);
console.log(ciphertext);
console.log(decryptCBC(key, ciphertext, iv));