Files
opencode2api/internal/config/password.go
T
Jason Xu 46def7fedc refactor: organize Go packages and remove test files
Move the command to cmd/opencode2api and group production code into
configuration, gateway, protocol, model, admin, telemetry and helper packages.
Keep shared state private behind context, metrics and runtime interfaces.

Remove the requested test files and replace test-only CI jobs with vet/build
checks. Update Docker and release build paths, preserve version injection and
make the WebUI an explicit embedded asset package.

Add directory trees and source reading guides to both READMEs. Anchor binary
ignore rules so the command package is tracked.
2026-09-15 01:28:44 +08:00

70 lines
2.1 KiB
Go

package config
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
"golang.org/x/crypto/argon2"
)
const (
argonMemory = 64 * 1024
argonIterations = 3
argonParallelism = 2
argonSaltLength = 16
argonKeyLength = 32
)
func HashPassword(password string) (string, error) {
if len(password) < 10 {
return "", errors.New("webui password must contain at least 10 characters")
}
salt := make([]byte, argonSaltLength)
if _, err := rand.Read(salt); err != nil {
return "", err
}
hash := argon2.IDKey([]byte(password), salt, argonIterations, argonMemory, argonParallelism, argonKeyLength)
return fmt.Sprintf("$argon2id$v=19$m=%d,t=%d,p=%d$%s$%s", argonMemory, argonIterations, argonParallelism,
base64.RawStdEncoding.EncodeToString(salt), base64.RawStdEncoding.EncodeToString(hash)), nil
}
func VerifyPassword(encoded, password string) bool {
parts := strings.Split(encoded, "$")
if len(parts) != 6 || parts[1] != "argon2id" || parts[2] != "v=19" {
return false
}
parameters := strings.Split(parts[3], ",")
if len(parameters) != 3 {
return false
}
memory, err1 := parseArgonParameter(parameters[0], "m")
iterations, err2 := parseArgonParameter(parameters[1], "t")
parallelism, err3 := parseArgonParameter(parameters[2], "p")
if err1 != nil || err2 != nil || err3 != nil || memory < 8*parallelism || memory > 256*1024 || iterations < 1 || iterations > 10 || parallelism < 1 || parallelism > 16 {
return false
}
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
if err != nil || len(salt) < 8 || len(salt) > 64 {
return false
}
expected, err := base64.RawStdEncoding.DecodeString(parts[5])
if err != nil || len(expected) < 16 || len(expected) > 64 {
return false
}
actual := argon2.IDKey([]byte(password), salt, uint32(iterations), uint32(memory), uint8(parallelism), uint32(len(expected)))
return subtle.ConstantTimeCompare(actual, expected) == 1
}
func parseArgonParameter(value, name string) (uint64, error) {
prefix := name + "="
if !strings.HasPrefix(value, prefix) {
return 0, errors.New("invalid parameter")
}
return strconv.ParseUint(strings.TrimPrefix(value, prefix), 10, 32)
}