-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
105 lines (93 loc) · 2.99 KB
/
index.ts
File metadata and controls
105 lines (93 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const re = /[a-zA-Z0-9]/
function match(x: string): boolean {
return x.match(re) !== null
}
function countMatches(xs: string): number {
let n = 0
for (const x of xs) if (match(x)) n++
return n
}
export type Options = {
/**
* A random generator. Should fill the array with uniformly
* random bytes.
*/
getRandomValues: ((a: Uint8Array) => void),
/**
* If sensitive, upper-case letters will not be changed to
* lower case, and vice versa.
*
* Default: 'sensitive'
*/
letterCase?: 'sensitive' | 'insensitive',
/**
* If sensitive, letters A-F will stay within that range. (This
* prevents hexadecimal strings from becoming not hexadecimal.)
*
* Default: 'sensitive'
*/
hexLetters?: 'sensitive' | 'insensitive',
}
export function muddle(input: string, options: Options): string {
const letterCase = options.letterCase ?? 'sensitive'
const hexLetters = options.hexLetters ?? 'sensitive'
let n = countMatches(input)
if (n === 0) return input
const entropy = new Uint8Array(n)
options.getRandomValues(entropy)
let i = 0
const output: Array<string> = []
for (const x of input) {
const y = x.codePointAt(0) as number
if (y < 48) output.push(x)
else if (y <= 57)
output.push(String.fromCodePoint(48 + (entropy.at(i++) as number % 10)))
else if (y < 65) output.push(x)
else if (hexLetters === 'sensitive' && y <= 70) {
if (letterCase === 'sensitive')
output.push(String.fromCodePoint(65 + (entropy.at(i++) as number % 6)))
else {
const offset = entropy.at(i++) as number % (2 * 6)
if (offset < 6)
output.push(String.fromCodePoint(65 + offset))
else
output.push(String.fromCodePoint(97 + (offset - 6)))
}
}
else if (y <= 90) {
if (letterCase === 'sensitive')
output.push(String.fromCodePoint(65 + (entropy.at(i++) as number % 26)))
else {
const offset = entropy.at(i++) as number % (2 * 26)
if (offset < 26)
output.push(String.fromCodePoint(65 + offset))
else
output.push(String.fromCodePoint(97 + (offset - 26)))
}
}
else if (y < 97) output.push(x)
else if (hexLetters === 'sensitive' && y <= 102) {
if (letterCase === 'sensitive')
output.push(String.fromCodePoint(97 + (entropy.at(i++) as number % 6)))
else {
const offset = entropy.at(i++) as number % (2 * 6)
if (offset < 6)
output.push(String.fromCodePoint(65 + offset))
else
output.push(String.fromCodePoint(97 + (offset - 6)))
}
}
else if (y <= 122)
if (letterCase === 'sensitive')
output.push(String.fromCodePoint(97 + (entropy.at(i++) as number % 26)))
else {
const offset = entropy.at(i++) as number % (2 * 26)
if (offset < 26)
output.push(String.fromCodePoint(65 + offset))
else
output.push(String.fromCodePoint(97 + (offset - 26)))
}
else output.push(x)
}
return output.join('')
}