New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

complexcaptcha

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

complexcaptcha

Project that aims for generating captcha image with various customization options - for Node.JS

latest
Source
npmnpm
Version
0.9.4
Version published
Maintainers
1
Created
Source
span.code { display: inline; padding: 0; width: 100%; font-size: 13px; font-family: monospace; background-color: rgba(0,0,0,0.08); color: rgba(0,0,0,0.8); } table.optstable { font-size: 14px; } table.optstable tr:nth-child(1) td { background-color: rgba(0,0,0,0.1); }

ComplexCaptcha

Project that aims for generating captcha image with various customization options - for Node.JS

Quick look

For simply generating and saving one captcha image:

import * as fs from "fs";

import ComplexCaptcha from "complexcaptcha";

//

// generating captcha
const data = ComplexCaptcha.Generate({
    mimeType:   "image/jpeg"
});

// saving to file
fs.writeFileSync(`./captcha__${data.solution}.jpeg`), data.buffer);

Current features

  • Custom characters list (string)
  • Custom font or fonts selected randomly for each character.
  • Custom colors by creating palette of 3 colors (background, foreground, details) or providing array of palettes (random picking)
  • Displacement of individual characters on y-axis
  • Deformation of individual characters (scaling/skewing)
  • Rotation of individual characters
  • Pairing individual characters for overlaping
  • Randomly picking characters for being impostors (characters that doesn't matter in answer/solution)
  • Randomly picking font for each character.
  • Ability to disable features for individual provided fonts
  • Ability to disable characters for individual provided fonts
  • Drawing fake captcha text in background with smaller font size (dummies)
  • Drawing dirt on top of captcha (lines, xor linex/box/islands)

How to build

To build from typescript to javascript CommonJS module, just run command npm run build. You will find built code in dist directory. This command build whole module along with examples.

Examples

To run any of the examples located in examples folder:

  • Run command npm run build for building whole module along with examples.
  • Run command npm run example1 for running example1 program.
  • Look at the output in output/example1 directory.

API

Registering custom font

If You want to use custom font/s, just call ComplexCaptcha.CanvasRegisterFont function with same arguments as in registerFont function in canvas module for Node.js. Read more details here: https://github.com/Automattic/node-canvas#registerfont

ComplexCaptcha.Generate

Static function ComplexCaptcha.Generate generates captcha and returns object of GeneratedCaptcha type. It takes one argument, object of GenerateOptions type.

GeneratedCaptcha

Object of GeneratedCaptcha type contains fields:

nametypedescription
textstringContains text that is displayed on generated image. Including impostors, excluding background text/dummies.
solutionstringContains text that is answer to generated image. Against that string You want to test answer of user.
bufferBufferContains buffer that holds image data in jpeg or png format.
instructionsInstructionsContains few informations about generated captcha - useful for composing question that will be presented to user.

GeneratedCaptcha.Instructions

nametypedescription
maxLengthnumberMax length of solution can be.
omitMarkedbooleanSays that impostors/fake characters are on image among real ones and are marked in some way. (currently colored circle)
omitBackgroundTextbooleanSays that background text units are on image that aren't part of solution/answer.

GenerateOptions

Object of GenerateOptions type may contains fields:

table#generate-options tr:not(:nth-child(1)) td:nth-child(2) { text-align: center; font-weight: bold; } table#generate-options tr:not(:nth-child(1)) td:nth-child(4) { max-width: 250px; }
nameoptionaltypedefaultdescription
mimeType"image/png" | "image/jpeg"N/ADecides about mime type of image in resulted buffer.
quality✔️number0.75Decides about quality of image - works only with mimeType of "image/jpeg"
padding✔️number10Sets padding around captcha for readability.
characters✔️string"abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456790"List of characters that captcha is built from.
palette✔️Palette | Palette[]
{
    background:     [255, 255, 255],
    foreground:     [0, 0, 0],
    details:        [255, 0, 0],
}
Holds colors information, it can be one or array of palettes. With array, generating function will pick one randomly.
fontSize✔️number60Size of front characters that build captcha code.
fontInfo✔️null | FrontFontInfo | FrontFontInfo[]nullDetails that describe font usage, or array of them for random picking. See below. Value of null means defaults of Konva library will be used, probably it's about default OS font. Tip: ComplexCaptcha has few fonts preinstalled, free for comercial use.
length✔️number | [number, number]6Length of captcha code. It can be number or number range if You want it random.
charDisplacementV✔️boolean | CharDisplacementVOptionstrueValue of true means default inner options will be applied, false means feature is disabled for all fonts and characters. Value of CharDisplacementVOptions type means custom options will be applied - described below.
charRotation✔️boolean | CharRotationOptionstrueValue of true means default inner options will be applied, false means feature is disabled for all fonts and characters. Value of CharRotationOptions type means custom options will be applied - described below.
charDeformation✔️boolean | CharDeformationOptionstrueValue of true means default inner options will be applied, false means feature is disabled for all fonts and characters. Value of CharDeformationOptions type means custom options will be applied - described below.
pairing✔️boolean | PairingOptionstrueValue of true means default inner options will be applied, false means feature is disabled for all fonts and characters. Value of PairingOptions type means custom options will be applied - described below.
impostors✔️boolean | ImpostorsOptionstrueValue of true means default inner options will be applied, false means feature is disabled for all fonts and characters. Value of ImpostorsOptions type means custom options will be applied - described below.
dirt✔️boolean | DirtOptionstrueValue of true means default inner options will be applied, false means feature is disabled for all fonts and characters. Value of DirtOptions type means custom options will be applied - described below.
dummies✔️boolean | DummiesOptionstrueValue of true means default inner options will be applied, false means feature is disabled for all fonts and characters. Value of DummiesOptions type means custom options will be applied - described below.

GenerateOptions, Palette

nametypedescription
backgroundColorColor of background. Color type is equivalent to Array of 3 or 4 numbers (rgb or rgba)
foregroundColorColor of foreground elements like for example text. Color type is equivalent to Array of 3 or 4 numbers (rgb or rgba)
detailsColorColor of details, for example impostors marks. Color type is equivalent to Array of 3 or 4 numbers (rgb or rgba)

GenerateOptions, FontInfo/FrontFontInfo

Object of FontInfo/FrontFontInfo type contains information about font used to render text:

nameoptionaltypedefaultdescription
familystringN/AHolds font family name, for example: "arial". Tip: ComplexCaptcha has few preinstalled fonts that are free for commercial use. You can find theirs family names looking to assets/fonts/ directory, each folder name is family name as well.
allowRotation✔️booleantrueOnly FrontFontInfo. If false then characters with that font can't be rotated.
allowDeformation✔️booleantrueOnly FrontFontInfo. If false then characters with that font can't be deformated.
allowForPairs✔️booleantrueOnly FrontFontInfo. If false then that font can't be used for pairs - two characters joined and overlapped into one unit.
disallowCharacters✔️string""Only FrontFontInfo. String that contains characters that font is not allowed to be used for.

GenerateOptions.CharDisplacementVOptions

Object of CharDisplacementVOptions type contains information about how generator will displace vertically characters on image.

nameoptionaltypedefaultdescription
chance✔️number0.5Value from 0 (never) to 1 (always) that describes chance of that feature will be applied to character/pair.
range✔️[number, number][20, 30]Range of displacement. Displacement value on y-axis will be random in that range, direction also will be random with 50:50 chance.

GenerateOptions.CharRotationOptions

Object of CharRotationOptions type contains information about how generator will rotate characters on image.

nameoptionaltypedefaultdescription
chance✔️number0.5Value from 0 (never) to 1 (always) that describes chance of that feature will be applied to character/pair.
range✔️[number, number][-35, 35]Range of rotation. Character will be rotated by random value in that range.
excludeChars✔️false | stringfalseIf You want some characters to not be rotated at all, put them in a string of that field, or set to false if You don't want to.

GenerateOptions.CharDeformationOptions

Object of CharDeformationOptions type contains information about how generator will deformate characters on image. First, a chance test will be performed, then the deformation type will be selected randomly (scaling on x or y axis, or skewing on x or y axis)

nameoptionaltypedefaultdescription
chance✔️number0.5Value from 0 (never) to 1 (always) that describes chance of that feature will be applied to character/pair.
scxRange✔️[number, number][0.75, 3]Range of scaling on x-axis. Character will be scaled by random value in that range.
scyRange✔️[number, number][0.75, 2]Range of scaling on y-axis. Character will be scaled by random value in that range.
skxRange✔️[number, number][0.1, 0.5]Range of skewing on x-axis. Character will be skewed by random value in that range.
skyRange✔️[number, number][0.1, 0.5]Range of skewing on y-axis. Character will be skewed by random value in that range.

GenerateOptions.PairingOptions

Object of PairingOptions type contains information about how generator will pair characters rendered on image. Pairing is feature that randomly pair two characters into one unit, that will be rotated/deformated together and also both characters in pair will slightly overlapped for less readability and greater challange (can be customized).

nameoptionaltypedefaultdescription
chance✔️number0.5Value from 0 (never) to 1 (always) that describes chance of that feature will be applied to whole captcha code.
disallowed✔️false | string[]falsePairs that aren't allowed for occuring. For example ["ab", "oq"] means if that characters will be generated side by side, it isn't possible for algorithm to make them as a pair in random decision.
defaultSqueezing✔️number3Value of overlapping that works by decreasing space between characters in pair.
squeezingPairs✔️null | Record<string, number>nullSqueezing value for individual pairs. For example: {"oq": 5} means that all pairs will be squeezed by defaultSqueezing value except oq that will be squeezed by 5. Value of null means that there is no individual configs.

GenerateOptions.ImpostorsOptions

Object of ImpostorsOptions type contains information about fake characters will be placed among real ones. User is challanged to skip fake characters when rewriting captcha. Fake characters are marked by circle colored by palette.details, but modified to be semi-transparent.

nameoptionaltypedefaultdescription
chance✔️number0.5Value from 0 (never) to 1 (always) that describes chance of that feature will be applied to whole captcha code.
count✔️number | [number, number][1, 2]Count of fake characters to be placed among real characters. Range can be provided for random count.
convert✔️booleantrueValue of true means that existed characters will be transformed into impostors (solution text/final real text will be shirnked then). Value of false means that fake characters will be added to existed characters and placed randomly among them.
hintRadius✔️number20Radius of the circle that marks fake character.

GenerateOptions.DirtOptions

Object of DirtOptions type contains information about drawings that will be put on top of whole image to make it less readable for cpu. Currently only xor box and line are avaliable and are selected randomly.

nameoptionaltypedefaultdescription
chance✔️number0.5Value from 0 (never) to 1 (always) that describes chance of that feature will be applied to image.
chanceForLine✔️number0.5Value from 0 (never) to 1 (always) that describes chance of "line" dirt will be applied.
chanceForXorLine✔️number0.5Value from 0 (never) to 1 (always) that describes chance of "xorLine" dirt will be applied.
chanceForXorBox✔️number0.5Value from 0 (never) to 1 (always) that describes chance of "xorBox" dirt will be applied.
chanceForXorIsland✔️number0.5Value from 0 (never) to 1 (always) that describes chance of "xorIsland" dirt will be applied.
count✔️number | [number, number][1, 3]Count of dirt units to be drawn.
lineOpacity✔️number1Opacity of line, use values from 0 to 1.
lineWidth✔️number | [number, number]3Width of line. Thick lines can make image unreadable for humans.
lineTension✔️number0Tension of line. Value of 0 means it is disabled.
lineBezier✔️booleantrueDraw line as bezier, works only if lineTension is disabled.
xorLineWidth✔️number | [number, number]3Width of xorLine..
xorIslandArmCount✔️number | [number, number]30Vertex count of polygon, the more You will set, the more island will be rounded, 30 is fine.
xorIslandArmRadius✔️number | [number, number]40Radius/distance of vertexes from the center point of polygon. It can be multiplied by accents (randomly choosen angles where distance is higher.
xorIslandAccentCount✔️number | [number, number][1-2]Accents count. Accents are angles where radius is multiplied for making shape looking less like circle.
xorIslandAccentStrength✔️number | [number, number]1.5Accent strength. Radius = radius * xorIslandAccentStrength
xorIslandAccentSensitivity✔️number | [number, number]35Accent sensitivity. That value determines how close to accent (radius/distance of vertex from center) starts to be multiplied.

GenerateOptions.DummiesOptions

Object of DummiesOptions type contains information about smaller text units that will be put behind real text to make whole captcha less readable for cpu.

nameoptionaltypedefaultdescription
chance✔️number0.75Value from 0 (never) to 1 (always) that describes chance of that feature will be applied to image.
count✔️number | [number, number][4, 8]Count of background text units to be drawn.
length✔️number | [number, number][2, 4]Length of each background text unit.
fontPercentageSize✔️number0.4Font size for background text units. Its relative to real text font size. Keep it lower than around 0.75 for readability.
fontInfo✔️null | FontInfo | FontInfo[]nullFont information that describes what font will be used, set to array for randomness. Value of null means it will use FontInfo from real text config.
opacity✔️number0.65Opacity of background text, use values from 0 to 1.
rotation✔️false | [number, number][-15, 15]Rotation of each background text unit in degrees. Use false value for disabling.

Keywords

captcha

FAQs

Package last updated on 04 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts