const createKeyGenerator1 = (groupSize, groupCount) => {
const dictionary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
const length = groupSize * groupCount;
const group = new RegExp(`.{${groupSize}}`, 'g');
return () => {
const values = new Uint32Array(length);
crypto.getRandomValues(values);
const chars = [...values].map(value => dictionary[value % dictionary.length]);
const key = '_'
.repeat(length)
.replace(/\w/g, (match, index) => chars[index])
.match(group)
.join('-');
return key;
};
};
const createKeyGenerator2 = (groupSize, groupCount) => {
const dictionary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
const length = groupSize * groupCount;
return () => {
const values = new Uint32Array(length);
crypto.getRandomValues(values);
const chars = [...values].map(value => dictionary[value % dictionary.length]);
const key = new Array(groupCount)
.fill(null)
.map((_, index) => {
const offset = index * groupSize;
return chars
.slice(offset, offset + groupSize)
.join('')
})
.join('-');
return key;
};
};
const createKey1 = createKeyGenerator1(4, 3);
const createKey2 = createKeyGenerator2(4, 3);