Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@bpe/one-time-pad

BenEdridge157ISC1.0.2

A more complete One-time pad implementation supporting Strings, Buffers and Files

one, time, pad, one-time, OTP, cipher, crypto, cryptography

readme

One-Time Pad

A simple and more complete One-Time Pad implementation in NodeJS

npm (scoped) GitHub issues Snyk Vulnerabilities for npm package npm bundle size NPM

  • No limitations on length of data or type of data encrypted
  • Supports any data type as long as you convert it to a Buffer
  • No dependencies required

Installation

npm install @bpe/one-time-pad

Usage

Strings

const { OneTimePad } = require('one-time-pad');

const plainText = 'Hello World!';
const plainTextBuffer = Buffer.from(plainText, 'utf8')

// Secure Pad the length of plainText (Keep Secure!)
const pad = OneTimePad.generatePad(plainTextBuffer);

const encryptedData = OneTimePad.encrypt(pad, plainTextBuffer);
console.log(`${Buffer.from(encryptedData).toString('base64')}`);

const decryptedData = OneTimePad.decrypt(pad, encryptedData);
console.log(`Decrypted Data: ${Buffer.from(decryptedData).toString('utf8')}`);

Binary Data

const { OneTimePad } = require('one-time-pad');
const fs = require('fs');

const pngBuffer = new Uint8Array(fs.readFileSync('./tests/test_image.png'));
const pad = OneTimePad.generatePad(pngBuffer);

fs.writeFileSync('./tests/scratch/encrypted.png', OneTimePad.encrypt(pad, pngBuffer));

const encryptedPng = new Uint8Array(fs.readFileSync('./tests/scratch/encrypted.png'));
const decryptedData = OneTimePad.decrypt(pad, encryptedPng);
const { OneTimePad } = require('one-time-pad');
const fs = require('fs');

const plainTextBuffer = new Uint8Array(fs.readFileSync('./tests/file.txt'));
const pad = OneTimePad.generatePad(plainTextBuffer);

const encryptedData = OneTimePad.encrypt(pad, plainTextBuffer);
const decryptedData = OneTimePad.decrypt(pad, encryptedData);

See more examples in tests

License

ISC

Acknowledgements

test_image.png file retrieved from: en.wikipedia.org.