npm package: @starryn/crypto
Description: basic crypto base on openssl 3.3.2
Author: om design team
Latest version: 1.0.0

# @starryn/crypto
  a lightweight crypto library base on [openssl 3.3.2](https://openssl-library.org/) and [emscripten 3.1.64](https://emscripten.org/). refactor some api from [@linker-design/crypto](https://www.npmjs.com/package/@linker-design/crypto). 

## features
  + Symmetric encryption
    1. AES
    2. SM4
  + Asymmetric encryption
    1. RSA
    2. SM2
  + Message Digest
    1. Md5
    2. Md5Sha1
    3. Sha1
    4. Sha256
    5. Sha384
    6. Sha512
    7. SM3

## usage
  follow is some basic usage.

### Symmetric encryption

```typescript
import { Aes, SM4 } from '@starryn/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

```typescript
import { RsaKeyGenerator, KeyPair, Rsa, SM2KeyGenerator, SM2 } from '@starryn/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

```typescript
import { Md5, SM3 } from '@starryn/crypto';

(async () => {
  const str = '我心素以闲，清川澹若此';
  let hash = await Md5.digestAsync(str);
  console.log(hash);

  hash = await SM3.digestAsync(str);
  console.log(hash);
})();

```