mirror of
https://github.com/S2-/securerandomstring.git
synced 2025-08-02 18:30:04 +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:
17
CHANGELOG.md
Normal file
17
CHANGELOG.md
Normal 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)
|
@@ -15,7 +15,7 @@ var srs = require('secure-random-string');
|
|||||||
var result = srs();
|
var result = srs();
|
||||||
|
|
||||||
// Async
|
// Async
|
||||||
srs(function(sr) {
|
srs(function(err,sr) {
|
||||||
console.log(sr);
|
console.log(sr);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -40,7 +40,9 @@ srs({length: 256, urlsafe:true}, function(sr) {
|
|||||||
|
|
||||||
## Error handling
|
## 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
|
## Author
|
||||||
|
|
||||||
|
@@ -11,9 +11,13 @@ function srs(options, cb) {
|
|||||||
|
|
||||||
// async path
|
// async path
|
||||||
if (cb) {
|
if (cb) {
|
||||||
crypto.randomBytes(length, function(ex, buf) {
|
crypto.randomBytes(length, function(err, buf) {
|
||||||
if (ex) throw ex;
|
if (err) {
|
||||||
return cb(_finish(buf));
|
return cb(err)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return cb(null,_finish(buf));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// sync path
|
// sync path
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "secure-random-string",
|
"name": "secure-random-string",
|
||||||
"version": "0.1.0",
|
"version": "1.0.0",
|
||||||
"description": "Generates a secure random string with a given length",
|
"description": "Generates a secure random string with a given length",
|
||||||
"main": "lib/secure-random-string.js",
|
"main": "lib/secure-random-string.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -11,9 +11,12 @@
|
|||||||
"url": "https://github.com/S2-/securerandomstring.git"
|
"url": "https://github.com/S2-/securerandomstring.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
"crypto",
|
||||||
|
"cryptography",
|
||||||
"secure",
|
"secure",
|
||||||
"random",
|
"random",
|
||||||
"string"
|
"string",
|
||||||
|
"token"
|
||||||
],
|
],
|
||||||
"author": "Simon Santoro",
|
"author": "Simon Santoro",
|
||||||
"contributors": ["Mark Stosberg <mark@rideamigos.com>"],
|
"contributors": ["Mark Stosberg <mark@rideamigos.com>"],
|
||||||
|
8
tests.js
8
tests.js
@@ -29,28 +29,28 @@ var test = function(name, what, ref, c) {
|
|||||||
|
|
||||||
|
|
||||||
// async tests
|
// async tests
|
||||||
srs(function(sr) {
|
srs(function(err,sr) {
|
||||||
test('generate a random string 32 chars long',
|
test('generate a random string 32 chars long',
|
||||||
sr.length,
|
sr.length,
|
||||||
32
|
32
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
srs({length: 1}, function(sr) {
|
srs({length: 1}, function(err,sr) {
|
||||||
test('generate a random string 1 char long',
|
test('generate a random string 1 char long',
|
||||||
sr.length,
|
sr.length,
|
||||||
1
|
1
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
srs({length: 256}, function(sr) {
|
srs({length: 256}, function(err,sr) {
|
||||||
test('generate a random string 256 chars long',
|
test('generate a random string 256 chars long',
|
||||||
sr.length,
|
sr.length,
|
||||||
256
|
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',
|
test('generate a urlsafe random string 256 chars long',
|
||||||
sr.length,
|
sr.length,
|
||||||
256
|
256
|
||||||
|
Reference in New Issue
Block a user