Skip to content

Commit 7073460

Browse files
committed
refactor
1 parent 6c73931 commit 7073460

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

index.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const base64UrlRegex = /-|_/
21
const encryptionAlgorithm = "AES-GCM"
32
const ivBytesLength = 12
43

@@ -111,9 +110,31 @@ export async function decryptTextSymmetrically(
111110
textDecoder = new TextDecoder()
112111
) {
113112

113+
/**
114+
* @type {(boolean|undefined)}
115+
*/
116+
let urlSafe
117+
118+
let i = 0
119+
120+
do {
121+
switch (ciphertext[i]) {
122+
case "-":
123+
case "_":
124+
urlSafe = true
125+
break
126+
case "+":
127+
case "/":
128+
urlSafe = false
129+
break
130+
default:
131+
i++
132+
}
133+
} while (urlSafe === undefined && i < ciphertext.length)
134+
114135
const data = Uint8Array.fromBase64(
115136
ciphertext,
116-
base64UrlRegex.test(ciphertext) ? base64UrlOptions : undefined
137+
urlSafe ? base64UrlOptions : undefined
117138
)
118139

119140
return (

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "singlecrypt-text",
3-
"version": "4.0.2",
3+
"version": "4.0.3",
44
"author": "Stefan Samson <ss42701@outlook.com> (https://ssbit01.github.io/)",
55
"repository": "github:SSbit01/singlecrypt-text",
66
"main": "index.js",

0 commit comments

Comments
 (0)