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

Method performEncode

src/Encoder/RailFenceCipher.js:55–82  ·  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

53 * @return {number[]|string|Uint8Array|Chain} Encoded content
54 */
55 performEncode (content) {
56 const { key: rows, offset } = this.getSettingValues()
57 const length = content.getLength()
58
59 // Cycle after which fence matrix repeats
60 const cycle = rows * 2 - 2
61
62 // Prepare an empty string for each row
63 const fenceRows = new Array(rows).fill('')
64
65 // X -->
66 // W I R E E
67 // Y E D S E E E A C
68 // | A E C V D L T N
69 // V R O F O
70 let x, y
71
72 // Go through plaintext characters
73 for (x = 0; x < length; x++) {
74 // Calculate at what y position the fence is for the current x position
75 y = rows - 1 - Math.abs(cycle / 2 - (x + offset) % cycle)
76 // Append the the current plaintext character to the respective fence row
77 fenceRows[y] += content.getCharAt(x)
78 }
79
80 // Glue fence rows together
81 return fenceRows.join('')
82 }
83
84 /**
85 * Performs decode on given content.

Callers

nothing calls this directly

Calls 4

getSettingValuesMethod · 0.80
getCharAtMethod · 0.80
joinMethod · 0.80
getLengthMethod · 0.45

Tested by

no test coverage detected