Skip to content

Commit 225ee39

Browse files
committed
random-number-generator
1 parent e112573 commit 225ee39

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/codedataUtil.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,33 @@ import CryptoJS from "crypto-js";
33

44

55

6-
6+
const shuffleCharacterAt = (content, rNumber) => {
7+
if (rNumber < 1) {
8+
return content.slice(1) + content.charAt(0);
9+
}
10+
const rIndex = rNumber % content.length;
11+
if ((rIndex + 1) >= content.length) {
12+
return content.charAt(content.length - 1) + content.slice(0, content.length - 1);
13+
}
14+
return content.slice(0, rIndex) + content.slice(rIndex + 1) + content.charAt(rIndex);
15+
}
16+
const randomNumberGenerator = () => {
17+
const indexString = CryptoJS.enc.Hex.stringify(CryptoJS.lib.WordArray.random(1));
18+
return parseInt(indexString, 16);
19+
}
20+
let possibleCharactersSeed = "Abm6fixYq;rMh9sSkjaGvl@*$tOVDZRyQF:8WzonIT41K0wL3PHp7XCEecB&Jgu£2dUN5";
21+
const startUpTime = new Date().getMilliseconds();
722
export const generateRandomString = (length = 10) => {
8-
const randPassword = Array(length).fill("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@£$&*:;").map(function (x) {
9-
const indexString = CryptoJS.enc.Hex.stringify(CryptoJS.lib.WordArray.random(1));
10-
const indexValue = parseInt(indexString, 16);
11-
return x[indexValue % x.length]
12-
}).join('');
13-
return randPassword;
23+
let result = '';
24+
for (let loop = 0; loop < length; loop++) {
25+
possibleCharactersSeed = shuffleCharacterAt(possibleCharactersSeed, Math.random() * possibleCharactersSeed.length); //reshuffle with browser random
26+
possibleCharactersSeed = shuffleCharacterAt(possibleCharactersSeed, new Date().getMilliseconds());//reshuffle with time
27+
possibleCharactersSeed = shuffleCharacterAt(possibleCharactersSeed, startUpTime);//reshuffle with application start time
28+
const indexValue = randomNumberGenerator(); //generate random using the crypto
29+
result += possibleCharactersSeed.charAt(indexValue % possibleCharactersSeed.length); //get the character from the seed using the crypto random
30+
possibleCharactersSeed = shuffleCharacterAt(possibleCharactersSeed, indexValue + new Date().getMilliseconds()); //reshuffle using the crypto random and time
31+
}
32+
return result;
1433
};
1534
export const encrypt = (content, password) => escape(CryptoJS.AES.encrypt(content, password).toString());
1635

test/createRandomString.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ describe("Communications between a Mobile App and a Device App", () => {
1111
const generatedStrings = [];
1212
for (let i = 0; i < 1000; i++) {
1313
const randomString1 = generateRandomString(stringLength);
14-
console.log(randomString1);
14+
1515
expect(randomString1.length).toBe(stringLength);
1616
const duplicates = generatedStrings.filter(f => f === randomString1);
17-
//expect(duplicates.length).toBe(0);
17+
expect(duplicates.length).toBe(0);
1818
generatedStrings.push(randomString1);
1919
}
2020

0 commit comments

Comments
 (0)