|
2 | 2 |
|
3 | 3 | const Promise = require('bluebird');
|
4 | 4 | const mysql = require('mysql');
|
5 |
| -const crypto = require('crypto'); |
6 | 5 | const omit = require('lodash/omit');
|
7 | 6 | const cli = require('../../lib');
|
8 | 7 |
|
@@ -77,7 +76,74 @@ class MySQLExtension extends cli.Extension {
|
77 | 76 | }
|
78 | 77 |
|
79 | 78 | createUser(ctx, dbconfig) {
|
80 |
| - const randomPassword = crypto.randomBytes(10).toString('hex'); |
| 79 | + // Generate random MySQL password |
| 80 | + const generateRandomPassword = () => { |
| 81 | + /** |
| 82 | + * LCG Random |
| 83 | + * @example const random = (lcgRandom()) |
| 84 | + * @description Use Linear Congruential Generator to generate a random number between 0 and 1 |
| 85 | + * Instead of Math.random() |
| 86 | + */ |
| 87 | + const lcgRandom = (() => { |
| 88 | + let _seed = (new Date()).getTime(); |
| 89 | + return () => { |
| 90 | + _seed = (_seed * 9301 + 49297) % 233280; |
| 91 | + return _seed / (233280.0); |
| 92 | + }; |
| 93 | + })(); |
| 94 | + |
| 95 | + /** |
| 96 | + * Random Sort |
| 97 | + * @param {Array} array |
| 98 | + * @return {Array} |
| 99 | + */ |
| 100 | + const randomSort = (array) => array.sort(() => (lcgRandom()) > .5 ? -1 : 1); |
| 101 | + |
| 102 | + /** |
| 103 | + * Random Number |
| 104 | + * @param {Number} length |
| 105 | + * @return {String} |
| 106 | + */ |
| 107 | + const randomNumber = (length) => { |
| 108 | + let _str = ''; |
| 109 | + for (let i = 0; i < length; i = i + 1) { |
| 110 | + _str += String.fromCharCode(Math.round((lcgRandom()) * 9) + 48); |
| 111 | + } |
| 112 | + return _str; |
| 113 | + }; |
| 114 | + |
| 115 | + /** |
| 116 | + * Random Letter |
| 117 | + * @param {Number} length |
| 118 | + * @param {Boolean} [capital=false] |
| 119 | + * @return {String} |
| 120 | + */ |
| 121 | + const randomLetter = (length, capital) => { |
| 122 | + let _str = ''; |
| 123 | + for (let i = 0; i < length; i = i + 1) { |
| 124 | + _str += String.fromCharCode(Math.round((lcgRandom()) * 25) + 97); |
| 125 | + } |
| 126 | + return capital ? _str.toUpperCase() : _str; |
| 127 | + }; |
| 128 | + |
| 129 | + /** |
| 130 | + * Random Symbol |
| 131 | + * @param {Number} length |
| 132 | + * @return {String} |
| 133 | + */ |
| 134 | + const randomSymbol = (length) => { |
| 135 | + const chars = ['~', '!', '@', '#', '$']; |
| 136 | + return randomSort(chars).join('').substr(0, length); |
| 137 | + }; |
| 138 | + |
| 139 | + const allChars = randomNumber(6) |
| 140 | + + randomLetter(4) |
| 141 | + + randomLetter(4, true) |
| 142 | + + randomSymbol(2); |
| 143 | + |
| 144 | + return randomSort(allChars.split('')).join(''); |
| 145 | + }; |
| 146 | + const randomPassword = generateRandomPassword(); |
81 | 147 |
|
82 | 148 | // IMPORTANT: we generate random MySQL usernames
|
83 | 149 | // e.g. you delete all your Ghost instances from your droplet and start from scratch, the MySQL users would remain and the CLI has to generate a random user name to work
|
|
0 commit comments