add more algo

This commit is contained in:
2025-08-12 21:03:07 +02:00
parent f15b193482
commit 61eab24ba4
12 changed files with 494 additions and 7 deletions

83
commands/argon2/argon2.go Normal file
View File

@@ -0,0 +1,83 @@
package argon2
import (
"context"
"crypto/rand"
"encoding/base64"
"flag"
"fmt"
"os"
"runtime"
"github.com/google/subcommands"
"golang.org/x/crypto/argon2"
)
type (
Argon2Cmd struct {
memory int
iterations int
parallelism int
saltLength int
keyLength int
version bool
}
)
func (*Argon2Cmd) Name() string { return "argon2" }
func (*Argon2Cmd) Synopsis() string { return "" }
func (*Argon2Cmd) Usage() string {
return `Usage: hash_utils argon2
Options:
`
}
func (p *Argon2Cmd) SetFlags(f *flag.FlagSet) {
f.IntVar(&p.memory, "memory", 64*1024, "the amount of memory used by the algorithm (in kibibytes)")
f.IntVar(&p.iterations, "iterations", 3, "the number of iterations over the memory.")
f.IntVar(&p.parallelism, "parallelism", runtime.NumCPU(), "the number of threads used by the algorithm.")
f.IntVar(&p.saltLength, "salt-length", 16, "length of the random salt. 16 bytes is recommended for password hashing")
f.IntVar(&p.keyLength, "key-length", 16, "length of the generated key (or password hash). 16 bytes or more is recommended")
f.BoolVar(&p.version, "v", false, "show the version of argon2")
}
func (p *Argon2Cmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if p.version {
fmt.Println(argon2.Version)
return subcommands.ExitSuccess
}
if f.NArg() != 1 {
fmt.Fprintln(os.Stderr, "error: too many or no argument. this command required 1 argument")
return subcommands.ExitUsageError
}
salt, err := salt(p.saltLength)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitUsageError
}
hash := argon2.IDKey([]byte(f.Arg(0)), salt, uint32(p.iterations), uint32(p.memory), uint8(p.parallelism), uint32(p.keyLength))
fmt.Println(p.toString(salt, hash))
return subcommands.ExitSuccess
}
func salt(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
func (p *Argon2Cmd) toString(s, h []byte) string {
return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
argon2.Version,
p.memory,
p.iterations,
p.parallelism,
base64.RawStdEncoding.EncodeToString(s),
base64.RawStdEncoding.EncodeToString(h))
}

112
commands/crc16/crc16.go Normal file
View File

@@ -0,0 +1,112 @@
package crc16
import (
"context"
"flag"
"fmt"
"hash_utils/tools"
"os"
"github.com/google/subcommands"
"github.com/sigurn/crc16"
)
type (
CRC16Cmd struct {
file string
table string
}
)
var (
tables = []crc16.Params{
crc16.CRC16_ARC,
crc16.CRC16_AUG_CCITT,
crc16.CRC16_BUYPASS,
crc16.CRC16_CCITT_FALSE,
crc16.CRC16_CDMA2000,
crc16.CRC16_CRC_A,
crc16.CRC16_DDS_110,
crc16.CRC16_DECT_R,
crc16.CRC16_DECT_X,
crc16.CRC16_DNP,
crc16.CRC16_EN_13757,
crc16.CRC16_GENIBUS,
crc16.CRC16_KERMIT,
crc16.CRC16_MAXIM,
crc16.CRC16_MCRF4XX,
crc16.CRC16_MODBUS,
crc16.CRC16_RIELLO,
crc16.CRC16_T10_DIF,
crc16.CRC16_TELEDISK,
crc16.CRC16_TMS37157,
crc16.CRC16_USB,
crc16.CRC16_XMODEM,
crc16.CRC16_X_25,
}
)
func (*CRC16Cmd) Name() string { return "crc16" }
func (*CRC16Cmd) Synopsis() string { return "" }
func (*CRC16Cmd) Usage() string {
res := "Usage: hash_utils crc16\n\n"
res += "Algorithm available:\n"
for _, table := range tables {
res += " - " + table.Name + "\n"
}
res += "\nOptions:\n"
return res
}
func (p *CRC16Cmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.file, "file", "", "get the checksum of a file")
f.StringVar(&p.table, "table", crc16.CRC16_ARC.Name, "Predefined CRC-16 algorithms")
}
func (p *CRC16Cmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
var err error
param := crc16.CRC16_ARC
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.Println()
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.CRC16(b, param)
fmt.Printf("%s: %s\n", f.Name(), h)
return subcommands.ExitSuccess
}
for _, arg := range f.Args() {
h := tools.CRC16([]byte(arg), param)
fmt.Printf("%s: %s\n", arg, h)
}
return subcommands.ExitSuccess
}
func parse(tableName string) (crc16.Params, error) {
for _, table := range tables {
if table.Name == tableName {
return table, nil
}
}
return crc16.CRC16_ARC, fmt.Errorf("invalid table name: %s", tableName)
}

83
commands/crc32/crc32.go Normal file
View File

