caprs
A Rust library for image-based CAPTCHAs. Generate a challenge, send the images to the client, and check the response against the answer on the server.
| Mode | Client interaction | Library output |
|---|---|---|
| Text click | Click the letters, characters, or short labels shown in the hint, in order | JPEG main image and PNG hint |
| Shape click | Click the shapes shown in the hint, in order | JPEG main image and PNG hint |
| Slide | Move a piece horizontally into its matching hole | JPEG main image and transparent PNG tile |
| Drag and drop | Move a piece along both axes into its matching hole | JPEG main image and transparent PNG tile |
| Rotate | Rotate the inner crop to match the reference | Two circular PNG images |
Install
The minimum supported Rust version is 1.88. Enable the serde feature to serialize answers and coordinates for server-side storage.
Cut a puzzle tile from a whole image
Pass an ordinary PNG or JPEG to the slide generator. It crops the background, cuts the matching tile from it and paints the hole using the built-in puzzle template.
use ;
Use slide::Mode::DragDrop for free movement and slide::Resources::new to supply custom mask, shadow and overlay images. The mask's alpha channel defines the cutout. Rotation similarly accepts whole images and creates the circular reference and smaller center crop. For graphic click puzzles, load individual transparent shapes or extract sprites from a larger image with click::Shape::from_region.
Customize text and backgrounds
use ;
Use click::Symbols::bundled_text()? for the default Chinese labels with the included open-source font. click::Font::bundled()? can also be combined with custom labels in the subset. Click options include symbol counts, size and angle ranges, palettes, shadows, opacity, hint noise, and separate hint backgrounds. Generated images can also be exported as bytes, Base64 or data URLs.
For verification, convert browser coordinates into original image pixels. Submit click points in hint order and the top-left tile position for slide or drag. For rotation, send the clockwise adjustment in degrees. A 90° generated rotation takes a 270° adjustment. Keep the answer on the server, associate it with a challenge ID, and enforce expiry and attempt limits in the application.
Run the example
The example saves a main image and a prompt for every mode. With no arguments it uses a generated background and the bundled font, writing to target/caprs-example.
Credits and licenses
Thanks to Awen and the go-captcha contributors for the original library and CAPTCHA designs. Porting notes map the Go APIs to Rust and describe the compatibility decisions.
- Rust code is licensed under MIT. The upstream Go project is licensed under Apache 2.0.
- Caprs Sans is a renamed subset of Noto Sans CJK SC Regular v2.004, copyright 2014–2021 Adobe, licensed under the SIL Open Font License 1.1. The subset contains printable ASCII and the characters used by the bundled examples and defaults.