npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@om-design/crypto

v2.3.0

Published

basic crypto base on openssl 3.3.1

Downloads

190

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
    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

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);
})();