README 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. *TODO(dchest): Add example images and audio.*
  2. Package captcha
  3. =====================
  4. import "github.com/dchest/captcha"
  5. Package captcha implements generation and verification of image and audio
  6. CAPTCHAs.
  7. Constants
  8. ---------
  9. const (
  10. // Standard number of digits in captcha.
  11. StdLength = 6
  12. // The number of captchas created that triggers garbage collection used
  13. // by default store.
  14. CollectNum = 100
  15. // Expiration time of captchas used by default store.
  16. Expiration = 10 * 60 // 10 minutes
  17. )
  18. const (
  19. // Standard width and height of a captcha image.
  20. StdWidth = 300
  21. StdHeight = 80
  22. )
  23. Variables
  24. ---------
  25. var ErrNotFound = os.NewError("captcha with the given id not found")
  26. Functions
  27. ---------
  28. ### func Collect
  29. func Collect()
  30. Collect deletes expired or used captchas from the internal storage. It is
  31. called automatically by New function every CollectNum generated captchas,
  32. but still exported to enable freeing memory manually if needed.
  33. Collection is launched in a new goroutine.
  34. ### func New
  35. func New(length int) (id string)
  36. New creates a new captcha of the given length, saves it in the internal
  37. storage, and returns its id.
  38. ### func RandomDigits
  39. func RandomDigits(length int) []byte
  40. RandomDigits returns a byte slice of the given length containing random
  41. digits in range 0-9.
  42. ### func Reload
  43. func Reload(id string) bool
  44. Reload generates and remembers new digits for the given captcha id. This
  45. function returns false if there is no captcha with the given id.
  46. After calling this function, the image or audio presented to a user must be
  47. refreshed to show the new captcha representation (WriteImage and WriteAudio
  48. will write the new one).
  49. ### func Server
  50. func Server(w, h int) http.Handler
  51. Server returns a handler that serves HTTP requests with image or
  52. audio representations of captchas. Image dimensions are accepted as
  53. arguments. The server decides which captcha to serve based on the last URL
  54. path component: file name part must contain a captcha id, file extension —
  55. its format (PNG or WAV).
  56. For example, for file name "B9QTvDV1RXbVJ3Ac.png" it serves an image captcha
  57. with id "B9QTvDV1RXbVJ3Ac", and for "B9QTvDV1RXbVJ3Ac.wav" it serves the
  58. same captcha in audio format.
  59. To serve an audio captcha as downloadable file, append "?get" to URL.
  60. To reload captcha (get a different solution for the same captcha id), append
  61. "?reload=x" to URL, where x may be anything (for example, current time or a
  62. random number to make browsers refetch an image instead of loading it from
  63. cache).
  64. ### func SetCustomStore
  65. func SetCustomStore(s Store)
  66. SetCustomStore sets custom storage for captchas, replacing the default
  67. memory store. This function must be called before generating any captchas.
  68. ### func Verify
  69. func Verify(id string, digits []byte) bool
  70. Verify returns true if the given digits are the ones that were used to
  71. create the given captcha id.
  72. The function deletes the captcha with the given id from the internal
  73. storage, so that the same captcha can't be verified anymore.
  74. ### func VerifyString
  75. func VerifyString(id string, digits string) bool
  76. VerifyString is like Verify, but accepts a string of digits. It removes
  77. spaces and commas from the string, but any other characters, apart from
  78. digits and listed above, will cause the function to return false.
  79. ### func WriteAudio
  80. func WriteAudio(w io.Writer, id string) os.Error
  81. WriteAudio writes WAV-encoded audio representation of the captcha with the
  82. given id.
  83. ### func WriteImage
  84. func WriteImage(w io.Writer, id string, width, height int) os.Error
  85. WriteImage writes PNG-encoded image representation of the captcha with the
  86. given id. The image will have the given width and height.
  87. Types
  88. -----
  89. type Audio struct {
  90. // contains unexported fields
  91. }
  92. ### func NewAudio
  93. func NewAudio(digits []byte) *Audio
  94. NewImage returns a new audio captcha with the given digits, where each digit
  95. must be in range 0-9.
  96. ### func (*Audio) EncodedLen
  97. func (a *Audio) EncodedLen() int
  98. EncodedLen returns the length of WAV-encoded audio captcha.
  99. ### func (*Audio) WriteTo
  100. func (a *Audio) WriteTo(w io.Writer) (n int64, err os.Error)
  101. WriteTo writes captcha audio in WAVE format into the given io.Writer, and
  102. returns the number of bytes written and an error if any.
  103. type Image struct {
  104. *image.NRGBA
  105. // contains unexported fields
  106. }
  107. ### func NewImage
  108. func NewImage(digits []byte, width, height int) *Image
  109. NewImage returns a new captcha image of the given width and height with the
  110. given digits, where each digit must be in range 0-9.
  111. ### func (*Image) WriteTo
  112. func (img *Image) WriteTo(w io.Writer) (int64, os.Error)
  113. WriteTo writes captcha image in PNG format into the given writer.
  114. type Store interface {
  115. // Set sets the digits for the captcha id.
  116. Set(id string, digits []byte)
  117. // Get returns stored digits for the captcha id. Clear indicates
  118. // whether the captcha must be deleted from the store.
  119. Get(id string, clear bool) (digits []byte)
  120. // Collect deletes expired captchas from the store. For custom stores
  121. // this method is not called automatically, it is only wired to the
  122. // package's Collect function. Custom stores must implement their own
  123. // procedure for calling Collect, for example, in Set method.
  124. Collect()
  125. }
  126. An object implementing Store interface can be registered with SetCustomStore
  127. function to handle storage and retrieval of captcha ids and solutions for
  128. them, replacing the default memory store.
  129. ### func NewMemoryStore
  130. func NewMemoryStore(collectNum int, expiration int64) Store
  131. NewMemoryStore returns a new standard memory store for captchas with the
  132. given collection threshold and expiration time in seconds. The returned
  133. store must be registered with SetCustomStore to replace the default one.
  134. Bugs
  135. ----
  136. [Not our bug] Google Chrome 10 plays unsigned 8-bit PCM WAVE
  137. audio on Mac with horrible distortions. Issue:
  138. http://code.google.com/p/chromium/issues/detail?id=70730.
  139. This has been fixed, and version 12 will play them properly.
  140. While Image conforms to io.WriterTo interface, its WriteTo
  141. method returns 0 instead of the actual bytes written because png.Encode
  142. doesn't report this.