Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. const { randomBytes } = require('crypto')
  2.  
  3. const ID_LENGTH = 64
  4.  
  5. const escape = string => string
  6. .replace(/\+/g, '-')
  7. .replace(/\//g, '_')
  8. .replace(/=/g, '')
  9.  
  10. module.exports = async () => new Promise((resolve, reject) => {
  11. randomBytes(ID_LENGTH, (error, bytes) => {
  12. if (error) {
  13. reject(error)
  14. } else {
  15. const [ seconds, nanoseconds ] = process.hrtime()
  16. resolve(escape(
  17. crypto
  18. .createHash('sha512')
  19. .update(`${bytes.toString('base64')}${seconds}${nanoseconds}`)
  20. .digest('base64')
  21. ))
  22. }
  23. })
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement