MCPcopy Create free account
hub / github.com/cryptii/cryptii / performEncode

Method performEncode

src/Encoder/BaconCipher.js:112–143  ·  view source on GitHub ↗

* Performs encode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain} Encoded content

(content)

Source from the content-addressed store, hash-verified

110 * @return {number[]|string|Uint8Array|Chain} Encoded content
111 */
112 performEncode (content) {
113 const { alphabet, codes, codeLength } = this._getVariant()
114 const aChar = this.getSettingValue('aMark').getCharAt(0)
115 const bChar = this.getSettingValue('bMark').getCharAt(0)
116
117 // Prepare content
118 const lowercaseContent = content.toLowerCase()
119
120 // Map input to alphabet codes
121 let result = ''
122 let codeIndex
123
124 for (let i = 0; i < lowercaseContent.getLength(); i++) {
125 codeIndex = alphabet.indexOf(lowercaseContent.getCharAt(i))
126 if (codeIndex !== -1) {
127 result += codes[codeIndex]
128 }
129 }
130
131 // Map code marks, if not using the defaults
132 if (aChar !== 'a' || bChar !== 'b') {
133 result = result
134 .split('')
135 .map(mark => mark === 'a' ? aChar : bChar)
136 .join('')
137 }
138
139 // Chunk codes separated by a whitespace
140 return ArrayUtil.chunk(Chain.wrap(result).getChars(), codeLength)
141 .map(slice => slice.join(''))
142 .join(' ')
143 }
144
145 /**
146 * Performs decode on given content.

Callers

nothing calls this directly

Calls 11

_getVariantMethod · 0.95
getCharAtMethod · 0.80
getSettingValueMethod · 0.80
toLowerCaseMethod · 0.80
indexOfMethod · 0.80
joinMethod · 0.80
splitMethod · 0.80
getCharsMethod · 0.80
wrapMethod · 0.80
getLengthMethod · 0.45
chunkMethod · 0.45

Tested by

no test coverage detected