@linker-design/crypto
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

@LinkerDesign/Crypto

A lightweight crypto library which could run on browser, node, java(jvm), dotnet platform base on openssl libcrypto library.

  • browser, node package

    • we use webassembly technology to port and encapsulate the native c library(openssl libcrypto). you could access the package here
  • dotnet package

    • we use p/invoke technology to port and encapsulate the native c library(openssl libcrypto) . you could access the package here
  • java(jvm) package

    • we use jni(java native interface) technology to port and encapsulate the native c library(openssl libcrypto). you could access the package here

Features

  1. Symmetric Encryt Decrypt
  • AES-CBC
  • AES-CTR
  1. Message Digest
  • Sha1
  • Sha256
  • Sha384
  • Sha512
  • Md5
  • Md5Sha1
  1. Big file(like 100G) message digest Support

more features will come soon!

Setup

npm i -S @linker-design/crypto

usages

  1. AES Uint8Array or Blob

We can use aes-cbc to encrypt or decrypt an arraybuffer

import { AES } from `@linker-design/crypto`;

const aes = new AES();

// key is a base64 string which can be geted from backend server or generate by aes
const key = await aes.generateKey();

// iv is a base64 string which can geted from backend server or generate by aes
const iv = await aes.generateIV();

// clearText is an arraybuffer which can be readed from a file or network
const file = new Uint8Array([1,2,3,5]);

// cipher is the encrypted arraybuffer
const cipher = await aes.encrypt(key, iv, file);

// after all, we can decrypt the cipher with the same key, iv;
// as a result, file1 has the same content of file.
const file1 = await aes.decrypt(key, iv, cipher);
  1. AES String

We can use AES-CBC to encryt a string who's encoding is utf8 to a base64 string.

Additional, we can AES-CBC to decrypt a base64 string to it's original utf8 string

import { AES } from `@linker-design/crypto`;

const aes = new AES();

// key is a base64 string which can be geted from backend server or generate by aes
const key = await aes.generateKey();

// iv is a base64 string which can geted from backend server or generate by aes
const iv = await aes.generateIV();

// data is a utf8 string
const text = "hello world";

// cipher is a base64 string
const cipher = await aes.encrypt(key, iv, text, 'utf8');

// we can decrypt the cipher like this.
// after that, text1 is the same as text;
const text1 = await aes.decrypt(key, iv, cipher, undefined, 'utf8');
  1. Message Digest

support regular message digest algorith, like sha1, sha256, md5 etc...

import { Sha1, Sha256, Sha384, Sha512, Md5, Md5Sha1 } from '@linker-design/crypto';

const msg = '《青溪》是唐代诗人王维创作的一首五言古诗。此诗描写了一条青溪的幽秀景色,诗人用多彩的画笔,绘出青溪流经不同地方时呈现的不同画面。其中“声喧乱石中,色静深松里”两句,以喧响的声音和幽冷的色调形成闹与静的强烈对比,如同一幅“有声画”。诗的末四句写出诗人心境的闲谈正如清川的闲淡,把自己的精神和自然的精神融和起来,意味隽永。全诗自然清淡素雅,写景抒情皆轻轻松松,然而韵味却隽永醇厚。诗人笔下的青溪是喧闹与沉郁的统一,活泼与安详的揉合,幽深与素静的融和。';

// sha1
const sha1 = new Sha1();
let hash = await sha1.digest(msg, 'utf8', 'base64');
console.log(hash);

// sha256
const sha256 = new Sha256();
hash = await sha256.digest(msg, 'utf8', 'base64');
console.log(hash);

// sha384
const sha384 = new Sha384();
hash = await sha384.digest(msg, 'utf8', 'base64');
console.log(hash);

// sha512
const sha512 = new Sha512();
hash = await sha512.digest(msg, 'utf8', 'base64');
console.log(hash);

// md5
const md5 = new Md5();
hash = await md5.digest(msg, 'utf8', 'base64');
console.log(hash);

// md5sha1
const md5Sha1 = new Md5Sha1();
hash = await md5Sha1.digest(msg, 'utf8', 'base64');
console.log(hash);

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i @linker-design/crypto

      Weekly Downloads

      50

      Version

      0.3.1

      License

      MIT

      Unpacked Size

      4.03 MB

      Total Files

      40

      Last publish

      Collaborators

      • linker-design