rewriting in go

This commit is contained in:
2025-08-11 23:10:30 +02:00
parent bea5baf026
commit 154f058971
23 changed files with 449 additions and 656 deletions

34
commands/bcrypt/bcrypt.go Normal file
View File

@@ -0,0 +1,34 @@
package bcrypt
import (
"context"
"flag"
"github.com/google/subcommands"
)
type (
MD5Cmd struct {
file string
round int
}
)
func (*MD5Cmd) Name() string { return "bcrypt" }
func (*MD5Cmd) Synopsis() string { return "" }
func (*MD5Cmd) 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 *MD5Cmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
return subcommands.ExitSuccess
}

95
commands/crc8/crc8.go Normal file
View File

@@ -0,0 +1,95 @@
package crc8
import (
"context"
"flag"
"fmt"
"hash_utils/tools"
"os"
"github.com/google/subcommands"
"github.com/sigurn/crc8"
)
type (
CRC8Cmd struct {
file string
table string
}
)
var (
tables = []crc8.Params{
crc8.CRC8,
crc8.CRC8_CDMA2000,
crc8.CRC8_DARC,
crc8.CRC8_DVB_S2,
crc8.CRC8_EBU,
crc8.CRC8_I_CODE,
crc8.CRC8_ITU,
crc8.CRC8_MAXIM,
crc8.CRC8_ROHC,
crc8.CRC8_WCDMA,
}
)
func (*CRC8Cmd) Name() string { return "crc8" }
func (*CRC8Cmd) Synopsis() string { return "" }
func (*CRC8Cmd) Usage() string {
return `Usage: hash_utils crc8
Options:
`
}
func (p *CRC8Cmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.file, "file", "", "get the checksum of a file")
f.StringVar(&p.table, "table", crc8.CRC8.Name, "Predefined CRC-8 algorithms")
}
func (p *CRC8Cmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
var err error
param := crc8.CRC8
if len(p.table) > 0 {
param, err = parse(p.table)
if err != nil {
fmt.Printf("Available tables: ")
for _, table := range tables {
fmt.Printf("%s ", table.Name)
}
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitFailure
}
}
if len(p.file) > 0 {
b, err := os.ReadFile(p.file)
if err != nil {
fmt.Fprintln(os.Stderr, "error: failed to read file:", err)
return subcommands.ExitFailure
}
h := tools.CRC8(b, param)
fmt.Printf("%s: %s\n", f.Name(), h)
return subcommands.ExitSuccess
}
for _, arg := range f.Args() {
h := tools.CRC8([]byte(arg), param)
fmt.Printf("%s: %s\n", arg, h)
}
return subcommands.ExitSuccess
}
func parse(tableName string) (crc8.Params, error) {
for _, table := range tables {
if table.Name == tableName {
return table, nil
}
}
return crc8.CRC8, fmt.Errorf("invalid table name: %s", tableName)
}

63
commands/md5/md5.go Normal file
View File

@@ -0,0 +1,63 @@
package md5
import (
"context"
"crypto/md5"
"flag"
"fmt"
"hash_utils/tools"
"os"
"strings"
"github.com/google/subcommands"
)
type (
MD5Cmd struct {
file string
}
)
func (*MD5Cmd) Name() string { return "md5" }
func (*MD5Cmd) Synopsis() string { return "" }
func (*MD5Cmd) Usage() string {
return `Usage: hash_utils md5
Options:
`
}
func (p *MD5Cmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.file, "file", "", "get the checksum of a file")
}
func (p *MD5Cmd) Execute(_ 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()
h, err := tools.Hash(md5.New(), file)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitFailure
}
fmt.Printf("%s: %s\n", f.Name(), h)
return subcommands.ExitSuccess
}
for _, arg := range f.Args() {
h, err := tools.Hash(md5.New(), strings.NewReader(arg))
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitFailure
}
fmt.Printf("%s: %s\n", arg, h)
}
return subcommands.ExitSuccess
}

63
commands/sha1/sha1.go Normal file
View File

@@ -0,0 +1,63 @@
package sha1
import (
"context"
"crypto/sha1"
"flag"
"fmt"
"hash_utils/tools"
"os"
"strings"
"github.com/google/subcommands"
)
type (
SHA1Cmd struct {
file string
}
)
func (*SHA1Cmd) Name() string { return "sha1" }
func (*SHA1Cmd) Synopsis() string { return "" }
func (*SHA1Cmd) Usage() string {
return `Usage: hash_utils sha1
Options:
`
}
func (p *SHA1Cmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.file, "file", "", "get the checksum of a file")
}
func (p *SHA1Cmd) Execute(_ 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()
h, err := tools.Hash(sha1.New(), file)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitFailure
}
fmt.Printf("%s: %s\n", f.Name(), h)
return subcommands.ExitSuccess
}
for _, arg := range f.Args() {
h, err := tools.Hash(sha1.New(), strings.NewReader(arg))
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitFailure
}
fmt.Printf("%s: %s\n", arg, h)
}
return subcommands.ExitSuccess
}

63
commands/sha256/sha256.go Normal file
View File

@@ -0,0 +1,63 @@
package sha256
import (
"context"
"crypto/sha256"
"flag"
"fmt"
"hash_utils/tools"
"os"
"strings"
"github.com/google/subcommands"
)
type (
SHA256Cmd struct {
file string
}
)
func (*SHA256Cmd) Name() string { return "sha256" }
func (*SHA256Cmd) Synopsis() string { return "" }
func (*SHA256Cmd) Usage() string {
return `Usage: hash_utils sha256
Options:
`
}
func (p *SHA256Cmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.file, "file", "", "get the checksum of a file")
}
func (p *SHA256Cmd) Execute(_ 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()
h, err := tools.Hash(sha256.New(), file)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitFailure
}
fmt.Printf("%s: %s\n", f.Name(), h)
return subcommands.ExitSuccess
}
for _, arg := range f.Args() {
h, err := tools.Hash(sha256.New(), strings.NewReader(arg))
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitFailure
}
fmt.Printf("%s: %s\n", arg, h)
}
return subcommands.ExitSuccess
}

63
commands/sha512/sha512.go Normal file
View File

@@ -0,0 +1,63 @@
package sha512
import (
"context"
"crypto/sha512"
"flag"
"fmt"
"hash_utils/tools"
"os"
"strings"
"github.com/google/subcommands"
)
type (
SHA512Cmd struct {
file string
}
)
func (*SHA512Cmd) Name() string { return "sha512" }
func (*SHA512Cmd) Synopsis() string { return "" }
func (*SHA512Cmd) Usage() string {
return `Usage: hash_utils sha512
Options:
`
}
func (p *SHA512Cmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.file, "file", "", "get the checksum of a file")
}
func (p *SHA512Cmd) Execute(_ 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()
h, err := tools.Hash(sha512.New(), file)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitFailure
}
fmt.Printf("%s: %s\n", f.Name(), h)
return subcommands.ExitSuccess
}
for _, arg := range f.Args() {
h, err := tools.Hash(sha512.New(), strings.NewReader(arg))
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitFailure
}
fmt.Printf("%s: %s\n", arg, h)
}
return subcommands.ExitSuccess
}