1
0
mirror of https://github.com/S2-/securerandomstring.git synced 2025-08-02 02:10:05 +02:00

1.0.0: Add sync API and update async API to pass error to callback as the first argument.

This commit is contained in:
Mark Stosberg
2015-08-27 16:25:30 -04:00
parent 3358bf21fc
commit 1cc664c4fa
5 changed files with 37 additions and 11 deletions

17
CHANGELOG.md Normal file
View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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 <mark@rideamigos.com>"],

View File

@@ -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