pax_global_header00006660000000000000000000000064133277236410014522gustar00rootroot0000000000000052 comment=78e335e7395d392031333b2e6016419ae5d91bf2 tsscmp-1.0.6/000077500000000000000000000000001332772364100130375ustar00rootroot00000000000000tsscmp-1.0.6/.gitignore000066400000000000000000000000161332772364100150240ustar00rootroot00000000000000node_modules/ tsscmp-1.0.6/.travis.yml000066400000000000000000000002201332772364100151420ustar00rootroot00000000000000language: node_js node_js: - "6" - "5" - "5.1" - "4" - "4.2" - "4.1" - "4.0" - "0.12" - "0.11" - "0.10" - "0.8" - "0.6" tsscmp-1.0.6/LICENSE000066400000000000000000000020521332772364100140430ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. tsscmp-1.0.6/README.md000066400000000000000000000036101332772364100143160ustar00rootroot00000000000000# Timing safe string compare using double HMAC [![Node.js Version](https://img.shields.io/node/v/tsscmp.svg?style=flat-square)](https://nodejs.org/en/download) [![npm](https://img.shields.io/npm/v/tsscmp.svg?style=flat-square)](https://npmjs.org/package/tsscmp) [![NPM Downloads](https://img.shields.io/npm/dm/tsscmp.svg?style=flat-square)](https://npmjs.org/package/tsscmp) [![Build Status](https://img.shields.io/travis/suryagh/tsscmp/master.svg?style=flat-square)](https://travis-ci.org/suryagh/tsscmp) [![Build Status](https://img.shields.io/appveyor/ci/suryagh/tsscmp/master.svg?style=flat-square&label=windows)](https://ci.appveyor.com/project/suryagh/tsscmp) [![Dependency Status](http://img.shields.io/david/suryagh/tsscmp.svg?style=flat-square)](https://david-dm.org/suryagh/tsscmp) [![npm-license](http://img.shields.io/npm/l/tsscmp.svg?style=flat-square)](LICENSE) Prevents [timing attacks](http://codahale.com/a-lesson-in-timing-attacks/) using Brad Hill's [Double HMAC pattern](https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2011/february/double-hmac-verification/) to perform secure string comparison. Double HMAC avoids the timing atacks by blinding the timing channel using random time per attempt comparison against iterative brute force attacks. ## Install ``` npm install tsscmp ``` ## Why To compare secret values like **authentication tokens**, **passwords** or **capability urls** so that timing information is not leaked to the attacker. ## Example ```js var timingSafeCompare = require('tsscmp'); var sessionToken = '127e6fbfe24a750e72930c'; var givenToken = '127e6fbfe24a750e72930c'; if (timingSafeCompare(sessionToken, givenToken)) { console.log('good token'); } else { console.log('bad token'); } ``` ##License: [MIT](LICENSE) **Credits to:** [@jsha](https://github.com/jsha) | [@bnoordhuis](https://github.com/bnoordhuis) | [@suryagh](https://github.com/suryagh) | tsscmp-1.0.6/appveyor.yml000066400000000000000000000012561332772364100154330ustar00rootroot00000000000000# Test against this version of Node.js environment: matrix: # nodejs_version: "0.6" not supported in Windows x86 - nodejs_version: "0.8" - nodejs_version: "0.10" - nodejs_version: "0.11" - nodejs_version: "0.12" - nodejs_version: "4.0" - nodejs_version: "5.0" - nodejs_version: "6.0" # Install scripts. (runs after repo cloning) install: # Get the latest stable version of Node.js or io.js - ps: Install-Product node $env:nodejs_version # install modules - npm install # Post-install test scripts. test_script: # Output useful info for debugging. - node --version - npm --version # run tests - npm test # Don't actually build. build: off tsscmp-1.0.6/lib/000077500000000000000000000000001332772364100136055ustar00rootroot00000000000000tsscmp-1.0.6/lib/index.js000066400000000000000000000021621332772364100152530ustar00rootroot00000000000000'use strict'; // Implements Brad Hill's Double HMAC pattern from // https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2011/february/double-hmac-verification/. // The approach is similar to the node's native implementation of timing safe buffer comparison that will be available on v6+. // https://github.com/nodejs/node/issues/3043 // https://github.com/nodejs/node/pull/3073 var crypto = require('crypto'); function bufferEqual(a, b) { if (a.length !== b.length) { return false; } // `crypto.timingSafeEqual` was introduced in Node v6.6.0 // if (crypto.timingSafeEqual) { return crypto.timingSafeEqual(a, b); } for (var i = 0; i < a.length; i++) { if (a[i] !== b[i]) { return false; } } return true; } function timeSafeCompare(a, b) { var sa = String(a); var sb = String(b); var key = crypto.pseudoRandomBytes(32); var ah = crypto.createHmac('sha256', key).update(sa).digest(); var bh = crypto.createHmac('sha256', key).update(sb).digest(); return bufferEqual(ah, bh) && a === b; } module.exports = timeSafeCompare; tsscmp-1.0.6/package.json000066400000000000000000000011671332772364100153320ustar00rootroot00000000000000{ "name": "tsscmp", "version": "1.0.6", "description": "Timing safe string compare using double HMAC", "main": "lib/index.js", "dependencies": {}, "devDependencies": {}, "scripts": { "test": "node test/unit && node test/benchmark" }, "repository": { "type": "git", "url": "https://github.com/suryagh/tsscmp.git" }, "keywords": [ "timing safe string compare", "double hmac string compare", "safe string compare", "hmac" ], "author": "suryagh", "publishConfig": { "registry": "https://registry.npmjs.org" }, "engines": { "node": ">=0.6.x" }, "license": "MIT" } tsscmp-1.0.6/test/000077500000000000000000000000001332772364100140165ustar00rootroot00000000000000tsscmp-1.0.6/test/benchmark/000077500000000000000000000000001332772364100157505ustar00rootroot00000000000000tsscmp-1.0.6/test/benchmark/index.js000066400000000000000000000012701332772364100174150ustar00rootroot00000000000000'use strict'; var timeSafeCompare = require('../../lib/index'); function random(length) { length = length || 32; var result = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-+/*[]{}-=\|;\':\"<>?,./"; for( var i=0; i < length; i++ ){ result += possible.charAt(Math.floor(Math.random() * possible.length)); } return result; } function run(count) { count = count || 100*1000; console.log('benchmark count: ' + count/1000 + 'k'); console.time('benchmark'); while(count--){ timeSafeCompare(random(), random()); } console.timeEnd('benchmark'); } run(100000); module.exports = run; tsscmp-1.0.6/test/unit/000077500000000000000000000000001332772364100147755ustar00rootroot00000000000000tsscmp-1.0.6/test/unit/index.js000066400000000000000000000033261332772364100164460ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var timeSafeCompare = require('../../lib/index'); process.on('error', function (e) { console.log('caught: ' + e); }); function testEqual(a, b) { assert(timeSafeCompare(a, b)); // lets also do a parity check with the strict equal to operator assert(a === b); } function testNotEqual(a, b) { assert(!timeSafeCompare(a, b)); // lets also do a parity check with the strict not equal to operator assert(a !== b); } // note: lets also make sure tsscmp can be inline replaced for any types - // just incase if anyone is interested // positive tests testEqual('127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935', '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935', 'test '); testEqual('a', 'a'); testEqual('', ''); testEqual(undefined, undefined); testEqual(true, true); testEqual(false, false); (function () { var a = { a: 1 }; testEqual(a, a); })(); (function () { function f1() { return 1; }; testEqual(f1, f1); })(); // negative tests testNotEqual(''); testNotEqual('a', 'b'); testNotEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'); testNotEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a'); testNotEqual('alpha', 'beta'); testNotEqual(false, true); testNotEqual(false, undefined); testNotEqual(function () { }, function () { }); testNotEqual({}, {}); testNotEqual({ a: 1 }, { a: 1 }); testNotEqual({ a: 1 }, { a: 2 }); testNotEqual([1, 2], [1, 2]); testNotEqual([1, 2], [1, 2, 3]); (function () { var a = { p: 1 }; var b = { p: 1 }; testNotEqual(a, b); })(); (function () { function f1() { return 1; }; function f2() { return 1; }; testNotEqual(f1, f2); })(); console.log('Success: all tests complete.');