base85
This tag encodes and decodes base85 encoding
Created by: hackvertor
Installed 1 times
Category: Convert
Created on: Thursday, October 10, 2024 at 11:47:27 AM
Updated on: Thursday, October 10, 2024 at 11:47:27 AM
Tag arguments
[]
Code
class base85 {
encode(input) {
const encoded = [];
const chunkSize = 4;
for (let i = 0; i < input.length; i += chunkSize) {
const chunk = input.substr(i, chunkSize).padEnd(chunkSize, "\0");
let value = 0;
for (let j = 0; j < chunk.length; j++) {
value = (value << 8) + chunk.charCodeAt(j);
}
for (let j = 4; j >= 0; j--) {
encoded.push(String.fromCharCode(33 + ((value / 85 ** j) % 85)));
}
}
return encoded.join("");
}
decode(input) {
const decoded = [];
const chunkSize = 5;
for (let i = 0; i < input.length; i += chunkSize) {
const chunk = input.substr(i, chunkSize).padEnd(chunkSize, "u");
let value = 0;
for (let j = 0; j < chunk.length; j++) {
value = value * 85 + (chunk.charCodeAt(j) - 33);
}
for (let j = 3; j >= 0; j--) {
decoded.push(String.fromCharCode((value >> (8 * j)) & 255));
}
}
return decoded.join("").replace(/\0+$/, "");
}
matches(input) {
const base85Regex = /^[\x21-\x75]{10,}$/;
const result = base85Regex.exec(input);
return result ? result : null;
}
}