cryptpadtab

encrypted notes in a browser tab  https://p.ce9e.org/cryptpadtab/
git clone https://git.ce9e.org/cryptpadtab.git

commit
c80c87e56c3d59edee735412504b6f4f107dd8e3
parent
2acd77b2f05b4b8f6aa97fb7d161880249569fbf
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-05-29 05:12
base64 encoding

Diffstat

A static/base64.js 33 +++++++++++++++++++++++++++++++++
M static/main.js 18 ++++++++++++++++--

2 files changed, 49 insertions, 2 deletions


diff --git a/static/base64.js b/static/base64.js

@@ -0,0 +1,33 @@
   -1     1 var table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
   -1     2 
   -1     3 export var encode = function(bytes) {
   -1     4 	var result = '';
   -1     5 	var push = x => result += table.charAt(x & 0b111111);
   -1     6 	for (let i = 0; i < bytes.length; i += 3) {
   -1     7 		var b1 = bytes[i];
   -1     8 		var b2 = bytes[i + 1];
   -1     9 		var b3 = bytes[i + 2];
   -1    10 
   -1    11 		push(b1 >> 2);
   -1    12 		push((b1 << 4) | (b2 >> 4));
   -1    13 		if (!isNaN(b2)) push((b2 << 2) | (b3 >> 6));
   -1    14 		if (!isNaN(b3)) push(b3);
   -1    15 	}
   -1    16 	return result;
   -1    17 };
   -1    18 
   -1    19 export var decode = function(string) {
   -1    20 	var result = [];
   -1    21 	for (let i = 0; i < string.length; i += 4) {
   -1    22 		var e1 = table.indexOf(string[i]);
   -1    23 		var e2 = table.indexOf(string[i + 1]);
   -1    24 		var e3 = table.indexOf(string[i + 2]);
   -1    25 		var e4 = table.indexOf(string[i + 3]);
   -1    26 
   -1    27 		result.push((e1 << 2) | (e2 >> 4));
   -1    28 		if (e3 !== -1) result.push((e2 << 4) | (e3 >> 2));
   -1    29 		if (e4 !== -1) result.push((e3 << 6) | e4);
   -1    30 	}
   -1    31 	// will truncate the values automatically
   -1    32 	return new Uint8Array(result);
   -1    33 };

diff --git a/static/main.js b/static/main.js

@@ -1,7 +1,21 @@
   -1     1 import * as base64 from './base64.js';
   -1     2 
   -1     3 var encode = function(text) {
   -1     4 	var encoder = new TextEncoder();
   -1     5 	var bytes = encoder.encode(text);
   -1     6 	return base64.encode(bytes);
   -1     7 };
   -1     8 
   -1     9 var decode = function(string) {
   -1    10 	var decoder = new TextDecoder();
   -1    11 	var bytes = base64.decode(string);
   -1    12 	return decoder.decode(bytes);
   -1    13 };
   -1    14 
    1    15 var textarea = document.querySelector('textarea');
    2    16 
    3    -1 textarea.value = location.hash.substr(1);
   -1    17 textarea.value = decode(location.hash.substr(1));
    4    18 
    5    19 textarea.addEventListener('input', event => {
    6    -1 	history.replaceState(null, '', '#' + textarea.value);
   -1    20 	history.replaceState(null, '', '#' + encode(textarea.value));
    7    21 });