Skip to content
This repository was archived by the owner on May 2, 2025. It is now read-only.

Commit 6337d6b

Browse files
committed
Replace Math.random by window.crypto.
1 parent 73538ab commit 6337d6b

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

ui/src/components/AESKeyField.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,11 @@ class AESKeyField extends Component {
165165
}
166166

167167
randomKey = () => {
168-
let key = "";
169-
const possible = 'abcdef0123456789';
168+
let cryptoObj = window.crypto || window.msCrypto;
169+
let b = new Uint8Array(16);
170+
cryptoObj.getRandomValues(b);
170171

171-
for(let i = 0; i < 32; i++){
172-
key += possible.charAt(Math.floor(Math.random() * possible.length));
173-
}
172+
let key = Buffer.from(b).toString('hex');
174173
this.setState({
175174
value: key,
176175
});

ui/src/components/EUI64Field.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@ class EUI64Field extends Component {
7676
}
7777

7878
randomKey = () => {
79-
let key = "";
80-
const possible = 'abcdef0123456789';
79+
let cryptoObj = window.crypto || window.msCrypto;
80+
let b = new Uint8Array(8);
81+
cryptoObj.getRandomValues(b);
8182

82-
for(let i = 0; i < 16; i++){
83-
key += possible.charAt(Math.floor(Math.random() * possible.length));
84-
}
83+
let key = Buffer.from(b).toString('hex');
8584
this.setState({
8685
value: key,
8786
});

ui/src/views/multicast-groups/MulticastGroupForm.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,19 @@ class MulticastGroupForm extends FormComponent {
4646
}
4747

4848
getRandomKey(len) {
49-
let key = "";
50-
const possible = 'abcdef0123456789';
49+
let cryptoObj = window.crypto || window.msCrypto;
50+
let b = new Uint8Array(len);
51+
cryptoObj.getRandomValues(b);
5152

52-
for(let i = 0; i < len; i++){
53-
key += possible.charAt(Math.floor(Math.random() * possible.length));
54-
}
55-
56-
return key;
53+
return Buffer.from(b).toString('hex');
5754
}
5855

5956
getRandomMcAddr = (cb) => {
60-
cb(this.getRandomKey(8));
57+
cb(this.getRandomKey(4));
6158
}
6259

6360
getRandomSessionKey = (cb) => {
64-
cb(this.getRandomKey(32));
61+
cb(this.getRandomKey(16));
6562
}
6663

6764

0 commit comments

Comments
 (0)