@li0ard/sm4
v0.1.0
Published
SM4 cipher implementation in pure TypeScript
Readme
Installation
# from NPM
npm i @li0ard/sm4
# from JSR
bunx jsr i @li0ard/sm4Supported modes
- [x] Electronic Codebook (ECB)
- [x] Cipher Block Chaining (CBC)
- [x] Output Feedback (OFB)
- [x] Counter (CTR)
- [x] Cipher Feedback (CFB)
- [x] Galois/Counter Mode (GCM)
Features
- 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
Examples
ECB mode
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));CBC mode
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));