]> git.codecow.com Git - nano25519.git/commitdiff
Improve regex matching.
authorChris Duncan <chris@zoso.dev>
Mon, 6 Jul 2026 07:20:21 +0000 (00:20 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 6 Jul 2026 07:20:21 +0000 (00:20 -0700)
src/lib/nano25519.ts

index 3a328e9667037a6cfd37fca5d529da743cd49fa1..3c9907509ce8185ed7a0d0f38a513be20fc4a8b1 100644 (file)
@@ -155,14 +155,13 @@ const nano25519_init = (bytes: number[]): { derive: typeof derive, sign: typeof
                        throw new TypeError(`Invalid byte length for ${name}`)
                }
                if (typeof value === 'string') {
-                       const regex = RegExp(`^[A-Fa-f0-9]{${byteLengthMin << 1},${byteLengthMax << 1}}$`)
-                       if (!regex.test(value)) {
+                       if (/[^0-9a-f]/i.test(value)) {
                                throw new TypeError(`Invalid hexadecimal characters in ${name} ${value}`)
                        }
-                       if (value.length & 1) {
-                               throw new TypeError(`Invalid hexadecimal length for ${name} ${value}`)
+                       if (value.length & 1 || value.length < (byteLengthMin << 1) || value.length > (byteLengthMax << 1)) {
+                               throw new TypeError(`Invalid hexadecimal length ${value.length} for ${name} ${value}`)
                        }
-                       value = new Uint8Array(value.match(/[A-Fa-f0-9]{2}/g)?.map(b => parseInt(b, 16)) || [])
+                       value = new Uint8Array(value.match(/[0-9a-f]{2}/gi)?.map(b => parseInt(b, 16)) || [])
                }
                if (value instanceof ArrayBuffer) {
                        value = new Uint8Array(value)
@@ -441,7 +440,7 @@ export async function run (data: Record<string, string | Uint8Array<ArrayBuffer>
        }
        try {
                const result = await dispatch(data)
-               if (typeof result === 'string' && !/^([a-f0-9]{64}|[a-f0-9]{128})$/i.test(result)) {
+               if (typeof result === 'string' && !/^([0-9a-f]{64}|[0-9a-f]{128})$/i.test(result)) {
                        throw new Error(result)
                }
                return result