@bpe/one-time-pad
v1.0.2
Published
A more complete One-time pad implementation supporting Strings, Buffers and Files
Downloads
4
Maintainers
Readme
One-Time Pad
A simple and more complete One-Time Pad implementation in NodeJS
- 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.