working commit

This commit is contained in:
2026-03-24 23:18:34 +02:00
parent 8efe7090be
commit 1f5b4a71f1
28 changed files with 1623 additions and 51 deletions
+117
View File
@@ -0,0 +1,117 @@
package hash
import (
"fmt"
"helmet/pkg/passwd"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)
type Tool struct {
cmd *cobra.Command
createHashParams CreateHashParams
checkHashParams CheckHashParams
}
func NewTool() *Tool {
tool := &Tool{}
tool.cmd = &cobra.Command{
Use: "hash",
Short: "Hash opeation",
}
createHashCmd := &cobra.Command{
Use: "create password",
Args: cobra.ExactArgs(1),
Short: "Create authentification hash",
RunE: tool.CreateHash,
}
checkHashCmd := &cobra.Command{
Use: "check password hash",
Args: cobra.ExactArgs(2),
Short: "Check authentification hash",
RunE: tool.CheckHash,
}
tool.cmd.AddCommand(createHashCmd)
tool.cmd.AddCommand(checkHashCmd)
return tool
}
func (tool *Tool) GetCmd() *cobra.Command {
return tool.cmd
}
type CreateHashParams struct {
Password string
}
type CreateHashResult struct {
Hash string `json:"hash"`
}
func (tool *Tool) CreateHash(cmd *cobra.Command, args []string) error {
tool.createHashParams.Password = args[0]
res, err := tool.createHash(&tool.createHashParams)
printResponse(res, err)
return err
}
func (tool *Tool) createHash(params *CreateHashParams) (*CreateHashResult, error) {
var err error
res := &CreateHashResult{}
if params.Password == "" {
err = fmt.Errorf("Empty password")
return res, err
}
res.Hash = passwd.MakeSHA256Hash([]byte(params.Password))
if !passwd.PasswordMatch([]byte(params.Password), res.Hash) {
err = fmt.Errorf("Error hash validation")
return res, err
}
return res, err
}
type CheckHashParams struct {
Password string
Hash string
}
type CheckHashResult struct {
Match bool `json:"match"`
}
func (tool *Tool) CheckHash(cmd *cobra.Command, args []string) error {
tool.checkHashParams.Password = args[0]
tool.checkHashParams.Hash = args[1]
res, err := tool.checkHash(&tool.checkHashParams)
printResponse(res, err)
return err
}
func (tool *Tool) checkHash(params *CheckHashParams) (*CheckHashResult, error) {
var err error
res := &CheckHashResult{}
if params.Password == "" {
err = fmt.Errorf("Empty password")
return res, err
}
res.Match = passwd.PasswordMatch([]byte(params.Password), params.Hash)
return res, err
}
func printResponse(res any, err error) {
type Response struct {
Error bool `json:"error" json:"error"`
Message string `json:"message,omitempty" json:"message,omitempty"`
Result any `json:"result,omitempty" json:"result,omitempty"`
}
resp := Response{}
if err != nil {
resp.Error = true
resp.Message = err.Error()
} else {
resp.Result = res
}
respBytes, _ := yaml.Marshal(resp)
fmt.Printf("---\n%s\n", string(respBytes))
}
+22
View File
@@ -0,0 +1,22 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package main
import (
"os"
)
func main() {
tool := NewTool()
err := tool.Exec(os.Args[1:])
if err != nil {
os.Exit(1)
}
}
+44
View File
@@ -0,0 +1,44 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package main
import (
"os"
"path/filepath"
"helmet/cmd/minilbadm/hash"
"github.com/spf13/cobra"
)
type Tool struct {
cmd *cobra.Command
hashTool *hash.Tool
}
func NewTool() *Tool {
execName := filepath.Base(os.Args[0])
tool := &Tool{}
tool.cmd = &cobra.Command{
Use: execName,
Short: "\nHash tool",
SilenceUsage: true,
}
tool.cmd.CompletionOptions.DisableDefaultCmd = true
tool.hashTool = hash.NewTool()
tool.cmd.AddCommand(tool.hashTool.GetCmd())
return tool
}
func (tool *Tool) Exec(args []string) error {
tool.cmd.SetArgs(args)
return tool.cmd.Execute()
}