feat: jwt_generate, jwt_validate, password_hash, password_verify

Fase 2 del issue 0010 — auth core:
- jwt_generate/validate: HS256 manual con crypto/hmac + crypto/sha256
- password_hash/verify: wrappers de golang.org/x/crypto/bcrypt (cost 12 default)
- JWT rechaza alg != HS256 para mitigar ataque 'alg=none'
- hmac.Equal para comparacion constant-time de firmas
This commit is contained in:
2026-04-18 17:39:00 +02:00
parent 4bc6d1bced
commit 07341aa89f
14 changed files with 517 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
package infra
import "golang.org/x/crypto/bcrypt"
// PasswordHash hashea un password con bcrypt.
// cost controla el trabajo computacional (4 = minimo, 14 = muy lento). Valor 0 usa default 12.
func PasswordHash(password string, cost int) (string, error) {
if cost <= 0 {
cost = 12
}
b, err := bcrypt.GenerateFromPassword([]byte(password), cost)
if err != nil {
return "", err
}
return string(b), nil
}