├── .github └── workflows │ └── test-node.yml ├── .gitignore ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.github/workflows/test-node.yml: -------------------------------------------------------------------------------- 1 | name: Build Status 2 | on: 3 | push: 4 | branches: 5 | - master 6 | pull_request: 7 | branches: 8 | - master 9 | jobs: 10 | build: 11 | strategy: 12 | matrix: 13 | node-version: [lts/*] 14 | os: [ubuntu-latest, macos-latest, windows-latest] 15 | runs-on: ${{ matrix.os }} 16 | steps: 17 | - uses: actions/checkout@v3 18 | - name: Use Node.js ${{ matrix.node-version }} 19 | uses: actions/setup-node@v3 20 | with: 21 | node-version: ${{ matrix.node-version }} 22 | - run: npm install 23 | - run: npm test 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # random-bytes-seed 2 | 3 | Get a random buffer based on a seed. Useful for reproducible tests. 4 | 5 | ``` 6 | npm install random-bytes-seed 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/random-bytes-seed.svg?style=flat)](http://travis-ci.org/mafintosh/random-bytes-seed) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var seed = require('random-bytes-seed') 15 | var randomBytes = seed('a seed') 16 | 17 | console.log(randomBytes(10)) // get 10 pseudo random bytes 18 | console.log(randomBytes(20)) // get 20 pseudo random bytes 19 | ``` 20 | 21 | If you run the above example multiple times you'll notice it is returning the same "random" bytes every time because it is using the same seed. 22 | You should only use this for testing / debugging as there are probably some security problems with the way this is implemented ¯\\_(ツ)_/¯. 23 | 24 | When testing you can override `crypto.randomBytes` with the seeded version like this 25 | 26 | ``` js 27 | var crypto = require('crypto') 28 | 29 | crypto.randomBytes = seed('a seed') 30 | ``` 31 | 32 | ## API 33 | 34 | #### `var randomBytes = seed([seed])` 35 | 36 | Create a new randomBytes generator. If you do not provide a seed a random one is chosen for you. 37 | 38 | #### `var buf = randomBytes(numberOfBytes)` 39 | 40 | Returns a new random buffer based on the seed. 41 | 42 | #### `randomBytes.seed` 43 | 44 | The seed originally used. 45 | 46 | #### `randomBytes.currentSeed` 47 | 48 | The seed for the next random operation. 49 | 50 | ## License 51 | 52 | MIT 53 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const seed = require('random-bytes-seed') 2 | const randomBytes = seed('a seed') 3 | 4 | console.log(randomBytes(10)) // get 10 pseudo random bytes 5 | console.log(randomBytes(20)) // get 20 pseudo random bytes 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const sodium = require('sodium-universal') 2 | 3 | module.exports = function (seed) { 4 | randomBytes.seed = seed = seed || randomBytesClassic(32) 5 | randomBytes.currentSeed = seed 6 | 7 | return randomBytes 8 | 9 | function randomBytes (n) { 10 | const result = Buffer.allocUnsafe(n) 11 | let used = 0 12 | 13 | while (used < result.length) { 14 | randomBytes.currentSeed = seed = next(seed) 15 | seed.copy(result, used) 16 | used += seed.length 17 | } 18 | 19 | return result 20 | } 21 | } 22 | 23 | function next (seed) { 24 | const output = Buffer.alloc(32) 25 | sodium.crypto_hash_sha256(output, Buffer.from(seed)) 26 | return output 27 | } 28 | 29 | function randomBytesClassic (n) { 30 | const buf = Buffer.allocUnsafe(n) 31 | sodium.randombytes_buf(buf) 32 | return buf 33 | } 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "random-bytes-seed", 3 | "version": "2.0.0", 4 | "description": "Get a random buffer based on a seed. Useful for reproducible tests", 5 | "main": "index.js", 6 | "dependencies": { 7 | "sodium-universal": "^4.0.1" 8 | }, 9 | "devDependencies": { 10 | "brittle": "^3.5.2", 11 | "standard": "^17.1.0" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/mafintosh/random-bytes-seed.git" 16 | }, 17 | "scripts": { 18 | "test": "standard && brittle test.js" 19 | }, 20 | "author": "Mathias Buus (@mafintosh)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/mafintosh/random-bytes-seed/issues" 24 | }, 25 | "homepage": "https://github.com/mafintosh/random-bytes-seed" 26 | } 27 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const test = require('brittle') 2 | const seed = require('./') 3 | 4 | test('returns a buffer', (t) => { 5 | const randomBytes = seed() 6 | 7 | let b = randomBytes(10) 8 | t.ok(Buffer.isBuffer(b)) 9 | t.is(b.length, 10) 10 | 11 | b = randomBytes(100) 12 | t.ok(Buffer.isBuffer(b)) 13 | t.is(b.length, 100) 14 | }) 15 | 16 | test('is seedable', (t) => { 17 | let randomBytes = seed() 18 | 19 | const b1 = randomBytes(10) 20 | t.ok(Buffer.isBuffer(b1)) 21 | t.is(b1.length, 10) 22 | 23 | const b2 = randomBytes(100) 24 | t.ok(Buffer.isBuffer(b2)) 25 | t.is(b2.length, 100) 26 | 27 | randomBytes = seed(randomBytes.seed) 28 | t.alike(b1, randomBytes(10)) 29 | t.alike(b2, randomBytes(100)) 30 | }) 31 | --------------------------------------------------------------------------------