Skip to content

Commit 91f9597

Browse files
author
liw003
committed
x
1 parent 24478bc commit 91f9597

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

src/ec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,21 @@ class EC{
121121
return new Uint8Array(await subtle.exportKey('raw',result))
122122
}
123123

124-
base64Encode(arr:Uint8Array):string{
125-
return base64js.fromByteArray(arr,76)
124+
base64Encode(arr:Uint8Array,urlsafe = 0):string{
125+
let r = base64js.fromByteArray(arr,76)
126+
if (urlsafe) {
127+
r = r.replace(/\+/g, '-')
128+
r = r.replace(/\//g, '_')
129+
r = r.replace(/=/g, '')
130+
}
131+
return r
126132
}
127-
base64Decode(str:string):Uint8Array{
133+
base64Decode(str:string,urlsafe = 0):Uint8Array{
134+
if (urlsafe) {
135+
str = str.replace(/-/g, '+')
136+
str = str.replace(/_/g, '/')
137+
str = str.replace(/=/g, '')
138+
}
128139
return base64js.toByteArray(str)
129140
}
130141
async generateNewKeyPair(seckey ?:string): Promise<{private:string,public:string}>{

src/index.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
(async function () {
33

4+
let g_urlsafe = 1 as 1|0
5+
46
interface InputData {
57
prefix: string;
68
pubkey: string;
@@ -65,7 +67,7 @@
6567
try {
6668
let te = new TextEncoder();
6769
let enc = await ec.encrypt(p, te.encode(text));
68-
setCipherText(ec.base64Encode(enc));
70+
setCipherText(ec.base64Encode(enc,g_urlsafe));
6971
return true;
7072
} catch (error) {
7173
setErrMsg(error as string);
@@ -111,7 +113,7 @@
111113
return;
112114
}
113115
try {
114-
let arr = ec.base64Decode(base64);
116+
let arr = ec.base64Decode(base64,g_urlsafe);
115117
let dec = await ec.decrypt(p, arr);
116118
let te = new TextDecoder();
117119
setPlainText(te.decode(dec));
@@ -259,6 +261,7 @@
259261

260262

261263
let msg = `
264+
v2
262265
${G_Input?.prefix || ""} ${newLine}
263266
备份时间:${beijingtime()} ${newLine}
264267
@@ -297,6 +300,7 @@
297300

298301

299302
let msg = `
303+
v2-urlsafe
300304
${G_Input?.prefix || ""} ${newLine}
301305
备份时间:${beijingtime()} ${newLine}
302306
@@ -334,14 +338,12 @@
334338
let jsonstring = JSON.stringify(s);
335339
let arr = new TextEncoder().encode(jsonstring);
336340
let dataBuff = await ec.encrypt(webPublic, arr);
337-
let data = ec.base64Encode(dataBuff);
341+
let data = ec.base64Encode(dataBuff,1);
338342

339-
// Convert to URL-safe base64 for data2
340-
let data2 = regularBase64ToUrlSafe(data);
341343

342344
let bookmark = `${location.origin}${
343345
location.pathname
344-
}?t=${new Date().toISOString()}#&data2=${encodeURIComponent(data2)}`;
346+
}?t=${new Date().toISOString()}#&data2=${encodeURIComponent(data)}`;
345347

346348
let a = document.createElement("a");
347349
a.innerText = bookmark;
@@ -403,21 +405,6 @@
403405
btime.innerText = `Package:${__BUILD_MOD__} \n ${__BUILD_TIME__} `;
404406

405407

406-
// Convert URL-safe base64 to regular base64
407-
function urlSafeBase64ToRegular(urlSafeBase64: string): string {
408-
return urlSafeBase64
409-
.replace(/-/g, '+')
410-
.replace(/_/g, '/');
411-
}
412-
413-
// Convert regular base64 to URL-safe base64
414-
function regularBase64ToUrlSafe(regularBase64: string): string {
415-
return regularBase64
416-
.replace(/\+/g, '-')
417-
.replace(/\//g, '_')
418-
.replace(/=/g, ''); // Remove padding
419-
}
420-
421408
(async function initDefaultValues() {
422409

423410
console.log(location.hash);
@@ -428,7 +415,10 @@
428415

429416
// Use data2 if available, otherwise use data
430417
if (data2) {
431-
data = urlSafeBase64ToRegular(data2);
418+
data = data2
419+
}
420+
if (data && !data2) {
421+
g_urlsafe = 0
432422
}
433423

434424
if (!data) {
@@ -438,7 +428,7 @@
438428
let ttlog = console.log;
439429
ttlog({ webPrivate, webPublic });
440430

441-
let plainBf = await ec.decrypt(webPrivate, ec.base64Decode(data));
431+
let plainBf = await ec.decrypt(webPrivate, ec.base64Decode(data,g_urlsafe));
442432
let plain = new TextDecoder().decode(plainBf);
443433
ttlog(plain);
444434

src/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ declare interface EC{
6565
decrypt(privateKeyB64:string,data:Uint8Array):Promise<Uint8Array>
6666
generateNewKeyPair(seckey ?:string): Promise<{private:string,public:string}>
6767

68-
base64Encode(arr:Uint8Array):string
69-
base64Decode(str:string):Uint8Array
68+
base64Encode(arr:Uint8Array,urlsafe ?:1|0):string
69+
base64Decode(str:string,urlsafe ?:1|0):Uint8Array
7070
}
7171
declare function initEC():EC;
7272

0 commit comments

Comments
 (0)