add things
This commit is contained in:
102
commands/base64/base64.go
Normal file
102
commands/base64/base64.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package base64
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
)
|
||||
|
||||
type (
|
||||
Base64Cmd struct {
|
||||
file string
|
||||
decoderMode bool
|
||||
}
|
||||
)
|
||||
|
||||
func (*Base64Cmd) Name() string { return "base64" }
|
||||
func (*Base64Cmd) Synopsis() string { return "" }
|
||||
func (*Base64Cmd) Usage() string {
|
||||
return `Usage: hash_utils base64
|
||||
|
||||
Options:
|
||||
`
|
||||
}
|
||||
|
||||
func (p *Base64Cmd) SetFlags(f *flag.FlagSet) {
|
||||
f.StringVar(&p.file, "file", "", "get the checksum of a file")
|
||||
f.BoolVar(&p.decoderMode, "decode", false, "decode instead of encode")
|
||||
}
|
||||
|
||||
func (p *Base64Cmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
|
||||
if p.decoderMode {
|
||||
return p.decode(ctx, f, args)
|
||||
}
|
||||
return p.encode(ctx, f, args)
|
||||
}
|
||||
|
||||
func (p *Base64Cmd) encode(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
if len(p.file) > 0 {
|
||||
b, err := os.ReadFile(p.file)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: failed to open the file:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
e := base64.NewEncoder(base64.RawStdEncoding, os.Stdout)
|
||||
if _, err := e.Write(b); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
fmt.Println()
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
for _, arg := range f.Args() {
|
||||
fmt.Print(arg, ": ")
|
||||
e := base64.NewEncoder(base64.RawStdEncoding, os.Stdout)
|
||||
if _, err := e.Write([]byte(arg)); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
func (p *Base64Cmd) decode(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
if len(p.file) > 0 {
|
||||
file, err := os.OpenFile(p.file, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: failed to open the file:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
e := base64.NewDecoder(base64.RawStdEncoding, file)
|
||||
if _, err := io.Copy(os.Stdout, e); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
fmt.Println()
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
for _, arg := range f.Args() {
|
||||
r := strings.NewReader(arg)
|
||||
e := base64.NewDecoder(base64.RawStdEncoding, r)
|
||||
if _, err := io.Copy(os.Stdout, e); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
@@ -3,32 +3,60 @@ package bcrypt
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type (
|
||||
MD5Cmd struct {
|
||||
file string
|
||||
round int
|
||||
BCryptCmd struct {
|
||||
cost int
|
||||
check bool
|
||||
}
|
||||
)
|
||||
|
||||
func (*MD5Cmd) Name() string { return "bcrypt" }
|
||||
func (*MD5Cmd) Synopsis() string { return "" }
|
||||
func (*MD5Cmd) Usage() string {
|
||||
func (*BCryptCmd) Name() string { return "bcrypt" }
|
||||
func (*BCryptCmd) Synopsis() string { return "" }
|
||||
func (*BCryptCmd) Usage() string {
|
||||
return `Usage: hash_utils bcrypt
|
||||
|
||||
Options:
|
||||
`
|
||||
}
|
||||
|
||||
func (p *MD5Cmd) SetFlags(f *flag.FlagSet) {
|
||||
f.StringVar(&p.file, "file", "", "get the checksum of a file")
|
||||
f.IntVar(&p.round, "round", 12, "number of iteration")
|
||||
func (p *BCryptCmd) SetFlags(f *flag.FlagSet) {
|
||||
f.IntVar(&p.cost, "cost", 12, "number of iteration")
|
||||
f.BoolVar(&p.check, "check", false, "check a password")
|
||||
}
|
||||
|
||||
func (p *MD5Cmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
|
||||
func (p *BCryptCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
if p.check {
|
||||
if f.NArg() != 2 {
|
||||
fmt.Fprintln(os.Stderr, "error: invalid number of parameters")
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(f.Arg(0)), []byte(f.Arg(1))); err != nil {
|
||||
fmt.Println("no match")
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
fmt.Println("match")
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
if p.cost < bcrypt.MinCost {
|
||||
fmt.Fprintln(os.Stderr, "error: invalid cost settings: cost should be over", bcrypt.MinCost)
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
if p.cost > bcrypt.MaxCost {
|
||||
fmt.Fprintln(os.Stderr, "error: invalid cost settings: cost should be under", bcrypt.MaxCost)
|
||||
return subcommands.ExitUsageError
|
||||
}
|
||||
h, err := bcrypt.GenerateFromPassword([]byte(f.Arg(0)), p.cost)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
fmt.Println(string(h))
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
34
commands/version/version.go
Normal file
34
commands/version/version.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/google/subcommands"
|
||||
)
|
||||
|
||||
const (
|
||||
version string = "2.0.0"
|
||||
)
|
||||
|
||||
type (
|
||||
VersionCmd struct {
|
||||
}
|
||||
)
|
||||
|
||||
func (*VersionCmd) Name() string { return "version" }
|
||||
func (*VersionCmd) Synopsis() string { return "" }
|
||||
func (*VersionCmd) Usage() string {
|
||||
return `Usage: hash_utils version
|
||||
`
|
||||
}
|
||||
|
||||
func (p *VersionCmd) SetFlags(f *flag.FlagSet) {
|
||||
}
|
||||
|
||||
func (p *VersionCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
||||
fmt.Printf("hash_utils %s %s/%s\n", version, runtime.GOOS, runtime.GOARCH)
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
Reference in New Issue
Block a user