How to use the randomBytesAsync function from crypto

Find comprehensive JavaScript crypto.randomBytesAsync code examples handpicked from public code repositorys.

2
3
4
5
6
7
8
9
10
11
var crypto = Promise.promisifyAll(require('crypto'));

function createKey() {
  return new Promise(function(resolve, reject) {
    co.wrap(function *() {
      var bytes = yield crypto.randomBytesAsync(33);
      var key = bytes.toString('base64').replace(/\+/g, '-').replace(/\//g, '_');
      resolve(key);
    })();
  });
fork icon0
star icon6
watch icon3

32
33
34
35
36
37
38
39
40
41
                return result.rows[0] || Promise.reject(new Error('No media available with that hash.'));
        });
}

function upload(uploadStream) {
        return crypto.randomBytesAsync(15).then(function (bytes) {
                var hash = crypto.createHash('sha256');
                var temporaryPath = path.join(temporaryDirectory, bytes.toString('base64').replace(/\//g, '-'));
                var temporaryFileStream = fs.createWriteStream(temporaryPath, { flags: 'wx', mode: 6 << 6 });
                var identifyStream = new IdentifyStream();
fork icon6
star icon0
watch icon1

79
80
81
82
83
84
85
 * Returns a buffer with the specified number of random bytes.
 * @param {number} nBytes - The number of random bytes to generate.
 * @returns {Promise} A promise that is resolved with a buffer containing nBytes random bytes.
 */
module.exports.randomBytesAsync = function(nBytes) {
    return crypto.randomBytesAsync(nBytes);
};
fork icon3
star icon5
watch icon3

10
11
12
13
14
15
16
17
18
19
//Convert Crypto functions to user Promises
Promise.promisifyAll(Crypto);

//Promise function to return random salt
function generateSalt() {
    return Crypto.randomBytesAsync(256);
}

//Promise function to return a random auth token
function generateAuthToken() {
fork icon1
star icon1
watch icon3

+ 3 other calls in file

115
116
117
118
119
120
121
122
123
124
  description: '',
  robots: ['index', 'follow'],
  analyticsService: '',
  analyticsId: ''
})
_.set(WIKI.config, 'sessionSecret', (await crypto.randomBytesAsync(32)).toString('hex'))
_.set(WIKI.config, 'telemetry', {
  isEnabled: req.body.telemetry === true,
  clientId: uuid()
})
fork icon0
star icon0
watch icon3