cryptpadtab

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

commit
98432234c0e8e48a8833c7ec19fcff92b32f0604
parent
2a8312a23f50ffd79b8a8107b18eed0bb5e95a97
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-12-25 16:07
use native base64 encoding

Diffstat

M static/crypto.js 38 ++------------------------------------

1 files changed, 2 insertions, 36 deletions


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

@@ -1,37 +1,3 @@
    1    -1 var b64table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
    2    -1 
    3    -1 export function b64encode(bytes) {
    4    -1 	var result = '';
    5    -1 	var push = x => result += b64table.charAt(x & 0b111111);
    6    -1 	for (let i = 0; i < bytes.length; i += 3) {
    7    -1 		var b1 = bytes[i];
    8    -1 		var b2 = bytes[i + 1];
    9    -1 		var b3 = bytes[i + 2];
   10    -1 
   11    -1 		push(b1 >> 2);
   12    -1 		push((b1 << 4) | (b2 >> 4));
   13    -1 		if (!isNaN(b2)) push((b2 << 2) | (b3 >> 6));
   14    -1 		if (!isNaN(b3)) push(b3);
   15    -1 	}
   16    -1 	return result;
   17    -1 }
   18    -1 
   19    -1 export function b64decode(string) {
   20    -1 	var result = [];
   21    -1 	for (let i = 0; i < string.length; i += 4) {
   22    -1 		var e1 = b64table.indexOf(string[i]);
   23    -1 		var e2 = b64table.indexOf(string[i + 1]);
   24    -1 		var e3 = b64table.indexOf(string[i + 2]);
   25    -1 		var e4 = b64table.indexOf(string[i + 3]);
   26    -1 
   27    -1 		result.push((e1 << 2) | (e2 >> 4));
   28    -1 		if (e3 !== -1) result.push((e2 << 4) | (e3 >> 2));
   29    -1 		if (e4 !== -1) result.push((e3 << 6) | e4);
   30    -1 	}
   31    -1 	// will truncate the values automatically
   32    -1 	return new Uint8Array(result);
   33    -1 }
   34    -1 
   35     1 async function applyStream(bytes, stream) {
   36     2 	var readableStream = new ReadableStream({
   37     3 		start(controller) {
@@ -89,12 +55,12 @@ export async function encodeText(text, key) {
   89    55 	if (key) {
   90    56 		bytes = await encrypt(bytes, key);
   91    57 	}
   92    -1 	return b64encode(bytes);
   -1    58 	return bytes.toBase64().replace(/\+/g, '-').replace(/\//g, '_');
   93    59 }
   94    60 
   95    61 export async function decodeText(string, key) {
   96    62 	var decoder = new TextDecoder();
   97    -1 	var bytes = b64decode(string);
   -1    63 	var bytes = Uint8Array.fromBase64(string.replace(/-/g, '+').replace(/_/g, '/'));
   98    64 	if (key) {
   99    65 		bytes = await decrypt(bytes, key);
  100    66 	}