1 | var crypto = require('crypto')
|
2 |
|
3 | if (crypto.randomFill) {
|
4 |
|
5 |
|
6 | module.exports = function (bytes) {
|
7 | return new Promise(function (resolve, reject) {
|
8 |
|
9 |
|
10 | crypto.randomFill(Buffer.allocUnsafe(bytes), function (err, buf) {
|
11 | if (err) {
|
12 | reject(err)
|
13 | } else {
|
14 | resolve(buf)
|
15 | }
|
16 | })
|
17 | })
|
18 | }
|
19 | } else {
|
20 | module.exports = function (bytes) {
|
21 | return new Promise(function (resolve, reject) {
|
22 | crypto.randomBytes(bytes, function (err, buf) {
|
23 | if (err) {
|
24 | reject(err)
|
25 | } else {
|
26 | resolve(buf)
|
27 | }
|
28 | })
|
29 | })
|
30 | }
|
31 | }
|