pax_global_header00006660000000000000000000000064134505770110014515gustar00rootroot0000000000000052 comment=dd272851a2429132506c3d430b75e6b1823ffa9c cryptiles-4.2.0/000077500000000000000000000000001345057701100135365ustar00rootroot00000000000000cryptiles-4.2.0/.gitignore000066400000000000000000000001541345057701100155260ustar00rootroot00000000000000**/node_modules **/package-lock.json coverage.* **/.DS_Store **/._* **/*.pem **/.vs **/.vscode **/.idea cryptiles-4.2.0/.npmignore000066400000000000000000000000261345057701100155330ustar00rootroot00000000000000* !lib/** !.npmignore cryptiles-4.2.0/.travis.yml000077500000000000000000000002231345057701100156470ustar00rootroot00000000000000language: node_js node_js: - "8" - "10" - "11" - "node" sudo: false install: - "npm install" os: - "linux" - "osx" - "windows" cryptiles-4.2.0/CHANGELOG.md000066400000000000000000000005541345057701100153530ustar00rootroot00000000000000Breaking changes are documented using GitHub issues, see [issues labeled "release notes"](https://github.com/hapijs/cryptiles/issues?q=is%3Aissue+label%3A%22release+notes%22). If you want changes of a specific minor or patch release, you can browse the [GitHub milestones](https://github.com/hapijs/cryptiles/milestones?state=closed&direction=asc&sort=due_date). cryptiles-4.2.0/LICENSE.md000077500000000000000000000026751345057701100151570ustar00rootroot00000000000000Copyright (c) 2014-2019, Sideway Inc, and project contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cryptiles-4.2.0/README.md000077500000000000000000000010731345057701100150210ustar00rootroot00000000000000# cryptiles General purpose crypto utilities [![Build Status](https://secure.travis-ci.org/hapijs/cryptiles.png)](http://travis-ci.org/hapijs/cryptiles) Lead Maintainer - [Eran Hammer](https://github.com/hueniverse) ## Methods ### `randomString( size)` Returns a cryptographically strong pseudo-random data string. Takes a size argument for the length of the string. ### `randomDigits( size)` Returns a cryptographically strong pseudo-random data string consisting of only numerical digits (0-9). Takes a size argument for the length of the string. cryptiles-4.2.0/lib/000077500000000000000000000000001345057701100143045ustar00rootroot00000000000000cryptiles-4.2.0/lib/index.js000077500000000000000000000031141345057701100157530ustar00rootroot00000000000000'use strict'; const Crypto = require('crypto'); const Boom = require('@hapi/boom'); const internals = {}; // Generate a cryptographically strong pseudo-random data exports.randomString = function (size) { const buffer = exports.randomBits((size + 1) * 6); const string = buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, ''); return string.slice(0, size); }; // Return a random string of digits exports.randomDigits = function (size) { const digits = []; let buffer = internals.random(size * 2); // Provision twice the amount of bytes needed to increase chance of single pass let pos = 0; while (digits.length < size) { if (pos >= buffer.length) { buffer = internals.random(size * 2); pos = 0; } if (buffer[pos] < 250) { digits.push(buffer[pos] % 10); } ++pos; } return digits.join(''); }; // Generate a buffer of random bits exports.randomBits = function (bits) { if (!bits || bits < 0) { throw Boom.internal('Invalid random bits count'); } const bytes = Math.ceil(bits / 8); return internals.random(bytes); }; exports.fixedTimeComparison = function (a, b) { try { return Crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b)); } catch (err) { return false; } }; internals.random = function (bytes) { try { return Crypto.randomBytes(bytes); } catch (err) { throw Boom.internal('Failed generating random bits: ' + err.message); } }; cryptiles-4.2.0/package.json000077500000000000000000000010441345057701100160260ustar00rootroot00000000000000{ "name": "@hapi/cryptiles", "description": "General purpose crypto utilities", "version": "4.2.0", "repository": "git://github.com/hapijs/cryptiles", "main": "lib/index.js", "keywords": [ "cryptography", "security", "utilites" ], "dependencies": { "@hapi/boom": "7.x.x" }, "devDependencies": { "@hapi/code": "5.x.x", "@hapi/lab": "18.x.x" }, "scripts": { "test": "lab -a @hapi/code -t 100 -L", "test-cov-html": "lab -a @hapi/code -r html -o coverage.html" }, "license": "BSD-3-Clause" } cryptiles-4.2.0/test/000077500000000000000000000000001345057701100145155ustar00rootroot00000000000000cryptiles-4.2.0/test/index.js000077500000000000000000000040311345057701100161630ustar00rootroot00000000000000'use strict'; const Code = require('@hapi/code'); const Cryptiles = require('..'); const Lab = require('@hapi/lab'); const internals = {}; const { describe, it } = exports.lab = Lab.script(); const expect = Code.expect; describe('randomString()', () => { it('should generate the right length string', () => { for (let i = 1; i <= 1000; ++i) { expect(Cryptiles.randomString(i).length).to.equal(i); } }); it('returns an error on invalid bits size', () => { expect(() => Cryptiles.randomString(99999999999999999999)).to.throw(/Failed generating random bits/); }); }); describe('randomDigits()', () => { it('should generate the right length string', () => { for (let i = 1; i <= 1000; ++i) { const string = Cryptiles.randomDigits(i); expect(string.length).to.equal(i); expect(string).to.match(/^\d+$/); } }); it('returns an error on invalid bits size', () => { expect(() => Cryptiles.randomDigits(99999999999999999999)).to.throw(/Failed generating random bits/); }); it('generates equal digits distribution', () => { const digits = { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0 }; for (let i = 0; i < 1000000; ++i) { digits[Cryptiles.randomDigits(1)] += 1; } for (const digit in digits) { expect(digits[digit]).to.be.between(99000, 101000); } }); }); describe('randomBits()', () => { it('returns an error on invalid input', () => { expect(() => Cryptiles.randomBits(0)).to.throw('Invalid random bits count'); expect(() => Cryptiles.randomBits(-1)).to.throw('Invalid random bits count'); }); }); describe('fixedTimeComparison()', () => { it('validates strings', () => { expect(Cryptiles.fixedTimeComparison('asdasd', 'asdasd')).to.be.true(); expect(Cryptiles.fixedTimeComparison('', '')).to.be.true(); expect(Cryptiles.fixedTimeComparison('asdas', 'asdasd')).to.be.false(); }); });