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:
@@ -0,0 +1,41 @@
|
||||
---
|
||||
name: password_hash
|
||||
kind: function
|
||||
lang: go
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "func PasswordHash(password string, cost int) (string, error)"
|
||||
description: "Hashea un password con bcrypt. Cost por defecto es 12 (si se pasa 0). El hash resultante incluye el salt y el cost embebidos."
|
||||
tags: [password, hash, bcrypt, auth, infra]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: error_go_core
|
||||
imports: [golang.org/x/crypto/bcrypt]
|
||||
params:
|
||||
- name: password
|
||||
desc: "password en texto plano a hashear"
|
||||
- name: cost
|
||||
desc: "coste bcrypt entre 4 y 14. 0 usa el default 12 (buen balance velocidad/seguridad en 2025)"
|
||||
output: "hash bcrypt en formato $2a$... apto para guardar en BD y verificar con PasswordVerify"
|
||||
tested: true
|
||||
tests: ["hashea password con cost default", "hashea password con cost custom", "hashes distintos para mismo password (salt diferente)"]
|
||||
test_file_path: "functions/infra/password_hash_test.go"
|
||||
file_path: "functions/infra/password_hash.go"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```go
|
||||
hash, err := PasswordHash(inputPassword, 12)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db.Exec("INSERT INTO users (email, password_hash) VALUES (?, ?)", email, hash)
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Impura — bcrypt usa entropia del OS para generar salt aleatorio en cada invocacion. El hash producido incluye el salt y el cost embebidos en el string (`$2a$12$salt...hash`), por lo que PasswordVerify no necesita el cost como parametro aparte. Cost 12 = ~250ms/hash en hardware moderno (2025): suficiente para bloquear ataques por fuerza bruta sin ser insoportable en el login. Para proteccion extra en servidores con mucho CPU disponible se puede subir a 14.
|
||||
Reference in New Issue
Block a user