Skip to content

Commit

Permalink
fix: use Random Number from crypto to generate AutoId
Browse files Browse the repository at this point in the history
This reverts commit 05b3363.
  • Loading branch information
wu-hui committed Mar 25, 2020
1 parent 05b3363 commit ce6ea39
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions dev/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import {randomBytes} from 'crypto';
import {GoogleError, ServiceConfig, Status} from 'google-gax';

import {DocumentData} from './types';
Expand Down Expand Up @@ -52,8 +53,17 @@ export function autoId(): string {
const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let autoId = '';
for (let i = 0; i < 20; i++) {
autoId += chars.charAt(Math.floor(Math.random() * chars.length));
while (autoId.length < 20) {
const bytes = randomBytes(40);
bytes.forEach(b => {
// Length of `chars` is 62. We only take bytes between 0 and 62*4-1
// (both inclusive). The value is then evenly mapped to indices of `char`
// via a modulo operation.
const maxValue = 62 * 4 - 1;
if (autoId.length < 20 && b <= maxValue) {
autoId += chars.charAt(b % 62);
}
});
}
return autoId;
}
Expand Down

0 comments on commit ce6ea39

Please sign in to comment.