{"id":13600872,"url":"https://github.com/dchest/captcha","last_synced_at":"2025-05-13T19:10:48.689Z","repository":{"id":38427889,"uuid":"1664978","full_name":"dchest/captcha","owner":"dchest","description":"Go package captcha implements generation and verification of image and audio CAPTCHAs.","archived":false,"fork":false,"pushed_at":"2024-12-11T08:07:11.000Z","size":871,"stargazers_count":1987,"open_issues_count":4,"forks_count":307,"subscribers_count":52,"default_branch":"master","last_synced_at":"2025-04-27T04:59:14.431Z","etag":null,"topics":["audio-captchas","captcha","go","image-captcha"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/dchest/captcha","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dchest.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-04-26T12:58:19.000Z","updated_at":"2025-04-25T10:50:02.000Z","dependencies_parsed_at":"2024-06-18T11:13:03.390Z","dependency_job_id":"10e66cd4-d3e8-4658-b66a-faa671051345","html_url":"https://github.com/dchest/captcha","commit_stats":{"total_commits":152,"total_committers":4,"mean_commits":38.0,"dds":"0.019736842105263164","last_synced_commit":"aa277053b17f58213f706e2b8a9b4dd3817136e3"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dchest%2Fcaptcha","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dchest%2Fcaptcha/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dchest%2Fcaptcha/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dchest%2Fcaptcha/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dchest","download_url":"https://codeload.github.com/dchest/captcha/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251089620,"owners_count":21534523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["audio-captchas","captcha","go","image-captcha"],"created_at":"2024-08-01T18:00:50.257Z","updated_at":"2025-04-27T04:59:18.120Z","avatar_url":"https://github.com/dchest.png","language":"Go","funding_links":[],"categories":["开源类库","Misc","Go","Generowanie","Open source library","Repositories","验证码生成工具"],"sub_categories":["图形处理","Graphics Processing"],"readme":"Package captcha\n=====================\n\n**:warning: Warning: this captcha can be broken by advanced OCR captcha breaking algorithms.**\n\n\timport \"github.com/dchest/captcha\"\n\nPackage captcha implements generation and verification of image and audio\nCAPTCHAs.\n\nA captcha solution is the sequence of digits 0-9 with the defined length.\nThere are two captcha representations: image and audio.\n\nAn image representation is a PNG-encoded image with the solution printed on\nit in such a way that makes it hard for computers to solve it using OCR.\n\nAn audio representation is a WAVE-encoded (8 kHz unsigned 8-bit) sound with the\nspoken solution (currently in English, Russian, Chinese, and Japanese). To make\nit hard for computers to solve audio captcha, the voice that pronounces numbers\nhas random speed and pitch, and there is a randomly generated background noise\nmixed into the sound.\n\nThis package doesn't require external files or libraries to generate captcha\nrepresentations; it is self-contained.\n\nTo make captchas one-time, the package includes a memory storage that stores\ncaptcha ids, their solutions, and expiration time. Used captchas are removed\nfrom the store immediately after calling Verify or VerifyString, while\nunused captchas (user loaded a page with captcha, but didn't submit the\nform) are collected automatically after the predefined expiration time.\nDevelopers can also provide custom store (for example, which saves captcha\nids and solutions in database) by implementing Store interface and\nregistering the object with SetCustomStore.\n\nCaptchas are created by calling New, which returns the captcha id.  Their\nrepresentations, though, are created on-the-fly by calling WriteImage or\nWriteAudio functions. Created representations are not stored anywhere, but\nsubsequent calls to these functions with the same id will write the same\ncaptcha solution. Reload function will create a new different solution for the\nprovided captcha, allowing users to \"reload\" captcha if they can't solve the\ndisplayed one without reloading the whole page.  Verify and VerifyString are\nused to verify that the given solution is the right one for the given captcha\nid.\n\nServer provides an http.Handler which can serve image and audio\nrepresentations of captchas automatically from the URL. It can also be used\nto reload captchas.  Refer to Server function documentation for details, or\ntake a look at the example in \"capexample\" subdirectory.\n\n\nExamples\n--------\n\n![Image](https://github.com/dchest/captcha/raw/master/capgen/example.png)\n\n[Audio](https://github.com/dchest/captcha/raw/master/capgen/example.wav)\n\n\nConstants\n---------\n\n``` go\nconst (\n    // Default number of digits in captcha solution.\n    DefaultLen = 6\n    // The number of captchas created that triggers garbage collection used\n    // by default store.\n    CollectNum = 100\n    // Expiration time of captchas used by default store.\n    Expiration = 10 * time.Minute\n)\n```\n\n``` go\nconst (\n    // Standard width and height of a captcha image.\n    StdWidth  = 240\n    StdHeight = 80\n)\n```\n\n\nVariables\n---------\n\n``` go\nvar (\n    ErrNotFound = errors.New(\"captcha: id not found\")\n)\n```\n\n\n\nFunctions\n---------\n\n### func New\n\n\tfunc New() string\n\t\nNew creates a new captcha with the standard length, saves it in the internal\nstorage and returns its id.\n\n### func NewLen\n\n\tfunc NewLen(length int) (id string)\n\t\nNewLen is just like New, but accepts length of a captcha solution as the\nargument.\n\n### func RandomDigits\n\n\tfunc RandomDigits(length int) (b []byte)\n\t\nRandomDigits returns a byte slice of the given length containing\npseudorandom numbers in range 0-9. The slice can be used as a captcha\nsolution.\n\n### func Reload\n\n\tfunc Reload(id string) bool\n\t\nReload generates and remembers new digits for the given captcha id.  This\nfunction returns false if there is no captcha with the given id.\n\nAfter calling this function, the image or audio presented to a user must be\nrefreshed to show the new captcha representation (WriteImage and WriteAudio\nwill write the new one).\n\n### func Server\n\n\tfunc Server(imgWidth, imgHeight int) http.Handler\n\t\nServer returns a handler that serves HTTP requests with image or\naudio representations of captchas. Image dimensions are accepted as\narguments. The server decides which captcha to serve based on the last URL\npath component: file name part must contain a captcha id, file extension —\nits format (PNG or WAV).\n\nFor example, for file name \"LBm5vMjHDtdUfaWYXiQX.png\" it serves an image captcha\nwith id \"LBm5vMjHDtdUfaWYXiQX\", and for \"LBm5vMjHDtdUfaWYXiQX.wav\" it serves the\nsame captcha in audio format.\n\nTo serve a captcha as a downloadable file, the URL must be constructed in\nsuch a way as if the file to serve is in the \"download\" subdirectory:\n\"/download/LBm5vMjHDtdUfaWYXiQX.wav\".\n\nTo reload captcha (get a different solution for the same captcha id), append\n\"?reload=x\" to URL, where x may be anything (for example, current time or a\nrandom number to make browsers refetch an image instead of loading it from\ncache).\n\nBy default, the Server serves audio in English language. To serve audio\ncaptcha in one of the other supported languages, append \"lang\" value, for\nexample, \"?lang=ru\".\n\n### func SetCustomStore\n\n\tfunc SetCustomStore(s Store)\n\t\nSetCustomStore sets custom storage for captchas, replacing the default\nmemory store. This function must be called before generating any captchas.\n\n### func Verify\n\n\tfunc Verify(id string, digits []byte) bool\n\t\nVerify returns true if the given digits are the ones that were used to\ncreate the given captcha id.\n\nThe function deletes the captcha with the given id from the internal\nstorage, so that the same captcha can't be verified anymore.\n\n### func VerifyString\n\n\tfunc VerifyString(id string, digits string) bool\n\t\nVerifyString is like Verify, but accepts a string of digits.  It removes\nspaces and commas from the string, but any other characters, apart from\ndigits and listed above, will cause the function to return false.\n\n### func WriteAudio\n\n\tfunc WriteAudio(w io.Writer, id string, lang string) error\n\t\nWriteAudio writes WAV-encoded audio representation of the captcha with the\ngiven id and the given language. If there are no sounds for the given\nlanguage, English is used.\n\n### func WriteImage\n\n\tfunc WriteImage(w io.Writer, id string, width, height int) error\n\t\nWriteImage writes PNG-encoded image representation of the captcha with the\ngiven id. The image will have the given width and height.\n\n\nTypes\n-----\n\n``` go\ntype Audio struct {\n    // contains unexported fields\n}\n```\n\n\n### func NewAudio\n\n\tfunc NewAudio(id string, digits []byte, lang string) *Audio\n\t\nNewAudio returns a new audio captcha with the given digits, where each digit\nmust be in range 0-9. Digits are pronounced in the given language. If there\nare no sounds for the given language, English is used.\n\nPossible values for lang are \"en\", \"ja\", \"ru\", \"zh\", \"pt\".\n\n### func (*Audio) EncodedLen\n\n\tfunc (a *Audio) EncodedLen() int\n\t\nEncodedLen returns the length of WAV-encoded audio captcha.\n\n### func (*Audio) WriteTo\n\n\tfunc (a *Audio) WriteTo(w io.Writer) (n int64, err error)\n\t\nWriteTo writes captcha audio in WAVE format into the given io.Writer, and\nreturns the number of bytes written and an error if any.\n\n``` go\ntype Image struct {\n    *image.Paletted\n    // contains unexported fields\n}\n```\n\n\n### func NewImage\n\n\tfunc NewImage(id string, digits []byte, width, height int) *Image\n\t\nNewImage returns a new captcha image of the given width and height with the\ngiven digits, where each digit must be in range 0-9.\n\n### func (*Image) WriteTo\n\n\tfunc (m *Image) WriteTo(w io.Writer) (int64, error)\n\t\nWriteTo writes captcha image in PNG format into the given writer.\n\n``` go\ntype Store interface {\n    // Set sets the digits for the captcha id.\n    Set(id string, digits []byte)\n\n    // Get returns stored digits for the captcha id. Clear indicates\n    // whether the captcha must be deleted from the store.\n    Get(id string, clear bool) (digits []byte)\n}\n```\n\nAn object implementing Store interface can be registered with SetCustomStore\nfunction to handle storage and retrieval of captcha ids and solutions for\nthem, replacing the default memory store.\n\nIt is the responsibility of an object to delete expired and used captchas\nwhen necessary (for example, the default memory store collects them in Set\nmethod after the certain amount of captchas has been stored.)\n\n### func NewMemoryStore\n\n\tfunc NewMemoryStore(collectNum int, expiration time.Duration) Store\n\t\nNewMemoryStore returns a new standard memory store for captchas with the\ngiven collection threshold and expiration time in seconds. The returned\nstore must be registered with SetCustomStore to replace the default one.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdchest%2Fcaptcha","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdchest%2Fcaptcha","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdchest%2Fcaptcha/lists"}