shortening of noteIds to 12 characters

This commit is contained in:
azivner
2017-10-28 12:12:20 -04:00
parent a76e82e9a2
commit 966ac6f620
6 changed files with 16 additions and 14 deletions

View File

@@ -1,20 +1,16 @@
"use strict";
const crypto = require('crypto');
function randomToken(length) {
return crypto.randomBytes(length).toString('base64');
}
function newNoteId() {
return randomString(22, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
return randomString(12);
}
const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function randomString(length, chars) {
let result = '';
for (let i = length; i > 0; --i) {
result += chars[Math.floor(Math.random() * chars.length)];
result += ALPHA_NUMERIC[Math.floor(Math.random() * ALPHA_NUMERIC.length)];
}
return result;
@@ -33,7 +29,7 @@ function fromBase64(encodedText) {
}
module.exports = {
randomToken,
randomString,
nowTimestamp,
newNoteId,
toBase64,