-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
BifidCipher.js
117 lines (104 loc) · 3.08 KB
/
BifidCipher.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
import PolybiusSquareEncoder from './PolybiusSquare'
import Encoder from '../Encoder'
const meta = {
name: 'bifid-cipher',
title: 'Bifid cipher',
category: 'Polybius square ciphers',
type: 'encoder'
}
/**
* The Bifid alphabet (without the letter J)
* @type {string}
*/
const alphabet = 'abcdefghiklmnopqrstuvwxyz'
/**
* Encoder brick for Bifid cipher encryption and decryption
*/
export default class BifidCipherEncoder extends Encoder {
/**
* Returns brick meta.
* @return {object}
*/
static getMeta () {
return meta
}
/**
* Constructor
*/
constructor () {
super()
this.addSetting({
name: 'key',
type: 'text',
value: '',
uniqueChars: true,
minLength: 0,
caseSensitivity: false
})
// Create internal Polybius square encoder instance
this._polybiusSquare = new PolybiusSquareEncoder()
this._polybiusSquare.setSettingValues({
alphabet: alphabet,
rows: '12345',
columns: '12345',
separator: '',
caseSensitivity: false,
includeForeignChars: false
})
}
/**
* Performs encode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain} Encoded content
*/
async performEncode (content) {
// Encode content to coordinates using Polybius square
// As I and J shares the same alphabet position, replace all J chars
const sourcePolybius = await this._polybiusSquare.encode(
content.getString().replace(/j/gi, 'i'))
// Transpose coordinates such that the first coordinates (X) appear
// combined before the second coordinates (Y)
const polybiusLength = sourcePolybius.getLength()
const resultPolybius = new Array(polybiusLength)
for (let i = 0; i < polybiusLength / 2; i++) {
resultPolybius[i] =
sourcePolybius.getCodePointAt(i * 2)
resultPolybius[polybiusLength / 2 + i] =
sourcePolybius.getCodePointAt(i * 2 + 1)
}
return this._polybiusSquare.decode(resultPolybius)
}
/**
* Performs decode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain} Decoded content
*/
async performDecode (content) {
const sourcePolybius = await this._polybiusSquare.encode(content)
const polybiusLength = sourcePolybius.getLength()
// Transpose coordinates back
const resultPolybius = new Array(polybiusLength)
for (let i = 0; i < polybiusLength / 2; i++) {
resultPolybius[i * 2] =
sourcePolybius.getCodePointAt(i)
resultPolybius[i * 2 + 1] =
sourcePolybius.getCodePointAt(polybiusLength / 2 + i)
}
return this._polybiusSquare.decode(resultPolybius)
}
/**
* Triggered when a setting field has changed.
* @param {Field} setting Sender setting field
* @param {mixed} value New field value
*/
settingValueDidChange (setting, value) {
switch (setting.getName()) {
case 'key':
// Derive Polybius square alphabet from the key setting
this._polybiusSquare.setSettingValue('alphabet', value.extend(alphabet))
break
}
}
}