Skip to content

Commit

Permalink
chore: restructured for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
johnabass committed Aug 29, 2024
1 parent 83d45ee commit f661208
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
27 changes: 17 additions & 10 deletions cmd/hash/cli.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main

import (
"fmt"
"io"

"github.com/alecthomas/kong"
"github.com/xmidt-org/bascule/basculehash"
"golang.org/x/crypto/bcrypt"
)

// Context is the contextual information for all commands.
type Context struct {
Stdout io.Writer
Stderr io.Writer
}
const (
// MaxBcryptPlaintextLength is the maximum length of the input that
// bcrypt will operate on. This value isn't exposed via the
// golang.org/x/crypto/bcrypt package.
MaxBcryptPlaintextLength = 72
)

// Bcrypt is the subcommand for the bcrypt algorithm.
type Bcrypt struct {
Cost int `default:"10" help:"the cost parameter for bcrypt"`
Plaintext string `arg:"" required:""`
Cost int `default:"10" short:"c" help:"the cost parameter for bcrypt. Must be between 4 and 31, inclusive."`
Plaintext string `arg:"" required:"" help:"the plaintext (e.g. password) to hash. This cannot exceed 72 bytes in length."`
}

func (cmd *Bcrypt) Validate() error {
Expand All @@ -28,17 +32,20 @@ func (cmd *Bcrypt) Validate() error {
case cmd.Cost > bcrypt.MaxCost:
return fmt.Errorf("Cost cannot be greater than %d", bcrypt.MaxCost)

case len(cmd.Plaintext) > MaxBcryptPlaintextLength:
return fmt.Errorf("Plaintext length cannot exceed %d bytes", MaxBcryptPlaintextLength)

default:
return nil
}
}

func (cmd *Bcrypt) Run(ctx *Context) error {
func (cmd *Bcrypt) Run(kong *kong.Kong) error {
hasher := basculehash.Bcrypt{
Cost: cmd.Cost,
}

_, err := hasher.Hash(ctx.Stdout, []byte(cmd.Plaintext))
_, err := hasher.Hash(kong.Stdout, []byte(cmd.Plaintext))
return err
}

Expand Down
31 changes: 15 additions & 16 deletions cmd/hash/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,37 @@
package main

import (
"fmt"
"os"

"github.com/alecthomas/kong"
)

func run(args []string, ctx *Context) (err error) {
var (
grammar *kong.Kong
kongCtx *kong.Context
func newKong() (*kong.Kong, error) {
return kong.New(
new(CLI),
kong.UsageOnError(),
kong.Description("hashes plaintext using bascule's infrastructure"),
)
}

grammar, err = kong.New(new(CLI), kong.Bind(ctx))
func run(grammar *kong.Kong, args []string) (err error) {
var ctx *kong.Context
if err == nil {
kongCtx, err = grammar.Parse(args)
ctx, err = grammar.Parse(args)
}

if err == nil {
err = kongCtx.Run()
err = ctx.Run()
}

return
}

func main() {
err := run(os.Args[1:], &Context{
Stdout: os.Stdout,
Stderr: os.Stderr,
})

if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
grammar, err := newKong()
if err == nil {
err = run(grammar, os.Args[1:])
}

grammar.FatalIfErrorf(err)
}

0 comments on commit f661208

Please sign in to comment.