密码
ling-base common/password 模块文档
在线 Playground
在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。
另见专题文档:密码安全文档。以下为
common/password包 README 全文。
password
Secure password hashing and verification using bcrypt and Argon2id, the two algorithms recommended by OWASP for password storage.
Both algorithms produce self-describing hash strings that encode the algorithm, parameters, salt, and digest, so a stored hash carries everything needed to verify it. Argon2id is the primary recommendation (memory-hard, GPU-resistant); bcrypt is provided as a widely-compatible alternative.
Key functions
Hash(plain string, opts *Options) (string, error)— produce a self-describing hashVerify(plain, stored string) bool— verify a password against a stored hashNeedsRehash(stored string, opts *Options) bool— detect outdated parameters for transparent upgradesMustHash— panic-on-error variant for tests/init
Key types
Options— algorithm selection and tuning (cost, memory, threads, key/salt length)Algorithm—AlgorithmArgon2id(default) orAlgorithmBcrypt
Quick start
import "github.com/LingByte/ling-base/common/password"
hashed, err := password.Hash("my-secret-password", nil)
if password.Verify("my-secret-password", hashed) {
// access granted
}
if password.NeedsRehash(hashed, nil) {
newHash, _ := password.Hash("my-secret-password", nil)
// persist newHash
}