go-crypt/cmd/crypt/cmd_checksum.go
Claude 7407b89b8d
refactor(ax): AX RFC-025 compliance sweep pass 1
Remove banned imports (fmt, strings, os, errors, path/filepath) across all
production and test files, replace with core.* primitives, coreio.ReadStream,
and coreerr.E. Upgrade dappco.re/go/core v0.5.0 → v0.7.0 for core.PathBase
and core.Is. Fix isRepoScoped to exclude pr.* capabilities (enforcement is at
the forge layer, not the policy engine). Add Good/Bad/Ugly test coverage to
all packages missing the mandatory three-category naming convention.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 08:48:56 +01:00

59 lines
1.4 KiB
Go

package crypt
import (
core "dappco.re/go/core"
"dappco.re/go/core/crypt/crypt"
"forge.lthn.ai/core/cli/pkg/cli"
)
// Checksum command flags
var (
checksumSHA512 bool
checksumVerify string
)
func addChecksumCommand(parent *cli.Command) {
checksumCmd := cli.NewCommand("checksum", "Compute file checksum", "", func(cmd *cli.Command, args []string) error {
return runChecksum(args[0])
})
checksumCmd.Args = cli.ExactArgs(1)
cli.BoolFlag(checksumCmd, &checksumSHA512, "sha512", "", false, "Use SHA-512 instead of SHA-256")
cli.StringFlag(checksumCmd, &checksumVerify, "verify", "", "", "Verify file against this hash")
parent.AddCommand(checksumCmd)
}
func runChecksum(path string) error {
var hash string
var err error
if checksumSHA512 {
hash, err = crypt.SHA512File(path)
} else {
hash, err = crypt.SHA256File(path)
}
if err != nil {
return cli.Wrap(err, "failed to compute checksum")
}
if checksumVerify != "" {
if hash == checksumVerify {
cli.Success(core.Sprintf("Checksum matches: %s", core.PathBase(path)))
return nil
}
cli.Error(core.Sprintf("Checksum mismatch: %s", core.PathBase(path)))
cli.Dim(core.Sprintf(" expected: %s", checksumVerify))
cli.Dim(core.Sprintf(" got: %s", hash))
return cli.Err("checksum verification failed")
}
algorithm := "SHA-256"
if checksumSHA512 {
algorithm = "SHA-512"
}
cli.Text(core.Sprintf("%s %s (%s)", hash, path, algorithm))
return nil
}