SHOW:
|
|
- or go back to the newest paste.
| 1 | module.exports = exports = function() {
| |
| 2 | var defaultAlphabets = {
| |
| 3 | - | var bignum = require('bignum');
|
| 3 | + | |
| 4 | - | var crypto = require('crypto');
|
| 4 | + | |
| 5 | 32: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", | |
| 6 | 36: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", | |
| 7 | - | /* |
| 7 | + | |
| 8 | - | * Default encoding alphabets |
| 8 | + | |
| 9 | - | * URL-friendly base-64 encoding is chosen. Base-32 is best suited |
| 9 | + | |
| 10 | - | * for tiny-URL like applications, because I and 1 can't be confused |
| 10 | + | |
| 11 | - | * and the upper-case characters are more easily remembered by a human. |
| 11 | + | |
| 12 | - | * |
| 12 | + | |
| 13 | - | * Where "native", we use the bignum native encoding routine. |
| 13 | + | |
| 14 | - | */ |
| 14 | + | |
| 15 | if (!alphabet) {
| |
| 16 | var err = new Error("Only base " + Object.keys(alphabets).join(", ") + " supported if an alphabet is not provided.");
| |
| 17 | cb(err, null); | |
| 18 | return; | |
| 19 | } | |
| 20 | var id=""; | |
| 21 | while(id.length<len) | |
| 22 | {
| |
| 23 | id+=alphabet[(Math.random()*alphabet.length|0)].toString(); | |
| 24 | - | /* |
| 24 | + | |
| 25 | - | // Convert the bignum to a string with the given base |
| 25 | + | |
| 26 | - | function bignumToString(bignum, base, alphabet) {
|
| 26 | + | |
| 27 | - | // Prefer native conversion |
| 27 | + |