Giter Club home page Giter Club logo

crypsi's Introduction

crypsi (Node Crypto Utility)

Custom crypto utility that wraps the crypto node module to make life easier

crypsi Node CI codecov Maintainability Known Vulnerabilities

Install

$ npm i crypsi

Usage

Just open the unit test folder, all available there.

Example Generate RSA Private and Public Key

const { rsa, keyUtil } = require('crypsi');
const fs = require('fs');

rsa.generateRSAkeyPair(keyUtil.KEY_SIZE_4KB, '').then(pairs => {
    console.log(pairs.publicKey);
    console.log(pairs.privateKey);

    const publicKeyWriter = fs.createWriteStream('public.key');
    publicKeyWriter.write(pairs.publicKey);
    
    const privateKeyWriter = fs.createWriteStream('private.key');
    privateKeyWriter.write(pairs.privateKey);

    publicKeyWriter.close();
    privateKeyWriter.close();
}).catch(err => {
    console.log(err);
});

Result RSA Public Key

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwIrVMXSXC5vxh+0fJny0
5neOzrb3kqvGOzLBgza4Emxj+MRLqzn1MtcSFobjwqlWoof8/5ycV0L74fhOywcX
n61vHwik/8As0HkyWK8yRd98T1Q5Z8U+ZYrl959h96Bi6il6y4IN+t7A8lfV2Rvz
UMEl9agfg0xNqPhEUFHCyDYzM7dW9cmSHyUhl+rp9RA/udNkv/k4ak7C4YmQEZyg
b9uEVlFY5Bpod5rZGm6roWqwZ54neDREuI4E7fWTnDXbYqif6/lNcBDvKW9s5oqx
YecNjMsrNrXQHkndoFENouzirQSITbxmwMAE5sJsU0RcFFP0yjsMtfSPSN48ubFH
mQIDAQAB
-----END PUBLIC KEY-----

Result RSA Private Key

-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDAitUxdJcLm/GH
7R8mfLTmd47OtveSq8Y7MsGDNrgSbGP4xEurOfUy1xIWhuPCqVaih/z/nJxXQvvh
+E7LBxefrW8fCKT/wCzQeTJYrzJF33xPVDlnxT5liuX3n2H3oGLqKXrLgg363sDy
V9XZG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5h+MJsI3t1pqdDJTGdYaN+ihAoGAcrj+
0aQ3xhO2qzMnRtCcSyUU7Iz0qocFWDy2OaVTwq63d+jvX44wrcmwy08ayfEqaSeR
K7km/c0PxoM/S2keZjNcc3vxDfDojCcdT5U9zSFxoLSgIEDtEOei1xwEiS8MDycy
9Av/W/gSbKmTSWOP314AUtmeR/RonpxrvIpb1kECgYAiRqS7MDwPVsktnzuT9ZWx
HDfktrdWAHO5+S2zG9gbCOgnhiOyIq4GakHgbdVlj2xsisOMGULFXEJBP3v8SzgF
7V2kM7+ThdnmQJaQC561871hb9gh5hOkf/GO9Gxe0cBOF4HmHO+QiQvylLdx/w8X
n3LQsISpln3R7g8riWFeWg==
-----END PRIVATE KEY-----

Example Loading RSA Private and Public Key

const { rsa } = require('crypsi');
const fs = require('fs');

// loading RSA private key from file
const privateKeyData = fs.readFileSync('./testdata/private.key');
const privateKey = rsa.loadPrivateKey(privateKeyData);

console.log(privateKey);

// loading RSA public key from file
const publicKeyData = fs.readFileSync('./testdata/public.key');
const publicKey = rsa.loadPublicKey(publicKeyData);

console.log(publicKey);

Example Loading RSA Private and Public Key as Base64 String

const { rsa } = require('crypsi');
const fs = require('fs');

// loading RSA private key from file
const privateKeyData = fs.readFileSync('./testdata/private.key');
const privateKeyBase64 = rsa.loadPrivateKeyAsBase64(privateKeyData);

console.log(privateKeyBase64);

// loading RSA public key from file
const publicKeyData = fs.readFileSync('./testdata/public.key');
const publicKeyBase64 = rsa.loadPublicKeyAsBase64(publicKeyData);

console.log(publicKeyBase64);

Example Loading RSA Private and Public Key as Base64 String and load back as RSA private and public key

const { rsa } = require('crypsi');
const fs = require('fs');

