12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- var crypto = require('crypto');
- var UIDCHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- function tostr(bytes) {
- var chars, r, i;
- r = [];
- for (i = 0; i < bytes.length; i++) {
- r.push(UIDCHARS[bytes[i] % UIDCHARS.length]);
- }
- return r.join('');
- }
- function uid(length, cb) {
- if (typeof cb === 'undefined') {
- return tostr(crypto.pseudoRandomBytes(length));
- } else {
- crypto.pseudoRandomBytes(length, function(err, bytes) {
- if (err) return cb(err);
- cb(null, tostr(bytes));
- })
- }
- }
- module.exports = uid;
|