// +build ignore // 密码哈希工具 - 用于生成管理员密码的哈希值 // 使用方法: go run tools/password_hash.go <密码> package main import ( "fmt" "os" "golang.org/x/crypto/bcrypt" ) func main() { if len(os.Args) < 2 { fmt.Println("用法: go run tools/password_hash.go <密码>") fmt.Println() fmt.Println("示例:") fmt.Println(" go run tools/password_hash.go mypassword") fmt.Println() fmt.Println("输出:") fmt.Println(" 生成 bcrypt 哈希值,复制到配置文件的 security.password_hash 字段") os.Exit(1) } password := os.Args[1] // 生成 bcrypt 哈希(cost=12,平衡安全性和性能) hash, err := bcrypt.GenerateFromPassword([]byte(password), 12) if err != nil { fmt.Fprintf(os.Stderr, "生成哈希失败: %v\n", err) os.Exit(1) } fmt.Println("密码哈希值:") fmt.Printf(" %s\n", hash) fmt.Println() fmt.Println("请将此哈希值添加到 config.yaml:") fmt.Printf(" security:\n") fmt.Printf(" password_hash: \"%s\"\n", string(hash)) fmt.Println() fmt.Println("注意:") fmt.Println(" - 一旦设置了 password_hash,明文的 password 字段将被忽略") fmt.Println(" - 每次运行会生成不同的哈希值(正常现象),但验证结果相同") fmt.Println(" - cost=12 平衡了安全性和性能,可根据需要调整") fmt.Println() }