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

lek-cryptools

v3.0.0

Published

'ctyptographic tools'

Downloads

258

Readme

lek-cryptools

lek-cryptools is a lightweight cryptographic utility package for Node.js, providing easy-to-use functions for generating unique keys, hashing, and encrypting/decrypting data.

Installation

You can install lek-cryptools using npm:

npm install lek-cryptools

Usage

First, require the package in your Node.js application:

const lekCryptoTools = require('lek-cryptools');

Available Functions

The exported methods are as follows:

  • getUniqueKey
  • getUniqueKeySync
  • cipher
  • cipherSync
  • decipher
  • decipherSync
  • encrypt
  • encryptSync
  • compare
  • compareSync
  • cipherStream
  • decipherStream

Many have a sync version. The truth is that the processes are usually very fast and it is not necessary to use the asynchronous view... however it can be useful for encrypting or decrypting large chunks of data in parallel or similar situations. I recommend using the Sync version for most cases.

Versions before 2.0.0 had almost only synchronous versions (except encrypt) so to migrate to later versions you should simply add sync to the method used.

cipher => cipherSync

getUniqueKey(num = 64)

Generates a unique key or ID.

const uniqueKey = await lekCryptoTools.getUniqueKey();
console.log(uniqueKey); // Outputs a 128-character hexadecimal string

or

const uniqueKey = lekCryptoTools.getUniqueKeySync();
console.log(uniqueKey); // Outputs a 128-character hexadecimal string

encrypt(data, num = 10)

Hashes a string using bcrypt.

const hashedPassword = await lekCryptoTools.encrypt('myPassword');
console.log(hashedPassword);

or

const hashedPassword = lekCryptoTools.encryptSync('myPassword');
console.log(hashedPassword);

compare(data, encrypted)

Compares a plain text string with a hashed string.

const isMatch = await lekCryptoTools.compare('myPassword', hashedPassword);
console.log(isMatch); // Outputs: true or false

or

const isMatch = lekCryptoTools.compareSync('myPassword', hashedPassword);
console.log(isMatch); // Outputs: true or false

cipher(data, secretKey)

Encrypts a string or buffer.

const encryptedData = await lekCryptoTools.cipher('sensitive data', 'mySecretKey');
console.log(encryptedData);

or

const encryptedData = lekCryptoTools.cipherSync('sensitive data', 'mySecretKey');
console.log(encryptedData);

decipher(encrypted, secretKey)

Decrypts previously encrypted data.

const decryptedData = await lekCryptoTools.decipher(encryptedData, 'mySecretKey');
console.log(decryptedData); // Outputs: 'sensitive data'

or

const decryptedData = lekCryptoTools.decipherSync(encryptedData, 'mySecretKey');
console.log(decryptedData); // Outputs: 'sensitive data'

In both methods above. If you give a string as the first parameter, a string will be returned. Otherwise, if you enter a buffer, a buffer will be returned.

3.0.0

This new version includes two new features.

cipherStream

This function receives three parameters, stream input and output and the secret key.

will pass through the stream encrypting the data bit by bit. You can wait for the promise or capture the event finish of the stream output. If you wait for the promise, note that it does not return anything since the essence of the stream is not to fill the memory.

const inputFile = fs.createReadStream('a-file.abc');
const outputFile = fs.createWriteStream('ciphred-file.enc');

lekCryptoTools.cipherStream(inputFile, outputFile, "secret-key");

decipherStream

This function receives an input and output stream in the same way and decrypts it bit by bit.

You can either encrypt with cipher and decrypt with decipherStream or encrypt with cipherStream and decrypt with decipherStream without problems.

const inputFile = fs.createReadStream('ciphred-file.enc');
const outputFile = fs.createWriteStream('a-file.abc');

inputFile.on('open', () =>
{
    lekCryptoTools.decipherStream(inputDecFile, outputDecFile, key);
});

LekCryptoolsError

Finally... LekCryptoolsError is a class. It is useful if you catch an error and want to know if it comes from here. Especially DECIPHER which is an error that usually has to do with the user setting the wrong decryption key.


try
{
    const result = await lekCryptoTools.decipher('the-cipher-data', 'incorrect-key');
}
catch(err)
{
    if(err instanceof lekCryptoTools.LekCryptoolsError)
    {
        console.error('the key is incorrect');
    }
}

Security Note

This package uses standard cryptographic libraries, but remember that the security of your application depends on how you manage your secret keys and sensitive data. Always follow best practices for key management and never expose your secret keys.

License

[ISC]

Contributing

Contributions, issues, and feature requests are welcome. Feel free to check issues page if you want to contribute.

Author

Your Name - Gabriel Farías

Acknowledgments