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

rabbit-cipher

v1.0.1

Published

rabbit 128bit cipher with poly1305 in javascript

Downloads

8

Readme

rabbit-cipher

rabbit 128bit cipher with poly1305

demo: https://angeal185.github.io/rabbit-cipher/

Installation

npm

$ npm install rabbit-cipher --save

bower

$ bower install rabbit-cipher

git

$ git clone [email protected]:angeal185/rabbit-cipher.git

nodejs


const rabbit = require('rabbit-cipher');

browser


<script src="./dist/rabbit.min.js"></script>

API


/* encrypt */

/**
 *  sync ~ encrypt data
 *  @param {string/byteArray/uint8Array} plain ~ data to be encrypted
 *  @param {string/byteArray/uint8Array} secret ~ encryption key
 *  @param {string} digest ~ encrypted data digest hex/bytes/binary/uint8/base64
 **/

rabbit.encSync(plain, secret, digest)



/**
 *  callback ~ encrypt data
 *  @param {string/byteArray/uint8Array} plain ~ data to be encrypted
 *  @param {string/byteArray/uint8Array} secret ~ encryption key
 *  @param {string} digest ~ encrypted data digest hex/bytes/binary/uint8/base64
 *  @param {function} cb ~ callback function(err,data)
 **/

rabbit.enc(plain, secret, digest, cb)


/**
 *  promise ~ encrypt data
 *  @param {string/byteArray/uint8Array} plain ~ data to be encrypted
 *  @param {string/byteArray/uint8Array} secret ~ encryption key
 *  @param {string} digest ~ encrypted data digest hex/bytes/binary/uint8/base64
 **/
rabbit.encP(plain, secret, digest)


/* decrypt */

/**
 *  sync  ~ decrypt data
 *  @param {string/byteArray/uint8Array} ctext ~ data to be decrypted
 *  @param {string/byteArray/uint8Array} key ~ encryption key
 *  @param {string} digest ~ encrypted data digest hex/bytes/binary/uint8/base64
 **/

rabbit.decSync(ctext, key, digest)


/**
 *  callback  ~ decrypt data
 *  @param {string/byteArray/uint8Array} ctext ~ data to be decrypted
 *  @param {string/byteArray/uint8Array} key ~ encryption key
 *  @param {string} digest ~ encrypted data digest hex/bytes/binary/uint8/base64
 *  @param {function} cb ~ callback function(err,data)
 **/

rabbit.dec(ctext, key, digest, cb)


/**
 *  promise  ~ decrypt data
 *  @param {string/byteArray/uint8Array} ctext ~ data to be encrypted
 *  @param {string/byteArray/uint8Array} key ~ encryption key
 *  @param {string} digest ~ encrypted data digest hex/bytes/binary/uint8/base64
 **/

rabbit.decP(ctext, key, digest)


/* encrypt and sign with poly1305 */

/**
 *  callback ~  encrypt and sign
 *  @param {string/byteArray/uint8Array} plain ~ data to encrypt
 *  @param {string/byteArray/uint8Array} key ~ encryption key
 *  @param {string/byteArray/uint8Array} skey ~ poly1305 key
 *  @param {string} digest ~ poly/data digest hex/bytes/binary/uint8/base64
 *  @param {function} cb ~ callback function(err,data)
 **/

rabbit.encPoly(plain, key, skey, digest, cb)


/**
 *  promise ~  encrypt and sign
 *  @param {string/byteArray/uint8Array} plain ~ data to encrypt
 *  @param {string/byteArray/uint8Array} key ~ encryption key
 *  @param {string/byteArray/uint8Array} skey ~ poly1305 key
 *  @param {string} digest ~ poly/data digest hex/bytes/binary/uint8/base64
 **/

rabbit.encPolyP(plain, hkey, hash, digest)


/* verify poly1305 and decrypt */

/**
 *  callback ~  verify and decrypt
 *  @param {string/byteArray/uint8Array} ctext ~ data to decrypt
 *  @param {string/byteArray/uint8Array} key ~ decrypt key
 *  @param {string/byteArray/uint8Array} sig1 ~ poly signature
 *  @param {string/byteArray/uint8Array} sig2 ~ ctext signature
 *  @param {string} digest ~ poly/data digest hex/bytes/binary/uint8/base64
 *  @param {function} cb ~ callback function(err,data)
 **/

rabbit.decPoly(ctext, key, sig1, sig2, digest, cb)


/**
 *  promise ~  verify and decrypt
 *  @param {string/byteArray/uint8Array} ctext ~ data to decrypt
 *  @param {string/byteArray/uint8Array} key ~ decrypt key
 *  @param {string/byteArray/uint8Array} sig1 ~ poly signature
 *  @param {string/byteArray/uint8Array} sig2 ~ ctext signature
 *  @param {string} digest ~ poly/data digest hex/bytes/binary/uint8/base64
 **/

rabbit.decPolyP(ctext, key, sig1, sig2, digest)



/* poly1305 */

