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

View File

@@ -5,8 +5,14 @@ This is a simple tool box with basic functionality.
## Supported tools ## Supported tools
- BCrypt - BCrypt
- MD5 - MD5
- SHA (1, 256, 512) - SHA1
- SHA2
- SHA3
- CRC8 - CRC8
- Base64 (not a hash algo but idc) - CRC16
- CRC32
- CRC64
- Argon2
- Base64 (not a hash but idc)
more later... more later...

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) Name() string { return "crc8" }
func (*CRC8Cmd) Synopsis() string { return "" } func (*CRC8Cmd) Synopsis() string { return "" }
func (*CRC8Cmd) Usage() string { func (*CRC8Cmd) Usage() string {
return `Usage: hash_utils crc8 res := "Usage: hash_utils crc8\n\n"
res += "Algorithm available:\n"
Options: for _, table := range tables {
` res += " - " + table.Name + "\n"
}
res += "\nOptions:\n"
return res
} }
func (p *CRC8Cmd) SetFlags(f *flag.FlagSet) { 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 ( const (
version string = "2.0.0" version string = "2.0.1"
) )
type ( type (

3
go.mod
View File

@@ -4,6 +4,9 @@ go 1.24
require ( require (
github.com/google/subcommands v1.2.0 github.com/google/subcommands v1.2.0
github.com/sigurn/crc16 v0.0.0-20240131213347-83fcde1e29d1
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f
golang.org/x/crypto v0.41.0 golang.org/x/crypto v0.41.0
) )
require golang.org/x/sys v0.35.0 // indirect

4
go.sum
View File

@@ -1,6 +1,10 @@
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/sigurn/crc16 v0.0.0-20240131213347-83fcde1e29d1 h1:NVK+OqnavpyFmUiKfUMHrpvbCi2VFoWTrcpI7aDaJ2I=
github.com/sigurn/crc16 v0.0.0-20240131213347-83fcde1e29d1/go.mod h1:9/etS5gpQq9BJsJMWg1wpLbfuSnkm8dPF6FdW2JXVhA=
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f h1:1R9KdKjCNSd7F8iGTxIpoID9prlYH8nuNYKt0XvweHA= github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f h1:1R9KdKjCNSd7F8iGTxIpoID9prlYH8nuNYKt0XvweHA=
github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f/go.mod h1:vQhwQ4meQEDfahT5kd61wLAF5AAeh5ZPLVI4JJ/tYo8= github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f/go.mod h1:vQhwQ4meQEDfahT5kd61wLAF5AAeh5ZPLVI4JJ/tYo8=
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4= golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc= golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=

View File

@@ -3,8 +3,12 @@ package main
import ( import (
"context" "context"
"flag" "flag"
"hash_utils/commands/argon2"
"hash_utils/commands/base64" "hash_utils/commands/base64"
"hash_utils/commands/bcrypt" "hash_utils/commands/bcrypt"
"hash_utils/commands/crc16"
"hash_utils/commands/crc32"
"hash_utils/commands/crc64"
"hash_utils/commands/crc8" "hash_utils/commands/crc8"
"hash_utils/commands/md5" "hash_utils/commands/md5"
"hash_utils/commands/sha1" "hash_utils/commands/sha1"
@@ -28,8 +32,12 @@ func main() {
subcommands.Register(&sha512.SHA512Cmd{}, "unkeyed cryptographic hash functions") subcommands.Register(&sha512.SHA512Cmd{}, "unkeyed cryptographic hash functions")
subcommands.Register(&crc8.CRC8Cmd{}, "cyclic redundancy checks") subcommands.Register(&crc8.CRC8Cmd{}, "cyclic redundancy checks")
subcommands.Register(&crc16.CRC16Cmd{}, "cyclic redundancy checks")
subcommands.Register(&crc32.CRC32Cmd{}, "cyclic redundancy checks")
subcommands.Register(&crc64.CRC64Cmd{}, "cyclic redundancy checks")
subcommands.Register(&bcrypt.BCryptCmd{}, "password hashing functions") subcommands.Register(&bcrypt.BCryptCmd{}, "password hashing functions")
subcommands.Register(&argon2.Argon2Cmd{}, "password hashing functions")
subcommands.Register(&base64.Base64Cmd{}, "other") subcommands.Register(&base64.Base64Cmd{}, "other")

View File

@@ -4,8 +4,11 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"hash" "hash"
"hash/crc32"
"hash/crc64"
"io" "io"
"github.com/sigurn/crc16"
"github.com/sigurn/crc8" "github.com/sigurn/crc8"
) )
@@ -21,3 +24,21 @@ func CRC8(s []byte, param crc8.Params) string {
crc := crc8.Checksum(s, table) crc := crc8.Checksum(s, table)
return fmt.Sprintf("%X", crc) return fmt.Sprintf("%X", crc)
} }
func CRC16(s []byte, param crc16.Params) string {
table := crc16.MakeTable(param)
crc := crc16.Checksum(s, table)
return fmt.Sprintf("%X", crc)
}
func CRC32(s []byte, param uint32) string {
table := crc32.MakeTable(param)
crc := crc32.Checksum(s, table)
return fmt.Sprintf("%X", crc)
}
func CRC64(s []byte, param uint64) string {
table := crc64.MakeTable(param)
crc := crc64.Checksum(s, table)
return fmt.Sprintf("%X", crc)
}