pax_global_header00006660000000000000000000000064136575752130014530gustar00rootroot0000000000000052 comment=d6d03a80d1152c5cf4da4eb0b1fe862ec5755dcd cryptiles-5.1.0/000077500000000000000000000000001365757521300135515ustar00rootroot00000000000000cryptiles-5.1.0/.gitignore000066400000000000000000000001541365757521300155410ustar00rootroot00000000000000**/node_modules **/package-lock.json coverage.* **/.DS_Store **/._* **/*.pem **/.vs **/.vscode **/.idea cryptiles-5.1.0/.travis.yml000077500000000000000000000002131365757521300156610ustar00rootroot00000000000000language: node_js node_js: - "14" - "12" - "node" sudo: false install: - "npm install" os: - "linux" - "osx" - "windows" cryptiles-5.1.0/API.md000077500000000000000000000010111365757521300145000ustar00rootroot00000000000000 ## Methods ### `randomString( size)` Returns a cryptographically strong pseudo-random data string. Takes a size argument for the length of the string. ### `randomAlphanumString( size)` Returns a cryptographically strong pseudo-random alphanumeric 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-5.1.0/LICENSE.md000077500000000000000000000026751365757521300151720ustar00rootroot00000000000000Copyright (c) 2014-2020, 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-5.1.0/README.md000077500000000000000000000020231365757521300150300ustar00rootroot00000000000000 # @hapi/cryptiles #### General purpose crypto utilities. **cryptiles** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together. ### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support ## Useful resources - [Documentation and API](https://hapi.dev/family/cryptiles/) - [Versions status](https://hapi.dev/resources/status/#cryptiles) (builds, dependencies, node versions, licenses, eol) - [Changelog](https://hapi.dev/family/cryptiles/changelog/) - [Project policies](https://hapi.dev/policies/) - [Free and commercial support options](https://hapi.dev/support/) cryptiles-5.1.0/lib/000077500000000000000000000000001365757521300143175ustar00rootroot00000000000000cryptiles-5.1.0/lib/index.d.ts000077500000000000000000000017461365757521300162330ustar00rootroot00000000000000/** Generate a cryptographically strong pseudo-random data @param size - Size of the string @returns A cryptographically strong pseudo-random data */ export function randomString(size: number): string; /** Generate a cryptographically strong pseudo-random alphanumeric data @param size - Size of the string @returns A cryptographically strong pseudo-random alphanumeric data */ export function randomAlphanumString(size: number): string; /** Return a random string of digits @param size - Size of the digits @returns A random string of digits */ export function randomDigits(size: number): string; /** Generate a buffer of random bits @param bits - Number of bits @returns A buffer of random bits */ export function randomBits(bits: number): Buffer; /** Generate a buffer of random bits @param a - Data to compare @param b - Data to compare @returns A boolean comparing a and b */ export function fixedTimeComparison(a: string | Array, b: string | Array): boolean; cryptiles-5.1.0/lib/index.js000077500000000000000000000036561365757521300160010ustar00rootroot00000000000000'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); }; // Generate a cryptographically strong pseudo-random alphanum data exports.randomAlphanumString = function (size) { let result = ''; while (result.length < size) { const buffer = exports.randomBits((size + 1) * 6); result += buffer.toString('base64').replace(/[^a-zA-Z0-9]/g, ''); } return result.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-5.1.0/package.json000077500000000000000000000012251365757521300160420ustar00rootroot00000000000000{ "name": "@hapi/cryptiles", "description": "General purpose crypto utilities", "version": "5.1.0", "repository": "git://github.com/hapijs/cryptiles", "main": "lib/index.js", "types": "lib/index.d.ts", "engines": { "node": ">=12.0.0" }, "files": [ "lib" ], "keywords": [ "cryptography", "security", "utilites" ], "dependencies": { "@hapi/boom": "9.x.x" }, "devDependencies": { "@hapi/code": "8.x.x", "@hapi/lab": "22.x.x" }, "scripts": { "test": "lab -a @hapi/code -t 100 -L -Y", "test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html" }, "license": "BSD-3-Clause" } cryptiles-5.1.0/test/000077500000000000000000000000001365757521300145305ustar00rootroot00000000000000cryptiles-5.1.0/test/index.js000077500000000000000000000050351365757521300162030ustar00rootroot00000000000000'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('randomAlphanumString()', () => { it('should generate the right length string', () => { for (let i = 1; i <= 1000; ++i) { const string = Cryptiles.randomAlphanumString(i); expect(string.length).to.equal(i); expect(string).to.match(/^[a-zA-Z0-9]+$/); } }); it('returns an error on invalid bits size', () => { expect(() => Cryptiles.randomAlphanumString(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(); }); }); cryptiles-5.1.0/test/index.ts000077500000000000000000000035121365757521300162130ustar00rootroot00000000000000import * as Cryptiles from '..'; import * as Lab from '@hapi/lab'; const { expect } = Lab.types; // randomString() Cryptiles.randomString(256); Cryptiles.randomString(5 * 5); expect.type(Cryptiles.randomString(128)) expect.error(Cryptiles.randomString('some')); expect.error(Cryptiles.randomString(true)); expect.error(Cryptiles.randomString({ foo: true })); expect.error(Cryptiles.randomString(128, 256)); // randomAlphanumString() Cryptiles.randomAlphanumString(256); Cryptiles.randomAlphanumString(5 * 5); expect.type(Cryptiles.randomAlphanumString(128)) expect.error(Cryptiles.randomAlphanumString('some')); expect.error(Cryptiles.randomAlphanumString(true)); expect.error(Cryptiles.randomAlphanumString({ foo: true })); expect.error(Cryptiles.randomAlphanumString(128, 256)); // randomDigits() Cryptiles.randomDigits(256); Cryptiles.randomDigits(5 * 5); expect.type(Cryptiles.randomDigits(128)) expect.error(Cryptiles.randomDigits('some')); expect.error(Cryptiles.randomDigits(true)); expect.error(Cryptiles.randomDigits({ foo: true })); expect.error(Cryptiles.randomDigits(128, 256)); // randomBits() Cryptiles.randomBits(256); Cryptiles.randomBits(5 * 5); expect.type(Cryptiles.randomBits(128)) expect.error(Cryptiles.randomBits('some')); expect.error(Cryptiles.randomBits(true)); expect.error(Cryptiles.randomBits({ foo: true })); expect.error(Cryptiles.randomBits(128, 256)); // fixedTimeComparison() Cryptiles.fixedTimeComparison(["foo"], ["bar"]); Cryptiles.fixedTimeComparison("foo", "bar"); Cryptiles.fixedTimeComparison("foo", ["foo"]); expect.type(Cryptiles.fixedTimeComparison("foo", "foo")) expect.error(Cryptiles.fixedTimeComparison('foo', 24)); expect.error(Cryptiles.fixedTimeComparison({ foo: "bar" }, "foo")); expect.error(Cryptiles.fixedTimeComparison("foo", "bar", "foo"));