refactor: move go-chi/captcha into Gitea (#39529)

The `gitea.com/go-chi/captcha` package only exists for Gitea,
so it moves into `modules/imagecaptcha`.

- Reloading an expired challenge shows a new image instead of a broken one
- Every answer is consumed on its first check
- OpenID registration stops after a failed captcha

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
silverwind
2026-10-02 16:21:41 +02:00
committed by GitHub
parent e6ffbea888
commit 873efed5a6
17 changed files with 656 additions and 59 deletions
+12 -1
View File
@@ -111,7 +111,7 @@ func FastCryptoRandomBytes(length int) []byte {
// ChaCha8 is about 20x times faster than system's crypto/rand.
// It is suitable for UUIDs, session IDs, etc
pool := chaCha8RandPool()
chaCha8Rand := pool.Get().(*rand2.ChaCha8) //nolint:forcetypeassert // the pool's New only ever makes *rand2.ChaCha8
chaCha8Rand, _ := pool.Get().(*rand2.ChaCha8)
defer pool.Put(chaCha8Rand)
buf := make([]byte, length)
_, _ = chaCha8Rand.Read(buf)
@@ -123,6 +123,17 @@ func FastCryptoRandomHex(length int) string {
return hex.EncodeToString(buf)
}
func FastCryptoRandomInt[T int | int64](n T) T {
pool := chaCha8RandPool()
chaCha8Rand, _ := pool.Get().(*rand2.ChaCha8)
defer pool.Put(chaCha8Rand)
return rand2.New(chaCha8Rand).N(n)
}
func FastCryptoRand(seed [32]byte) *rand2.Rand {
return rand2.New(rand2.NewChaCha8(seed))
}
// ToLowerASCII returns s with all ASCII letters mapped to their lower case.
func ToLowerASCII(s string) string {
b := []byte(s)