You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
16 lines
361 B
CoffeeScript
16 lines
361 B
CoffeeScript
9 years ago
|
Promise = require "bluebird"
|
||
|
crypto = Promise.promisifyAll(require "crypto")
|
||
|
|
||
|
module.exports = (length = 16) ->
|
||
|
Promise.try ->
|
||
|
byteLength = Math.ceil(length / 4) * 3
|
||
|
return crypto.randomBytesAsync(byteLength)
|
||
|
.then (bytes) ->
|
||
|
bytes = bytes
|
||
|
.toString "base64"
|
||
|
.replace /\+/g, "-"
|
||
|
.replace /\//g, "_"
|
||
|
.slice 0, length
|
||
|
|
||
|
Promise.resolve bytes
|