captcha

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2020 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Overview

Package captcha contains methods to generate captchas. There are two basic methods: normal captchas and timed captchas. A captcha consists of a captcha and an id. The id can be shown publicly, the captcha should be guessed by humans. A captcha can not be derivated from an id other than through brute force. The verification of a captcha soley depends on the id, so no state is required. This also means that you can use the functions parallel without problems or side effects. There are two caviats, though: * A hidden value is used to make predictions impossible. This means that whenever you restart the program, old captchas are no longer valid. * No session management is implemented. One captcha / id combination is always valid (as long as the hidden value is the same).

Normal only captchas consist of a id / captcha combination. Therefore, you are strongly advised to use some sort of session management. Timed captchas are valid for a specified amount of time. Therefore, a session management might not be needed (but you might use one, too).

Index

Constants

View Source
const (
	// RandomSizeDefault contains the suggested default size for random data.
	RandomSizeDefault = 6
)

Variables

This section is empty.

Functions

func Get

func Get(randomSize int) (id, captcha []byte, err error)

Get returns one random id / captcha combination. The number of bytes is determined by randomSize.

Can be used concurrent.

func GetStrings

func GetStrings() (id, captcha string, err error)

GetStrings returns a string representation of a new captcha (with default size). Please note: You have no access on the original id. See Get for more information about captchas.

Can be used concurrent.

func GetStringsTimed

func GetStringsTimed(start time.Time) (id, captcha string, err error)

GetStringsTimed returns a string representation of a new timed captcha (with default size). Please note: You have no access on the original id. See Get for more information about captchas.

Can be used concurrent.

func GetTimed

func GetTimed(start time.Time, randomSize int) (id, captcha []byte, err error)

GetTimed returns one timed random id / captcha combination. The number of bytes is determined by randomSize. start determines the time from which the captcha is valid.

You do not need to remember the time since it is encoded in the id (and can not be tampered with without invalidating the captcha).

Can be used concurrent.

func Verify

func Verify(id, captcha []byte, randomSize int) bool

Verify validates whether an id / captia combination is valid. randomSize musst correspond to the captcha size and must be the same as at the generation.

Since Verify does not check if an id is already used, the same id / captcha combination is always valid.

Can be used concurrent.

func VerifyStrings

func VerifyStrings(id, captcha string) bool

VerifyStrings verifies a string representation of a new captcha (with default size). See Verify for more information about captchas.

Can be used concurrent.

func VerifyStringsTimed

func VerifyStringsTimed(id, captcha string, now time.Time, validDuration time.Duration) bool

VerifyStringsTimed verifies a string representation of a new timed captcha (with default size). See Verify for more information about captchas.

Can be used concurrent.

func VerifyTimed

func VerifyTimed(id, captcha []byte, now time.Time, validDuration time.Duration, randomSize int) bool

VerifyTimed validates whether an id / captia combination is valid and in date. randomSize musst correspond to the captcha size and must be the same as at the generation. Duration determines how long a captcha should be seen as valid.

Since VerifyTimed does not check if an id is already used, the same id / captcha combination is always valid (in the given time period).

Can be used concurrent.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL