index.js 692 B

1234567891011121314151617181920
  1. 'use strict'
  2. var crypto = require('crypto')
  3. var MurmurHash3 = require('imurmurhash')
  4. module.exports = function (uniq) {
  5. if (uniq) {
  6. var hash = new MurmurHash3(uniq)
  7. return ('00000000' + hash.result().toString(16)).substr(-8)
  8. } else {
  9. // Called without a callback, because this interface should neither block
  10. // nor error (by contrast with randomBytes which will throw an exception
  11. // without enough entropy).
  12. //
  13. // However, due to a change in Node 0.10.27+, pseudoRandomBytes is now the
  14. // same as randomBytes, and may in fact block in situations where
  15. // insufficent entropy is available.
  16. return crypto.pseudoRandomBytes(4).toString('hex')
  17. }
  18. }