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/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": { 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