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

Method performEncode

src/Encoder/Base32.js:112–158  ·  view source on GitHub ↗

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

(content)

Source from the content-addressed store, hash-verified

110 * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
111 */
112 performEncode (content) {
113 const { alphabet, padding } = this.getVariantOptions()
114 const alphabetCodePoints = alphabet.getCodePoints()
115
116 // Prepare input and output arrays
117 const input = content.getBytes()
118 const inputLength = input.length
119 const resultLength = Math.ceil(inputLength / 5) * 8
120 const result = new Array(resultLength)
121 let j = 0
122
123 let shift = 3
124 let carry = 0
125 let byte, index
126
127 // Go through every input byte
128 for (let i = 0; i < inputLength; i++) {
129 byte = input[i]
130
131 index = carry | (byte >> shift)
132 result[j++] = alphabetCodePoints[index & 0x1f]
133
134 if (shift > 5) {
135 shift -= 5
136 index = byte >> shift
137 result[j++] = alphabetCodePoints[index & 0x1f]
138 }
139
140 shift = 5 - shift
141 carry = byte << shift
142 shift = 8 - shift
143 }
144
145 if (shift !== 3) {
146 result[j++] = alphabetCodePoints[carry & 0x1f]
147 }
148
149 // Finalize result padding
150 if (padding !== null) {
151 while (j < resultLength) {
152 result[j++] = padding
153 }
154 return result
155 } else {
156 return result.slice(0, j)
157 }
158 }
159
160 /**
161 * Performs decode on given content.

Callers

nothing calls this directly

Calls 3

getVariantOptionsMethod · 0.95
getCodePointsMethod · 0.80
getBytesMethod · 0.80

Tested by

no test coverage detected