From c4c82cb8f8e4e6c551e4741647905954cad99ad7 Mon Sep 17 00:00:00 2001 From: "Mr. Happiness" Date: Sun, 31 Mar 2024 13:31:44 +0330 Subject: [PATCH 1/2] feat: add custom letters parameter to captcha function --- lib/captcha.js | 10 +++++----- test/index.js | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/captcha.js b/lib/captcha.js index 18093c2..aae5e82 100644 --- a/lib/captcha.js +++ b/lib/captcha.js @@ -126,7 +126,7 @@ function filter(im) { om.copy(im) } -async function captcha(size) { +async function captcha(letters,size) { const rb = await urandom(size + 200 + 100 * 4 + 1 + 1) const l = Buffer.alloc(size) const swr = Buffer.alloc(200) @@ -148,9 +148,9 @@ async function captcha(size) { let p = 30 for (let n = 0; n < size; n++) { - l[n] %= 25 + l[n] %= letters.length -1; p = letter(l[n], p, im, swr, s1, s2) - l[n] = LETTERS.charCodeAt(l[n]) + l[n] = letters.charCodeAt(l[n]) } line(im, swr, s1) @@ -192,9 +192,9 @@ function makegif(im, gif, style) { gif.fill('\x01\x11\x00;', GIF_SIZE - 4) } -async function generate({ size = SIZE, style = -1 } = {}) { +async function generate({ size = SIZE, style = -1, letters = LETTERS } = {}) { const gif = Buffer.alloc(GIF_SIZE) - const { im, l } = await captcha(size) + const { im, l } = await captcha(letters,size) makegif(im, gif, style) return { diff --git a/test/index.js b/test/index.js index a448aa6..1fd3ea1 100644 --- a/test/index.js +++ b/test/index.js @@ -7,3 +7,9 @@ test('should return an object and include token and buffer', async t => { t.is(token.length, 5) t.is(buffer.length, 17646) }) +test('random numbers should be from letters', async t => { + const { token } = await captcha({letters:'1'}) + console.log(token); + t.is(token, '11111') + }) + \ No newline at end of file From a1a2e0483eb0e4b053a9bb3204edafbf69046af3 Mon Sep 17 00:00:00 2001 From: "Mr. Happiness" Date: Sun, 31 Mar 2024 13:32:11 +0330 Subject: [PATCH 2/2] v0.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e758802..eda1440 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "trek-captcha", - "version": "0.4.0", + "version": "0.5.0", "description": "A Lightweight Pure JavaScript Captcha for Node.js. No C/C++, No ImageMagick, No canvas.", "repository": "trekjs/captcha", "author": {