123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.md5Hash = md5Hash;
- exports.newObjectId = newObjectId;
- exports.newToken = newToken;
- exports.randomHexString = randomHexString;
- exports.randomString = randomString;
- var _crypto = require("crypto");
- function randomHexString(size) {
- if (size === 0) {
- throw new Error('Zero-length randomHexString is useless.');
- }
- if (size % 2 !== 0) {
- throw new Error('randomHexString size must be divisible by 2.');
- }
- return (0, _crypto.randomBytes)(size / 2).toString('hex');
- }
- function randomString(size) {
- if (size === 0) {
- throw new Error('Zero-length randomString is useless.');
- }
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789';
- let objectId = '';
- const bytes = (0, _crypto.randomBytes)(size);
- for (let i = 0; i < bytes.length; ++i) {
- objectId += chars[bytes.readUInt8(i) % chars.length];
- }
- return objectId;
- }
- function newObjectId(size = 10) {
- return randomString(size);
- }
- function newToken() {
- return randomHexString(32);
- }
- function md5Hash(string) {
- return (0, _crypto.createHash)('md5').update(string).digest('hex');
- }
|