/**
 *  callback ~ verify encrypted data
 *  @param {string/byteArray/uint8Array} ctext ~ cipher text
 *  @param {integer} len ~ cipher text length
 *  @param {string/byteArray/uint8Array} key ~ poly1305 key
 *  @param {string} digest ~ poly key digest hex/bytes/binary/uint8/base64
 *  @param {function} cb ~ callback function(err,data)
 **/

rabbit.poly1305.sign(ctext, len, key, digest, cb)


/**
 *  callback ~ verify encrypted data
 *  @param {string/byteArray/uint8Array} sig ~ first signature
 *  @param {string/byteArray/uint8Array} sig ~ second signature
 *  @param {string} digest ~ poly key digest hex/bytes/binary/uint8/base64
 *  @param {function} cb ~ callback function(err,data)
 **/

rabbit.poly1305.verify(sig1, sig2, digest, cb)


/**
 *  callback ~ verify encrypted data
 *  @param {string/byteArray/uint8Array} ctext ~ cipher text
 *  @param {integer} len ~ cipher text length
 *  @param {string/byteArray/uint8Array} key ~ poly1305 key
 *  @param {string} digest ~ poly key digest hex/bytes/binary/uint8/base64
 **/

rabbit.poly1305.signSync(ctext, len, key, digest)


/**
 *  sync ~ verify encrypted data
 *  @param {string/byteArray/uint8Array} sig ~ first signature
 *  @param {string/byteArray/uint8Array} sig ~ second signature
 *  @param {string} digest ~ poly key digest hex/bytes/binary/uint8/base64
 **/

rabbit.poly1305.verifySync(sig1, sig2, digest)



/* utils */

rabbit.utils.u82s(Uint8Array) // Uint8Array to string
rabbit.utils.s2u8(string) // string to Uint8Array
rabbit.utils.b2s(Uint8Array) // byteArray to string
rabbit.utils.s2b(string) // string to byteArray
rabbit.utils.a2b(byteArray) // byteArray to binary
rabbit.utils.b2a(byteArray) // binary to byteArray
rabbit.utils.u82a(uint8Array) // uint8Array to byteArray
rabbit.utils.h2u8(string) // hex to Uint8Array
rabbit.utils.u82h(Uint8Array) // uint8Array to hex
rabbit.utils.h2s(string) // hex to string
rabbit.utils.s2h(string) // string to hex
rabbit.utils.u8to16(Uint8Array) // Uint8Array to Uint16Array
rabbit.utils.u16to8(Uint16Array) //  Uint16Array to Uint8Array

rabbit.utils.secRand(string) // prng single
rabbit.utils.randomBytes(length) // prng byteArray

rabbit.utils.isUint8(i) // check Uint8Array
rabbit.utils.isArray(i) // check array
rabbit.utils.isString(i) // check string

// demo

const utils = rabbit.utils,
cl = console.log,
ce = console.error,
secret = 'secret',
skey = utils.randomBytes(32),
text = 'test',
digest = 'hex';

// enc/dec ~ sync
let sync = rabbit.encSync(text, secret, digest);
cl(sync)
sync = rabbit.decSync(sync, secret, digest)
cl(sync === text)



// enc/dec ~ callback
rabbit.enc(text, secret, digest, function(err, ctext){
  if(err){return ce(err)};
  rabbit.dec(ctext, secret, digest, function(err, plain){
    if(err){return ce(err)};
    cl(plain === text);
  });
});


// enc/dec ~ promise
rabbit.encP(text, secret, digest).then(function(ctext){
  rabbit.decP(ctext, secret, digest).then(function(plain){
      cl(plain === text);
  }).catch(function(err){
    ce(err)
  })
}).catch(function(err){
  ce(err)
})

// encrypt/decrypt with poly1305 ~ callback
rabbit.encPoly(text, secret, skey, digest, function(err, res){
  if(err){return ce(err)};

  let verify = rabbit.poly1305.signSync(res.ctext, res.ctext.length, skey, digest);

  rabbit.decPoly(res.ctext, secret, res.sig, verify, digest, function(err, plain){
    if(err){return ce(err)};
    cl(plain)
  });

});

// encrypt/decrypt with poly1305 ~ promise
rabbit.encPolyP(text, secret, skey, digest).then(function(res){
  let verify = rabbit.poly1305.signSync(res.ctext, res.ctext.length, skey, digest);
  rabbit.decPolyP(res.ctext, secret, res.sig, verify, digest).then(function(plain){
    cl(plain)
  }).catch(function(err){
    ce(err)
  })
}).catch(function(err){
  ce(err)
})


// poly1305
rabbit.enc(text, secret, digest, function(err, ctext){
  if(err){return ce(err)};

  // poly1305 ~ callback
  rabbit.poly1305.sign(ctext, ctext.length, skey, digest, function(err, sig){

    // poly1305 ~ sync
    let verify = rabbit.poly1305.signSync(ctext, ctext.length, skey, digest);

    rabbit.poly1305.verify(sig, verify, digest, function(err, ver){
      if(ver){
        rabbit.dec(ctext, secret, digest, function(err, plain){
          if(err){return ce(err)};
          cl(plain);
        });
      } else {
        ce('rabbit poly1305 authentication failure')
      }
    });

  })
});