ollie/toolsrv/auth9p.go

27 lines
702 B
Go

// auth9p.go - Authentication helpers for 9P toolsrv
package toolsrv
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
)
// GenerateSecret generates a random shared secret for toolsrv auth.
func GenerateSecret() ([]byte, error) {
secret := make([]byte, 32)
if _, err := rand.Read(secret); err != nil {
return nil, err
}
return secret, nil
}
// ComputeRegistrationSig computes the HMAC signature for agent registration.
// This is used by olliesrv to sign agent registration requests.
func ComputeRegistrationSig(secret []byte, agentID, cwd string) string {
h := hmac.New(sha256.New, secret)
h.Write([]byte(agentID + "|" + cwd))
return hex.EncodeToString(h.Sum(nil))
}