@@ -0,0 +1,83 @@
package crc32
import (
"context"
"flag"
"fmt"
"hash/crc32"
"hash_utils/tools"
"os"
"github.com/google/subcommands"
)
type (
CRC32Cmd struct {
file string
table string
}
)
func (*CRC32Cmd) Name() string { return "crc32" }
func (*CRC32Cmd) Synopsis() string { return "" }
func (*CRC32Cmd) Usage() string {
return `Usage: hash_utils crc32
Algorithm available:
- Castagnoli
- Koopman
- IEEE
Options:
`
}
func (p *CRC32Cmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.file, "file", "", "get the checksum of a file")
f.StringVar(&p.table, "table", "IEEE", "Predefined CRC-32 algorithms")
}
func (p *CRC32Cmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
var err error
var param uint32 = crc32.IEEE
if len(p.table) > 0 {
param, err = parse(p.table)
if err != nil {
fmt.Printf("Available tables: IEEE Castagnoli Koopman")
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.CRC32(b, param)
fmt.Printf("%s: %s\n", f.Name(), h)
return subcommands.ExitSuccess
}
for _, arg := range f.Args() {
h := tools.CRC32([]byte(arg), param)
fmt.Printf("%s: %s\n", arg, h)
}
return subcommands.ExitSuccess
}
func parse(tableName string) (uint32, error) {
switch tableName {
case "Castagnoli":
return crc32.Castagnoli, nil
case "Koopman":
return crc32.Koopman, nil
case "IEEE":
return crc32.IEEE, nil
}
return 0, fmt.Errorf("invalid table name: %s", tableName)
}

80
commands/crc64/crc64.go Normal file
View File

@@ -0,0 +1,80 @@
package crc64
import (
"context"
"flag"
"fmt"
"hash/crc64"
"hash_utils/tools"
"os"
"github.com/google/subcommands"
)
type (
CRC64Cmd struct {
file string
table string
}
)
func (*CRC64Cmd) Name() string { return "crc64" }
func (*CRC64Cmd) Synopsis() string { return "" }
func (*CRC64Cmd) Usage() string {
return `Usage: hash_utils crc64
Algorithm available:
- ECMA
- ISO
Options:
`
}
func (p *CRC64Cmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.file, "file", "", "get the checksum of a file")
f.StringVar(&p.table, "table", "ISO", "Predefined CRC-64 algorithms")
}
func (p *CRC64Cmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
var err error
var param uint64 = crc64.ISO
if len(p.table) > 0 {
param, err = parse(p.table)
if err != nil {
fmt.Printf("Available tables: IEEE Castagnoli Koopman")
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.CRC64(b, param)
fmt.Printf("%s: %s\n", f.Name(), h)
return subcommands.ExitSuccess
}
for _, arg := range f.Args() {
h := tools.CRC64([]byte(arg), param)
fmt.Printf("%s: %s\n", arg, h)
}
return subcommands.ExitSuccess
}
func parse(tableName string) (uint64, error) {
switch tableName {
case "Castagnoli":
return crc64.ISO, nil
case "Koopman":
return crc64.ECMA, nil
}
return 0, fmt.Errorf("invalid table name: %s", tableName)
}

View File

@@ -36,10 +36,13 @@ var (
func (*CRC8Cmd) Name() string { return "crc8" }
func (*CRC8Cmd) Synopsis() string { return "" }
func (*CRC8Cmd) Usage() string {
return `Usage: hash_utils crc8
Options:
`
res := "Usage: hash_utils crc8\n\n"
res += "Algorithm available:\n"
for _, table := range tables {
res += " - " + table.Name + "\n"
}
res += "\nOptions:\n"
return res
}
func (p *CRC8Cmd) SetFlags(f *flag.FlagSet) {

84
commands/sha3/sha3.go Normal file
View File

@@ -0,0 +1,84 @@
package sha3
import (
"context"
"crypto/sha3"
"flag"
"fmt"
"hash_utils/tools"
"os"
"strings"
"github.com/google/subcommands"
)
type (
SHA3Cmd struct {
file string
length int
}
)
func (*SHA3Cmd) Name() string { return "sha3" }
func (*SHA3Cmd) Synopsis() string { return "" }
func (*SHA3Cmd) Usage() string {
return `Usage: hash_utils sha2
Options:
`
}
func (p *SHA3Cmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.file, "file", "", "get the checksum of a file")
f.IntVar(&p.length, "length", 256, "change the length, supported length is 224, 256, 384 and 512")
}
func (p *SHA3Cmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
h, err := parseBitLength(p.length)
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
return subcommands.ExitUsageError
}
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(h, 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(h, 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
}
func parseBitLength(length int) (*sha3.SHA3, error) {
switch length {
case 224:
return sha3.New224(), nil
case 256:
return sha3.New256(), nil
case 384:
return sha3.New384(), nil
case 512:
return sha3.New512(), nil
}
return nil, fmt.Errorf("invalid length: supported length is 224, 256, 384 and 512")
}

View File

@@ -10,7 +10,7 @@ import (
)
const (
version string = "2.0.0"
version string = "2.0.1"
)
type (