// loading RSA private key from file
const privateKeyData = fs.readFileSync('./testdata/private.key');
const privateKeyBase64 = rsa.loadPrivateKeyAsBase64(privateKeyData);

// for example: save privateKeyBase64 to database
const privateKey = rsa.loadPrivateKeyFromBase64(privateKeyBase64);
console.log(privateKey);

// loading RSA public key from file
const publicKeyData = fs.readFileSync('./testdata/public.key');
const publicKeyBase64 = rsa.loadPublicKeyAsBase64(publicKeyData);

// for example: save publicKeyBase64 to database

const publicKey = rsa.loadPublicKeyFromBase64(publicKeyBase64);
console.log(publicKey);

Example Digital Signature with RSA Private and Public Key

const { rsa, rsaSign, keyUtil } = require('crypsi');
const { Buffer } = require('buffer');
const fs = require('fs');

rsa.generateRSAkeyPair(keyUtil.KEY_SIZE_4KB, '').then(pairs => {
    console.log(pairs.publicKey);
    console.log(pairs.privateKey);

    // data can be anything, from simple string or Buffer of file
    const fileData = fs.readFileSync('./testdata/myfile.txt');

    // sign with private key
    const signature = rsaSign.signWithSha256(pairs.privateKey, Buffer.from(fileData));

     // => for example: save signature to database

    // verifying digital signature with public key
    const signatureValid = rsaSign.verifyWithSha256(pairs.publicKey, signature, Buffer.from(fileData));
    console.log(signatureValid);
}).catch(err => {
    console.log(err);
});

Example Encryption with RSA Private and Public Key

const { rsa, rsaEncryption, keyUtil } = require('crypsi');
const fs = require('fs');

rsa.generateRSAkeyPair(keyUtil.KEY_SIZE_4KB, '').then(pairs => {
    console.log(pairs.publicKey);
    console.log(pairs.privateKey);

    // data can be anything, from simple string or Buffer of file
    const fileData = fs.readFileSync('./testdata/myfile.txt');

    // encrypt with public key
    const encryptedData = rsaEncryption.encryptWithOaepSha256(pairs.publicKey, fileData);

    // decrypt with private key
    const decryptedData = rsaEncryption.decryptWithOaepSha256(pairs.privateKey, encryptedData);

    console.log(decryptedData);
}).catch(err => {
    console.log(err);
});

Example Generate Hash with common Digest Algorithm

const { digest } = require('crypsi');

const data = 'hello world';

// MD5
const generatedHashMd5 = digest.md5(data);
console.log(generatedHashMd5);

// SHA1
const generatedHashSha1 = digest.sha1(data);
console.log(generatedHashSha1);

// SHA256
const generatedHashSha256 = digest.sha256(data);
console.log(generatedHashSha256);

// SHA384
const generatedHashSha384 = digest.sha384(data);
console.log(generatedHashSha384);

// SHA384
const generatedHashSha512 = digest.sha512(data);
console.log(generatedHashSha512);

Example Generate Hash with HMAC

Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message (from Golang Documentation).

const { hmac } = require('crypsi');

const key = 'abc$#128djdyAgbjau&YAnmcbagryt5x';
const data = 'hello world';

// MD5
const generatedHmacMd5 = hmac.md5(key, data);
console.log(generatedHmacMd5);

// SHA1
const generatedHmacSha1 = hmac.sha1(key, data);
console.log(generatedHmacSha1);

// SHA256
const generatedHmacSha256 = hmac.sha256(key, data);
console.log(generatedHmacSha256);

// SHA384
const generatedHmacSha384 = hmac.sha384(key, data);
console.log(generatedHmacSha384);

// SHA384
const generatedHmacSha512 = hmac.sha512(key, data);
console.log(generatedHmacSha512);

Example Encryption with AES Algorithm

Expected key len:

  • AES 128: key length should be 16 bytes
  • AES 192: key length should be 24 bytes
  • AES 256: key length should be 32 bytes

Data encryption with AES 256 CBC

const { aesEncryption } = require('crypsi');

const key = 'abc$#128djdyAgbjau&YAnmcbagryt5x';
const data = 'hello world';

// encrypt data with AES 256 CBC
const encryptedData = aesEncryption.encryptWithAes256Cbc(key, data);
console.log(encryptedData);

// decrypt data with AES 256 CBC with the same key
const decryptedData = aesEncryption.decryptWithAes256Cbc(key, encryptedData);
console.log(decryptedData);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.