-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
AlphabeticalSubstitution.js
204 lines (181 loc) · 5.91 KB
/
AlphabeticalSubstitution.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import Chain from '../Chain'
import Encoder from '../Encoder'
import ArrayUtil from '../ArrayUtil'
const meta = {
name: 'alphabetical-substitution',
title: 'Alphabetical substitution',
category: 'Ciphers',
type: 'encoder'
}
const defaultPlaintextAlphabet = 'abcdefghijklmnopqrstuvwxyz'
const defaultCiphertextAlphabet = 'zyxwvutsrqponmlkjihgfedcba'
/**
* Encoder brick for alphabetical substitution
*/
export default class AlphabeticalSubstitutionEncoder extends Encoder {
/**
* Returns brick meta.
* @return {object}
*/
static getMeta () {
return meta
}
/**
* Constructor
*/
constructor () {
super()
this.addSettings([
{
name: 'plaintextAlphabet',
type: 'text',
value: defaultPlaintextAlphabet,
uniqueChars: true,
minLength: 2,
caseSensitivity: false,
randomizable: false
},
{
name: 'ciphertextAlphabet',
type: 'text',
value: defaultCiphertextAlphabet,
uniqueChars: true,
minLength: 0,
caseSensitivity: false,
validateValue: this.validateCiphertextValue.bind(this),
randomizeValue: this.randomizeCiphertextValue.bind(this)
},
{
name: 'caseStrategy',
type: 'enum',
value: 'maintain',
elements: ['maintain', 'ignore', 'strict'],
labels: ['Maintain case', 'Ignore case', 'Strict (A ≠ a)'],
width: 6,
randomizable: false
},
{
name: 'includeForeignChars',
type: 'boolean',
label: 'Foreign Chars',
value: true,
trueLabel: 'Include',
falseLabel: 'Ignore',
width: 6,
randomizable: false
}
])
}
/**
* Performs encode or decode on given content.
* @param {Chain} content
* @param {boolean} isEncode True for encoding, false for decoding
* @return {number[]|string|Uint8Array|Chain} Resulting content
*/
performTranslate (content, isEncode) {
const caseStrategy = this.getSettingValue('caseStrategy')
const includeForeignChars = this.getSettingValue('includeForeignChars')
// Retrieve alphabets
let sourceAlphabet = this.getSettingValue('plaintextAlphabet')
let destAlphabet =
this.getSettingValue('ciphertextAlphabet').extend(sourceAlphabet)
// Swap source and destination alphabets if decoding
if (!isEncode) {
;[destAlphabet, sourceAlphabet] = [sourceAlphabet, destAlphabet]
}
// Prepare uppercase alphabets if needed by case strategy
let uppercaseSourceAlphabet, uppercaseDestAlphabet
if (caseStrategy !== 'strict') {
sourceAlphabet = sourceAlphabet.toLowerCase()
destAlphabet = destAlphabet.toLowerCase()
uppercaseSourceAlphabet = sourceAlphabet.toUpperCase()
uppercaseDestAlphabet = destAlphabet.toUpperCase()
}
const codePoints = content.getCodePoints()
const result = new Array(codePoints.length).fill(0)
let codePoint, charIndex, uppercase
let j = 0
// Map each character from source to destination alphabet
for (let i = 0; i < codePoints.length; i++) {
codePoint = codePoints[i]
// Match alphabet character
charIndex = sourceAlphabet.indexOfCodePoint(codePoint)
uppercase = false
// Match uppercase alphabet character (depending on case strategy)
if (charIndex === -1 && caseStrategy !== 'strict') {
charIndex = uppercaseSourceAlphabet.indexOfCodePoint(codePoint)
uppercase = true
}
if (charIndex !== -1) {
// Put char index back into a character following the case strategy
if (caseStrategy === 'maintain' && uppercase) {
result[j++] = uppercaseDestAlphabet.getCodePointAt(charIndex)
} else {
result[j++] = destAlphabet.getCodePointAt(charIndex)
}
} else if (includeForeignChars) {
result[j++] = codePoint
}
}
return result.slice(0, j)
}
/**
* Triggered when a setting field has changed.
* @protected
* @param {Field} setting Sender setting field
* @param {mixed} value New field value
*/
settingValueDidChange (setting, value) {
switch (setting.getName()) {
case 'plaintextAlphabet':
// The validity of the ciphertext alphabet depends
// on the plaintext alphabet
this.getSetting('ciphertextAlphabet').revalidateValue()
break
case 'caseStrategy':
this.getSetting('plaintextAlphabet')
.setCaseSensitivity(value === 'strict')
this.getSetting('ciphertextAlphabet')
.setCaseSensitivity(value === 'strict')
break
}
}
/**
* Validates ciphertext setting value.
* @protected
* @param {mixed} rawValue Raw value
* @param {Setting} setting Sender setting
* @return {boolean|object}
*/
validateCiphertextValue (rawValue, setting) {
// The ciphertext alphabet depends on the plaintext alphabet
if (!this.isSettingValid('plaintextAlphabet')) {
return false
}
const plaintextAlphabet = this.getSettingValue('plaintextAlphabet')
if (rawValue.getLength() > plaintextAlphabet.getLength()) {
return {
key: 'alphabetContainsUnusedCharacters',
message: `The ciphertext alphabet is longer than the plaintext alphabet`
}
}
return true
}
/**
* Generates a random ciphertext setting value.
* @protected
* @param {Random} random Random instance
* @param {Setting} setting Setting instance
* @return {Chain|null} Random ciphertext setting value
*/
randomizeCiphertextValue (random, setting) {
// The ciphertext alphabet value can only be randomized when having
// a valid plaintext alphabet
if (!this.getSetting('plaintextAlphabet').isValid()) {
return null
}
// Shuffle the plaintext alphabet to compose a ciphertext alphabet
const plaintextAlphabet = this.getSettingValue('plaintextAlphabet')
return Chain.wrap(ArrayUtil.shuffle(plaintextAlphabet.getCodePoints()))
}
}