Git Product home page Git Product logo

xsalsa20-encoding's Introduction

xsalsa20-encoding

XSalsa20 codec that implements that abstract-encoding interface. Nonces are generated randomly and prepended to the ciphertext.

Installation

$ npm install xsalsa20-encoding

Usage

const codec = require('xsalsa20-encoding')(secretKey)

// encode a value
buffer = codec.encode(value)

// decode a value
value = codec.decode(buffer)

Example

const crypto = require('crypto')
const Codec = require('xsalsa20-encoding')

const key = crypto.randomBytes(32)

const codec = Codec(key)
const hello = codec.encode('hello')
const world = codec.encode('world')

console.log('%s %s', codec.decode(hello), codec.decode(world)) // 'hello world'

Custom Value Encodings

const pbs = require('protocol-buffers')
const { Message } = pbs(`
message {
  string data = 1;
}
`)

const codec = Codec(key, { valueEncoding: Message })
const encoded = codec.encode({ data: 'hello world' })
const message = codec.decode(encoded) // { data: 'hello world' }

API

codec = require('xsalsa20-encoding')([secretKey[, opts])

Create a codec object from 32 byte secretKey.

const key = crypto.randomBytes(32)
const codec = Codec(key)

buffer = codec.encode(value[, output[, offset]])

Encode a value using xsalsa20 (XOR) into an optional output buffer at an optional offset defaulting to 0. If an output buffer is not given, one is allocated for you and returned.

const buffer = codec.encode('hello world')

value = codec.decode(buffer[, offset])

Decode a buffer using xsalsa20 (XOR) at an optional offset defaulting to 0.

const value = codec.decode(buffer)

length = codec.encodingLength(value)

Returns the encoding length for a given value.

const length = codec.encodingLength('hello world') // 35

License

MIT

xsalsa20-encoding's People

Contributors

bcomnes avatar jwerle avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

xsalsa20-encoding's Issues

nonce reuse

I think you are reusing the nonce when encoding multiple values which can lead to security issues.

A solution would be to generate a new nonce everytime and return it as part of the encoded payload.

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.