Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

secure-random-string

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

secure-random-string - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

.jscsrc

8

CHANGELOG.md

@@ -7,2 +7,8 @@

## [1.1.0] 2017-12-26
### New Features
- alphanumeric option to generate alphanumeric characters only.
## [1.0.0] 2015-08-27

@@ -19,1 +25,3 @@

to the callback. The error might be populated if the system runs out of entropy. (#2, @markstos)
- `urlsafe` option was removed. All strings are URL-safe now. (#4, @S2-, @markstos)

13

lib/secure-random-string.js

@@ -12,3 +12,3 @@ var crypto = require('crypto');

var length = options['length'] || 32;
var alphanumeric = options['alphanumeric'] || false;
// async path

@@ -20,3 +20,3 @@ if (cb) {

}
return cb(null,_finish(buf));
return cb(null, _finish(buf));
});

@@ -29,8 +29,11 @@ }

function _finish (buf) {
function _finish(buf) {
var string = buf.toString('base64');
string = string.replace(/\//g,'_').replace(/\+/g,'-');
if (alphanumeric === true) {
string = string.replace(/[\W_]+/g, '');
} else {
string = string.replace(/\//g, '_').replace(/\+/g, '-');
}
return string.substr(0, length);
}
};

@@ -37,0 +40,0 @@

{
"name": "secure-random-string",
"version": "1.0.0",
"version": "1.1.0",
"description": "Generates a secure random string with a given length",

@@ -5,0 +5,0 @@ "main": "lib/secure-random-string.js",

@@ -37,3 +37,15 @@ # secure-random-string

```
### Options: alphanumeric
Optionally, you can specify a 'alphanumeric' option to get a alphanumerical chars only.
```javascript
// sync
var result = srs({alphanumeric: true});
// async
srs({alphanumeric: true}, function(err, sr) {
console.log(sr);
});
```
## Error handling

@@ -52,2 +64,3 @@

[Mark Stosberg](https://github.com/markstos)
[Sandro Gomez](https://github.com/mrsangrin)

@@ -57,2 +70,1 @@ ## License

[MIT](https://github.com/aheckmann/node-ses/blob/master/LICENSE)

@@ -53,7 +53,12 @@ var srs = require('./lib/secure-random-string.js');

srs({alphanumeric: true}, function(err, sr) {;
test('Must contain alphanumeric only',
sr.match(/^[a-zA-Z0-9_]*$/g)[0] === sr,
true
);
});
// sync tests
test('generate a random string 32 chars long (sync)', srs().length, 32);
test('generate a random string 1 chars long (sync)', srs({length:1}).length, 1);
test('generate a random string 256 chars long (sync)', srs({length:256}).length, 256);
test('generate a random string 1 chars long (sync)', srs({length: 1}).length, 1);
test('generate a random string 256 chars long (sync)', srs({length: 256}).length, 256);

@@ -60,0 +65,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc