vanillaaont
Version:
simple all-or-nothing transform in nothing but vanilla js
104 lines (96 loc) • 2.92 kB
JavaScript
;
{
const vanillaaont = {
T,TU
};
const alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const dec = "0123456789";
const twomore = "/+";
const alphabet = alph + dec + alph.toLowerCase() + twomore;
const pad = new Array(33).join('0');
const initial_rotate = 13;
const rotates = alphabet.
split('').
reduce( (m,c,i) => ( m[c] = i+1, m ), {} );
try { Object.assign( self, { vanillaaont } ); } catch(e) { module.exports = vanillaaont; }
function t( m ) {
m = Array.from(m);
m.reverse();
const b = Array.from(m);
let i = -1;
while( true ) {
const mc = b[i+1];
const rotate_amount = i >= 0 ? rotates[mc] : initial_rotate;
if ( ! rotate_amount ) {
throw new TypeError(`This string is not in base 64. We only know how to interpret these symbols: ${alphabet}. You provided this: "${mc}"` );
}
const remaining = Math.min(m.length - (i+2),alphabet.length);
if ( i+2 < m.length - 1 ) {
const rot_sec = rotate_left_by( (rotate_amount) % remaining, b.slice( i+2, i+67 ) );
b.splice( i+2, 65, ...rot_sec );
} else {
break;
}
i+=1;
}
return b.join('');
}
function T( m ) {
m = Array.from(m);
let head, tail;
let tail_start = m.indexOf('=');
if ( tail_start >= 0 ) {
head = m.slice(0, tail_start);
tail = m.slice(tail_start);
} else {
head = m;
tail = [];
}
head.unshift(...pad);
head.push(...pad);
m = head;
let r = t(t(t(t(m))));
r = r + tail.join('');
return r;
}
function tu(m) {
const b = Array.from(m);
let j = m.length-1;
while( j > 0 ) {
const remaining = Math.min(m.length-j,alphabet.length);
const mc = b[j-1];
const rotate_amount = j >= 2 ? rotates[mc] : initial_rotate;
if ( ! rotate_amount ) {
throw new TypeError(`This string is not in base 64. We only know how to interpret these symbols: ${alphabet}. You provided this: "${mc}"` );
}
const rot_sec = rotate_right_by( (rotate_amount) % remaining, b.slice( j, j+65 ) );
b.splice( j, 65, ...rot_sec );
j -= 1;
}
b.reverse();
return b.join('');
}
function TU( m ) {
m = Array.from(m);
const tail_count = m.filter( mc => mc == "=" ).length;
if ( tail_count ) {
m = m.slice(0,m.indexOf('='));
}
const b = tu(tu(tu(tu(m)))).split('');
const result = b.slice(32,-32);
result.push( ...Array(tail_count+1).join('=') );
return result.join('');
}
function rotate_right_by( amount, array ) {
let head = array.slice( array.length - amount );
const tail = array.slice(0,array.length - amount);
head = head.concat(tail);
return head;
}
function rotate_left_by( amount, array ) {
let head = array.slice( amount );
const tail = array.slice(0,amount);
head = head.concat(tail);
return head;
}
}