package crypto import "golang.org/x/crypto/bcrypt" // HashPassword hashes a plain text password using bcrypt. // HashPassword 使用 bcrypt 对明文密码进行加密。 // // Parameters: // - password: The plain text password to hash. // // Returns: // - string: The hashed password. // - error: An error if hashing fails. // // Note: // - Uses bcrypt.DefaultCost (10) for hashing. // - The resulting hash is approximately 60 characters long. // // 参数: // - password: 要加密的明文密码。 // // 返回值: // - string: 加密后的密码。 // - error: 如果加密失败则返回错误。 // // 注意: // - 使用 bcrypt.DefaultCost (10) 进行加密。 // - 生成的哈希值大约为 60 个字符。 func HashPassword(password string) (string, error) { bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) return string(bytes), err } // CheckPasswordHash compares a plain text password with a hashed password. // CheckPasswordHash 比较明文密码和加密密码。 // // Parameters: // - password: The plain text password to check. // - hash: The hashed password to compare against. // // Returns: // - bool: true if the password matches the hash, false otherwise. // // 参数: // - password: 要检查的明文密码。 // - hash: 要比较的加密密码。 // // 返回值: // - bool: 如果密码匹配哈希值则返回 true,否则返回 false。 func CheckPasswordHash(password, hash string) bool { err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) return err == nil }