From 1cc664c4fa7843352bdebab2463897f35a52b660 Mon Sep 17 00:00:00 2001 From: Mark Stosberg Date: Thu, 27 Aug 2015 16:25:30 -0400 Subject: [PATCH] 1.0.0: Add sync API and update async API to pass error to callback as the first argument. --- CHANGELOG.md | 17 +++++++++++++++++ README.md | 6 ++++-- lib/secure-random-string.js | 10 +++++++--- package.json | 7 +++++-- tests.js | 8 ++++---- 5 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..71c9ce2 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,17 @@ + +# Change Log + +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). + +## [1.0.0] 2015-08-27 + +### New Features + + - New sync API was added. Like crypto.getRandomBytes(), it can throw an exception if the system + is short on entropy. (#1, @markstos) + +### Breaking changes + +- Async API now follows the Node.js convention of returning an error as the first argument + to the callback. The error might be populated if the system runs out of entropy. (#2, @markstos) diff --git a/README.md b/README.md index 728346a..ce76624 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ var srs = require('secure-random-string'); var result = srs(); // Async -srs(function(sr) { +srs(function(err,sr) { console.log(sr); }); @@ -40,7 +40,9 @@ srs({length: 256, urlsafe:true}, function(sr) { ## Error handling -Will throw error if there is not enough accumulated entropy to generate cryptographically strong data. In other words, this without callback will not block even if all entropy sources are drained. + +An error is possible if there is not enough accumulated entropy to generate cryptographically strong data. In other words, this will not block even if all entropy sources are drained. Note that the sync API throws an exception, while +the async API returns the error to the callback. ## Author diff --git a/lib/secure-random-string.js b/lib/secure-random-string.js index 65390a2..876563e 100644 --- a/lib/secure-random-string.js +++ b/lib/secure-random-string.js @@ -11,9 +11,13 @@ function srs(options, cb) { // async path if (cb) { - crypto.randomBytes(length, function(ex, buf) { - if (ex) throw ex; - return cb(_finish(buf)); + crypto.randomBytes(length, function(err, buf) { + if (err) { + return cb(err) + } + else { + return cb(null,_finish(buf)); + } }); } // sync path diff --git a/package.json b/package.json index 4acafd6..945de2a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "secure-random-string", - "version": "0.1.0", + "version": "1.0.0", "description": "Generates a secure random string with a given length", "main": "lib/secure-random-string.js", "scripts": { @@ -11,9 +11,12 @@ "url": "https://github.com/S2-/securerandomstring.git" }, "keywords": [ + "crypto", + "cryptography", "secure", "random", - "string" + "string", + "token" ], "author": "Simon Santoro", "contributors": ["Mark Stosberg "], diff --git a/tests.js b/tests.js index c7780eb..b8a5520 100644 --- a/tests.js +++ b/tests.js @@ -29,28 +29,28 @@ var test = function(name, what, ref, c) { // async tests -srs(function(sr) { +srs(function(err,sr) { test('generate a random string 32 chars long', sr.length, 32 ); }); -srs({length: 1}, function(sr) { +srs({length: 1}, function(err,sr) { test('generate a random string 1 char long', sr.length, 1 ); }); -srs({length: 256}, function(sr) { +srs({length: 256}, function(err,sr) { test('generate a random string 256 chars long', sr.length, 256 ); }); -srs({length: 256, urlsafe: true}, function(sr) { +srs({length: 256, urlsafe: true}, function(err,sr) { test('generate a urlsafe random string 256 chars long', sr.length, 256