@om-design/crypto
v2.3.0
Published
basic crypto base on openssl 3.3.1
Downloads
21
Maintainers
Readme
@om-design/crypto
a lightweight crypto library base on openssl 3.3.1 and emscripten 3.1.64. refactor some api from @linker-design/crypto.
features
- Symmetric encryption
- AES
- SM4
- Asymmetric encryption
- RSA
- SM2
- Message Digest
- Md5
- Md5Sha1
- Sha1
- Sha256
- Sha384
- Sha512
- SM3
usage
follow is some basic usage.
Symmetric encryption
import { Aes, SM4 } from '@om-design/crypto'
const testAesNeat = async () => {
const str = '一襟晚照淡风云,半船蝉音半梵心。江月吟尽古今事,白首偶趣三千经'
const key = await Aes.keyAsync(256)
const iv = await Aes.ivAsync(16)
const eRes = await Aes.encryptAsync(str, key, iv)
const dRes = await Aes.decryptAsync(eRes, key, iv)
console.log('aes-neat', str, dRes, str === dRes)
}
const testSM4Neat = async () => {
const str = '一襟晚照淡风云,半船蝉音半梵心。江月吟尽古今事,白首偶趣三千经'
const key = await SM4.keyAsync(256)
const iv = await SM4.ivAsync(16)
const eRes = await SM4.encryptAsync(str, key, iv)
const dRes = await SM4.decryptAsync(eRes, key, iv)
console.log('sm4-neat', str, dRes, str === dRes)
}
const testAes = async () => {
const str = '我心素以闲,清川澹若此';
const key = await Aes.keyAsync();
const iv = await Aes.ivAsync();
const eRes = new Array<Uint8Array>();
const enc = new Aes(key, iv, 'encrypt', 'CBC');
eRes.push(await enc.updateAsync(str));
eRes.push(await enc.finalAsync());
const dRes = new Array<Uint8Array>();
const dec = new Aes(key, iv, 'decrypt', 'CBC');
for (let data in eRes){
dRes.push(await dec.updateAsync(data));
}
dRes.push(await dec.finalAsync());
const dData = Aes.exportData(dRes, 'utf8');
console.log(str, dData, str === dData);
}
const testSM4 = async () => {
const str = '我心素以闲,清川澹若此';
const key = await Aes.keyAsync();
const iv = await Aes.ivAsync();
const eRes = new Array<Uint8Array>();
const enc = new SM4(key, iv, 'encrypt', 'CBC');
eRes.push(await enc.updateAsync(str));
eRes.push(await enc.finalAsync());
const eData = Aes.exportData(eRes, 'binary');
const dRes = new Array<Uint8Array>();
const dec = new SM4(key, iv, 'decrypt', 'CBC');
dRes.push(await dec.updateAsync(eData));
dRes.push(await dec.finalAsync());
const dData = Aes.exportData(dRes, 'utf8');
console.log(str, dData, str === dData);
}
(async () => {
await testAes();
await testSM4();
await testAesNeat();
await testSM4Neat();
})();
Asymmetric encryption
import { RsaKeyGenerator, KeyPair, Rsa, SM2KeyGenerator, SM2 } from '@om-design/crypto'
const testRsa = async () => {
const decoder = new TextDecoder();
const str = "我心素以闲,清川澹若此";
const generator = new RsaKeyGenerator(4096, 2);
const keyPair = await generator.generateAsync();
const publicKey = await keyPair.exportPublicAsync('PEM');
const privateKey = await keyPair.exportPrivateAsync('PEM');
const pubKey = await KeyPair.importPublicAsync(publicKey, 'PEM');
const priKey = await KeyPair.importPrivateAsync(privateKey,'PEM');
const enc = new Rsa('encrypt', pubKey, "oaep");
const dec = new Rsa('decrypt', priKey, "oaep");
const ctext = await enc.updateAsync(str);
const rtext = await dec.updateAsync(ctext);
const ctxt = decoder.decode(rtext);
generator.dispose();
keyPair.dispose();
pubKey.dispose();
priKey.dispose();
enc.dispose();
dec.dispose();
console.log('rsa', str, ctxt, str === ctxt);
}
const testSM2 = async () => {
const decoder = new TextDecoder()
const str = '一襟晚照淡风云,半船蝉音半梵心。江月吟尽古今事,白首偶趣三千经'
const generator = new SM2KeyGenerator()
const keyPair = await generator.generateAsync()
const publicKey = await keyPair.exportPublicAsync('PEM', 'SM2')
const privateKey = await keyPair.exportPrivateAsync('PEM', 'SM2')
const pubKey = await KeyPair.importPublicAsync(publicKey, 'PEM', 'SM2')
const priKey = await KeyPair.importPrivateAsync(privateKey, 'PEM', 'SM2')
const enc = new SM2('encrypt', pubKey, 'oaep')
const dec = new SM2('decrypt', priKey, 'oaep')
const ctext = await enc.updateAsync(str)
const rtext = await dec.updateAsync(ctext)
const ctxt = decoder.decode(rtext)
generator.dispose()
keyPair.dispose()
pubKey.dispose()
priKey.dispose()
enc.dispose()
dec.dispose()
console.log('sm2', str, ctxt, str === ctxt)
}
(async () => {
await testRsa();
await testSM2();
})()
Message Digest
import { Md5, SM3 } from '@om-design/crypto';
(async () => {
const str = '我心素以闲,清川澹若此';
let hash = await Md5.digestAsync(str);
console.log(hash);
hash = await SM3.digestAsync(str);
console.log(hash);
})();