mirror of https://github.com/seald/nedb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.3 KiB
76 lines
2.3 KiB
12 years ago
|
/**
|
||
|
* Specific customUtils for the browser, where we don't have access to the Crypto and Buffer modules
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* Taken from the crypto-browserify module
|
||
|
* https://github.com/dominictarr/crypto-browserify
|
||
|
* NOTE: Math.random() does not guarantee "cryptographic quality" but we actually don't need it
|
||
|
*/
|
||
|
function randomBytes (size) {
|
||
4 years ago
|
const bytes = new Array(size)
|
||
12 years ago
|
|
||
4 years ago
|
for (let i = 0, r; i < size; i++) {
|
||
|
if ((i & 0x03) === 0) r = Math.random() * 0x100000000
|
||
|
bytes[i] = r >>> ((i & 0x03) << 3) & 0xff
|
||
12 years ago
|
}
|
||
|
|
||
4 years ago
|
return bytes
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Taken from the base64-js module
|
||
|
* https://github.com/beatgammit/base64-js/
|
||
|
*/
|
||
|
function byteArrayToBase64 (uint8) {
|
||
4 years ago
|
const lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||
|
const extraBytes = uint8.length % 3 // if we have 1 byte left, pad 2 bytes
|
||
|
let output = ''
|
||
|
let temp
|
||
|
let length
|
||
|
let i
|
||
12 years ago
|
|
||
|
function tripletToBase64 (num) {
|
||
4 years ago
|
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
|
||
|
}
|
||
12 years ago
|
|
||
|
// go through the array every three bytes, we'll deal with trailing stuff later
|
||
|
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
|
||
4 years ago
|
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
|
||
|
output += tripletToBase64(temp)
|
||
12 years ago
|
}
|
||
|
|
||
|
// pad the end with zeros, but make sure to not forget the extra bytes
|
||
|
switch (extraBytes) {
|
||
|
case 1:
|
||
4 years ago
|
temp = uint8[uint8.length - 1]
|
||
|
output += lookup[temp >> 2]
|
||
|
output += lookup[(temp << 4) & 0x3F]
|
||
|
output += '=='
|
||
|
break
|
||
12 years ago
|
case 2:
|
||
4 years ago
|
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
|
||
|
output += lookup[temp >> 10]
|
||
|
output += lookup[(temp >> 4) & 0x3F]
|
||
|
output += lookup[(temp << 2) & 0x3F]
|
||
|
output += '='
|
||
|
break
|
||
12 years ago
|
}
|
||
|
|
||
4 years ago
|
return output
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Return a random alphanumerical string of length len
|
||
|
* There is a very small probability (less than 1/1,000,000) for the length to be less than len
|
||
|
* (il the base64 conversion yields too many pluses and slashes) but
|
||
|
* that's not an issue here
|
||
|
* The probability of a collision is extremely small (need 3*10^12 documents to have one chance in a million of a collision)
|
||
|
* See http://en.wikipedia.org/wiki/Birthday_problem
|
||
|
*/
|
||
|
function uid (len) {
|
||
4 years ago
|
return byteArrayToBase64(randomBytes(Math.ceil(Math.max(8, len * 2)))).replace(/[+/]/g, '').slice(0, len)
|
||
12 years ago
|
}
|
||
|
|
||
4 years ago
|
module.exports.uid = uid
|