diff --git a/.gitignore b/.gitignore
index ea8c4bf..8d2fafc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1 @@
-/target
+/hash_utils
\ No newline at end of file
diff --git a/.idea/.gitignore b/.idea/.gitignore
deleted file mode 100644
index 13566b8..0000000
--- a/.idea/.gitignore
+++ /dev/null
@@ -1,8 +0,0 @@
-# Default ignored files
-/shelf/
-/workspace.xml
-# Editor-based HTTP Client requests
-/httpRequests/
-# Datasource local storage ignored files
-/dataSources/
-/dataSources.local.xml
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index 639900d..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index 1359cd1..0000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index 35eb1dd..0000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Cargo.lock b/Cargo.lock
deleted file mode 100644
index c767595..0000000
--- a/Cargo.lock
+++ /dev/null
@@ -1,373 +0,0 @@
-# This file is automatically @generated by Cargo.
-# It is not intended for manual editing.
-version = 3
-
-[[package]]
-name = "atty"
-version = "0.2.14"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
-dependencies = [
- "hermit-abi",
- "libc",
- "winapi",
-]
-
-[[package]]
-name = "autocfg"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
-
-[[package]]
-name = "base64"
-version = "0.13.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
-
-[[package]]
-name = "base64"
-version = "0.20.0-alpha.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "149ea5dc24cb11513350770afebba32b68e3d2e356f9221351a2a1ee89112a82"
-
-[[package]]
-name = "bcrypt"
-version = "0.10.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f691e63585950d8c1c43644d11bab9073e40f5060dd2822734ae7c3dc69a3a80"
-dependencies = [
- "base64 0.13.0",
- "blowfish",
- "getrandom",
-]
-
-[[package]]
-name = "bitflags"
-version = "1.3.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
-
-[[package]]
-name = "block-buffer"
-version = "0.10.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324"
-dependencies = [
- "generic-array",
-]
-
-[[package]]
-name = "blowfish"
-version = "0.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fe3ff3fc1de48c1ac2e3341c4df38b0d1bfb8fdf04632a187c8b75aaa319a7ab"
-dependencies = [
- "byteorder",
- "cipher",
- "opaque-debug",
-]
-
-[[package]]
-name = "byteorder"
-version = "1.4.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
-
-[[package]]
-name = "cfg-if"
-version = "1.0.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
-
-[[package]]
-name = "cipher"
-version = "0.3.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7"
-dependencies = [
- "generic-array",
-]
-
-[[package]]
-name = "clap"
-version = "3.1.18"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d2dbdf4bdacb33466e854ce889eee8dfd5729abf7ccd7664d0a2d60cd384440b"
-dependencies = [
- "atty",
- "bitflags",
- "clap_derive",
- "clap_lex",
- "indexmap",
- "lazy_static",
- "strsim",
- "termcolor",
- "textwrap",
-]
-
-[[package]]
-name = "clap_derive"
-version = "3.1.18"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "25320346e922cffe59c0bbc5410c8d8784509efb321488971081313cb1e1a33c"
-dependencies = [
- "heck",
- "proc-macro-error",
- "proc-macro2",
- "quote",
- "syn",
-]
-
-[[package]]
-name = "clap_lex"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213"
-dependencies = [
- "os_str_bytes",
-]
-
-[[package]]
-name = "crypto-common"
-version = "0.1.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8"
-dependencies = [
- "generic-array",
- "typenum",
-]
-
-[[package]]
-name = "digest"
-version = "0.10.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
-dependencies = [
- "block-buffer",
- "crypto-common",
-]
-
-[[package]]
-name = "generic-array"
-version = "0.14.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803"
-dependencies = [
- "typenum",
- "version_check",
-]
-
-[[package]]
-name = "getrandom"
-version = "0.2.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
-dependencies = [
- "cfg-if",
- "libc",
- "wasi",
-]
-
-[[package]]
-name = "hash_utils"
-version = "1.0.0"
-dependencies = [
- "base64 0.20.0-alpha.1",
- "bcrypt",
- "clap",
- "md-5",
-]
-
-[[package]]
-name = "hashbrown"
-version = "0.11.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
-
-[[package]]
-name = "heck"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
-
-[[package]]
-name = "hermit-abi"
-version = "0.1.19"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
-dependencies = [
- "libc",
-]
-
-[[package]]
-name = "indexmap"
-version = "1.8.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e6012d540c5baa3589337a98ce73408de9b5a25ec9fc2c6fd6be8f0d39e0ca5a"
-dependencies = [
- "autocfg",
- "hashbrown",
-]
-
-[[package]]
-name = "lazy_static"
-version = "1.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
-
-[[package]]
-name = "libc"
-version = "0.2.126"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
-
-[[package]]
-name = "md-5"
-version = "0.10.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "658646b21e0b72f7866c7038ab086d3d5e1cd6271f060fd37defb241949d0582"
-dependencies = [
- "digest",
-]
-
-[[package]]
-name = "opaque-debug"
-version = "0.3.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
-
-[[package]]
-name = "os_str_bytes"
-version = "6.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "21326818e99cfe6ce1e524c2a805c189a99b5ae555a35d19f9a284b427d86afa"
-
-[[package]]
-name = "proc-macro-error"
-version = "1.0.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
-dependencies = [
- "proc-macro-error-attr",
- "proc-macro2",
- "quote",
- "syn",
- "version_check",
-]
-
-[[package]]
-name = "proc-macro-error-attr"
-version = "1.0.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
-dependencies = [
- "proc-macro2",
- "quote",
- "version_check",
-]
-
-[[package]]
-name = "proc-macro2"
-version = "1.0.39"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f"
-dependencies = [
- "unicode-ident",
-]
-
-[[package]]
-name = "quote"
-version = "1.0.18"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
-dependencies = [
- "proc-macro2",
-]
-
-[[package]]
-name = "strsim"
-version = "0.10.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
-
-[[package]]
-name = "syn"
-version = "1.0.95"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fbaf6116ab8924f39d52792136fb74fd60a80194cf1b1c6ffa6453eef1c3f942"
-dependencies = [
- "proc-macro2",
- "quote",
- "unicode-ident",
-]
-
-[[package]]
-name = "termcolor"
-version = "1.1.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
-dependencies = [
- "winapi-util",
-]
-
-[[package]]
-name = "textwrap"
-version = "0.15.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
-
-[[package]]
-name = "typenum"
-version = "1.15.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
-
-[[package]]
-name = "unicode-ident"
-version = "1.0.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
-
-[[package]]
-name = "version_check"
-version = "0.9.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
-
-[[package]]
-name = "wasi"
-version = "0.10.2+wasi-snapshot-preview1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
-
-[[package]]
-name = "winapi"
-version = "0.3.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
-dependencies = [
- "winapi-i686-pc-windows-gnu",
- "winapi-x86_64-pc-windows-gnu",
-]
-
-[[package]]
-name = "winapi-i686-pc-windows-gnu"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
-
-[[package]]
-name = "winapi-util"
-version = "0.1.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
-dependencies = [
- "winapi",
-]
-
-[[package]]
-name = "winapi-x86_64-pc-windows-gnu"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/Cargo.toml b/Cargo.toml
deleted file mode 100644
index 39d0410..0000000
--- a/Cargo.toml
+++ /dev/null
@@ -1,15 +0,0 @@
-[package]
-name = "hash_utils"
-version = "1.0.0"
-edition = "2018"
-
-# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
-
-[dependencies]
-bcrypt = "0.10"
-clap = { version = "3.1.18", features = ["derive"] }
-md-5 = "0.10.1"
-base64 = "0.20.0-alpha.1"
-
-[profile.release]
-opt-level = 3
\ No newline at end of file
diff --git a/README.md b/README.md
index cbb8486..2c81e73 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,12 @@
-# hash_utils
-
-This is a simple tool box with basic functionality.
-
-## Supported tools
- - BCrypt
- - MD5
- - Base64 (not a hash algo but idc)
\ No newline at end of file
+# hash_utils
+
+This is a simple tool box with basic functionality.
+
+## Supported tools
+ - BCrypt
+ - MD5
+ - SHA (1, 256, 512)
+ - CRC8
+ - Base64 (not a hash algo but idc)
+
+more later...
\ No newline at end of file
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..24f0b2b
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+MAKE_PACKAGE=false
+
+usage() {
+ echo "Usage: $0 [OPTIONS]"
+ echo "Options:"
+ echo " --package Make a delivery package instead of plain binary"
+}
+
+# Function to handle options and arguments
+handle_options() {
+ while [ $# -gt 0 ]; do
+ case $1 in
+ --package)
+ MAKE_PACKAGE=true
+ ;;
+ *)
+ echo "Invalid option: $1" >&2
+ usage
+ exit 1
+ ;;
+ esac
+ shift
+ done
+}
+
+# Main script execution
+handle_options "$@"
+
+if [ ! -d "./build" ]; then
+ mkdir ./build
+fi
+
+platforms=("linux/386" "linux/amd64" "linux/arm64" "linux/riscv64" "darwin/arm64" "windows/arm64" "windows/amd64" "freebsd/amd64" "android/arm64")
+
+for platform in "${platforms[@]}"; do
+ echo "* Compiling for $platform..."
+ platform_split=(${platform//\// })
+
+ EXT=""
+ if [ "${platform_split[0]}" == "windows" ]; then
+ EXT=.exe
+ fi
+
+ if [ "$MAKE_PACKAGE" == "true" ]; then
+ CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} go build -o build/hash_utils$EXT -a
+ tar -czf build/hash_utils_${platform_split[0]}_${platform_split[1]}.tar.gz build/hash_utils$EXT
+ rm build/hash_utils$EXT
+ else
+ CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} go build -o build/hash_utils_${platform_split[0]}_${platform_split[1]}$EXT -a
+ fi
+done
diff --git a/commands/base64/base64.go b/commands/base64/base64.go
new file mode 100644
index 0000000..e2d8157
--- /dev/null
+++ b/commands/base64/base64.go
@@ -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
+}
diff --git a/commands/bcrypt/bcrypt.go b/commands/bcrypt/bcrypt.go
new file mode 100644
index 0000000..cb06559
--- /dev/null
+++ b/commands/bcrypt/bcrypt.go
@@ -0,0 +1,62 @@
+package bcrypt
+
+import (
+ "context"
+ "flag"
+ "fmt"
+ "os"
+
+ "github.com/google/subcommands"
+ "golang.org/x/crypto/bcrypt"
+)
+
+type (
+ BCryptCmd struct {
+ cost int
+ check bool
+ }
+)
+
+func (*BCryptCmd) Name() string { return "bcrypt" }
+func (*BCryptCmd) Synopsis() string { return "" }
+func (*BCryptCmd) Usage() string {
+ return `Usage: hash_utils bcrypt
+
+Options:
+`
+}
+
+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 *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
+}
diff --git a/commands/crc8/crc8.go b/commands/crc8/crc8.go
new file mode 100644
index 0000000..3886670
--- /dev/null
+++ b/commands/crc8/crc8.go
@@ -0,0 +1,96 @@
+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.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.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)
+}
diff --git a/commands/md5/md5.go b/commands/md5/md5.go
new file mode 100644
index 0000000..b2feec6
--- /dev/null
+++ b/commands/md5/md5.go
@@ -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
+}
diff --git a/commands/sha1/sha1.go b/commands/sha1/sha1.go
new file mode 100644
index 0000000..41f03c0
--- /dev/null
+++ b/commands/sha1/sha1.go
@@ -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
+}
diff --git a/commands/sha256/sha256.go b/commands/sha256/sha256.go
new file mode 100644
index 0000000..5c90f7b
--- /dev/null
+++ b/commands/sha256/sha256.go
@@ -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
+}
diff --git a/commands/sha512/sha512.go b/commands/sha512/sha512.go
new file mode 100644
index 0000000..dc4920b
--- /dev/null
+++ b/commands/sha512/sha512.go
@@ -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
+}
diff --git a/commands/version/version.go b/commands/version/version.go
new file mode 100644
index 0000000..5ea4886
--- /dev/null
+++ b/commands/version/version.go
@@ -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
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..ed40445
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,9 @@
+module hash_utils
+
+go 1.24
+
+require (
+ github.com/google/subcommands v1.2.0
+ github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f
+ golang.org/x/crypto v0.41.0
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..c2b0f92
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,6 @@
+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/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f h1:1R9KdKjCNSd7F8iGTxIpoID9prlYH8nuNYKt0XvweHA=
+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/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
diff --git a/hash_utils.iml b/hash_utils.iml
deleted file mode 100644
index 2fecef3..0000000
--- a/hash_utils.iml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..0ca5cf2
--- /dev/null
+++ b/main.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+ "context"
+ "flag"
+ "hash_utils/commands/base64"
+ "hash_utils/commands/bcrypt"
+ "hash_utils/commands/crc8"
+ "hash_utils/commands/md5"
+ "hash_utils/commands/sha1"
+ "hash_utils/commands/sha256"
+ "hash_utils/commands/sha512"
+ "hash_utils/commands/version"
+ "os"
+
+ "github.com/google/subcommands"
+)
+
+func main() {
+ subcommands.Register(subcommands.HelpCommand(), "help")
+ subcommands.Register(subcommands.FlagsCommand(), "help")
+ subcommands.Register(subcommands.CommandsCommand(), "help")
+ subcommands.Register(&version.VersionCmd{}, "help")
+
+ subcommands.Register(&md5.MD5Cmd{}, "unkeyed cryptographic hash functions")
+ subcommands.Register(&sha1.SHA1Cmd{}, "unkeyed cryptographic hash functions")
+ subcommands.Register(&sha256.SHA256Cmd{}, "unkeyed cryptographic hash functions")
+ subcommands.Register(&sha512.SHA512Cmd{}, "unkeyed cryptographic hash functions")
+
+ subcommands.Register(&crc8.CRC8Cmd{}, "cyclic redundancy checks")
+
+ subcommands.Register(&bcrypt.BCryptCmd{}, "password hashing functions")
+
+ subcommands.Register(&base64.Base64Cmd{}, "other")
+
+ flag.Parse()
+ ctx := context.Background()
+
+ os.Exit(int(subcommands.Execute(ctx)))
+}
diff --git a/src/base64_utils.rs b/src/base64_utils.rs
deleted file mode 100644
index 560d152..0000000
--- a/src/base64_utils.rs
+++ /dev/null
@@ -1,37 +0,0 @@
-use clap::{Subcommand};
-use std::str;
-
-#[derive(Subcommand)]
-pub enum Base64SubCommands {
- /// Encode the text in base64
- Encode {
- /// The text to encode
- text: Option,
- },
-
- /// Decode the text
- Decode {
- /// Base64 encoded text
- text: Option,
- },
-}
-
-pub fn do_base64_encode(text: String) {
- let b64 = base64::encode(text);
- println!("{}", b64);
-}
-
-pub fn do_base64_decode(text: String) {
- let res = base64::decode(text);
- if res.is_err() {
- eprintln!("E: {}", res.unwrap_err());
- return;
- }
- let bytes = res.unwrap();
- let utf8_text = str::from_utf8(&bytes);
- if utf8_text.is_err() {
- eprintln!("E: {}", utf8_text.unwrap_err());
- return;
- }
- println!("{}", utf8_text.unwrap());
-}
\ No newline at end of file
diff --git a/src/bcrypt_utils.rs b/src/bcrypt_utils.rs
deleted file mode 100644
index 45fa43e..0000000
--- a/src/bcrypt_utils.rs
+++ /dev/null
@@ -1,42 +0,0 @@
-extern crate bcrypt;
-
-use bcrypt::{DEFAULT_COST, hash, verify};
-use clap::{Subcommand};
-
-#[derive(Subcommand)]
-pub enum BCryptSubCommands {
- /// Hash the text and print it
- Hash {
- /// How many times to run the hash command before print result
- #[clap(short, long, default_value_t = DEFAULT_COST)]
- cost: u32,
- /// The text to hash
- text: Option,
- },
-
- /// Verify the text hashed and print the result
- Verify {
- /// The hash to verify
- hash: Option,
- /// The clear text that need to be verified with the hash
- text: Option,
- },
-}
-
-pub fn do_bcrypt_hash(text: String, cost: u32) {
- let hashed = hash(text, cost);
- if hashed.is_err() {
- eprintln!("E: {}", hashed.err().unwrap())
- } else {
- println!("{}", hashed.unwrap());
- }
-}
-
-pub fn do_bcrypt_verify(hash: String, text: String) {
- let valid = verify(text, &hash);
- if valid.is_err() {
- eprintln!("E: {}", valid.err().unwrap())
- } else {
- println!("{} : {}", hash, if valid.unwrap() { "true" } else { "false" });
- }
-}
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
deleted file mode 100644
index 73e4289..0000000
--- a/src/main.rs
+++ /dev/null
@@ -1,107 +0,0 @@
-mod bcrypt_utils;
-mod md5_utils;
-mod base64_utils;
-
-pub use crate::bcrypt_utils::{do_bcrypt_hash, do_bcrypt_verify, BCryptSubCommands};
-pub use crate::md5_utils::{do_md5_hash, do_md5_verify, MD5SubCommands};
-pub use crate::base64_utils::{do_base64_encode, do_base64_decode, Base64SubCommands};
-use clap::{Parser, Subcommand};
-
-#[derive(Parser)]
-#[clap(author, version, about, long_about = None)]
-struct Cli {
- #[clap(subcommand)]
- command: Option,
-}
-
-#[derive(Subcommand)]
-enum Commands {
- /// Use the bcrypt algorithm
- Bcrypt {
- #[clap(subcommand)]
- command: Option,
- },
- /// Use the md5 algorithm
- MD5 {
- #[clap(subcommand)]
- command: Option,
- },
- /// Encode and decode base64 text (I know, it's not a hash but I need it in my tools)
- Base64 {
- #[clap(subcommand)]
- command: Option,
- },
-}
-
-fn main() {
- let cmd = Cli::parse();
-
- match &cmd.command {
- Some(Commands::Bcrypt { command }) => {
- match &command {
- Some(BCryptSubCommands::Hash { cost, text }) => {
- if let Some(value) = text {
- do_bcrypt_hash(String::from(value), *cost);
- } else {
- eprintln!("E: Missing the text value")
- }
- }
- Some(BCryptSubCommands::Verify { hash, text }) => {
- if let Some(value) = text {
- if let Some(hashed_value) = hash {
- do_bcrypt_verify(String::from(hashed_value), String::from(value));
- } else {
- eprintln!("E: Missing the hash")
- }
- } else {
- eprintln!("E: Missing the text value")
- }
- }
- None => {eprintln!("E: Subcommand invalid, use 'help' to show subcommand available")}
- }
- }
- Some(Commands::MD5 { command }) => {
- match &command {
- Some(MD5SubCommands::Hash { text }) => {
- if let Some(value) = text {
- do_md5_hash(String::from(value));
- } else {
- eprintln!("E: Missing the text value")
- }
- }
- Some(MD5SubCommands::Verify { hash, text }) => {
- if let Some(value) = text {
- if let Some(hashed_value) = hash {
- do_md5_verify(String::from(hashed_value), String::from(value));
- } else {
- eprintln!("E: Missing the hash")
- }
- } else {
- eprintln!("E: Missing the text value")
- }
- }
- None => {eprintln!("E: Subcommand invalid, use 'help' to show subcommand available")}
- }
- }
- Some(Commands::Base64 { command }) => {
- match &command {
- Some(Base64SubCommands::Encode { text }) => {
- if let Some(value) = text {
- do_base64_encode(String::from(value));
- } else {
- eprintln!("E: Missing the text value")
- }
- }
- Some(Base64SubCommands::Decode { text }) => {
- if let Some(value) = text {
- do_base64_decode(String::from(value));
- } else {
- eprintln!("E: Missing the encoded value")
- }
- }
- None => {eprintln!("E: Subcommand invalid, use 'help' to show subcommand available")}
- }
- }
- None => {eprintln!("E: Subcommand invalid, use 'help' to show subcommand available")}
- }
-}
\ No newline at end of file
diff --git a/src/md5_utils.rs b/src/md5_utils.rs
deleted file mode 100644
index 112d2a0..0000000
--- a/src/md5_utils.rs
+++ /dev/null
@@ -1,33 +0,0 @@
-use md5::{Md5, Digest};
-use clap::{Subcommand};
-
-#[derive(Subcommand)]
-pub enum MD5SubCommands {
- /// Hash the text and print it
- Hash {
- /// The text to hash
- text: Option,
- },
-
- /// Verify the text hashed and print the result
- Verify {
- /// The hash to verify
- hash: Option,
- /// The clear text that need to be verified with the hash
- text: Option,
- },
-}
-
-pub fn do_md5_hash(text: String) {
- let mut hasher = Md5::new();
- hasher.update(text);
- let result = hasher.finalize();
- println!("{:x}", result)
-}
-
-pub fn do_md5_verify(hash: String, text: String) {
- let mut hasher = Md5::new();
- hasher.update(text);
- let result = hasher.finalize();
- println!("{} : {}", hash, if format!("{:x}", result) == hash { "true" } else { "false" });
-}
\ No newline at end of file
diff --git a/tools/tools.go b/tools/tools.go
new file mode 100644
index 0000000..c9f370e
--- /dev/null
+++ b/tools/tools.go
@@ -0,0 +1,23 @@
+package tools
+
+import (
+ "encoding/hex"
+ "fmt"
+ "hash"
+ "io"
+
+ "github.com/sigurn/crc8"
+)
+
+func Hash(h hash.Hash, r io.Reader) (string, error) {
+ if _, err := io.Copy(h, r); err != nil {
+ return "", fmt.Errorf("failed to read string: %w", err)
+ }
+ return hex.EncodeToString(h.Sum(nil)), nil
+}
+
+func CRC8(s []byte, param crc8.Params) string {
+ table := crc8.MakeTable(param)
+ crc := crc8.Checksum(s, table)
+ return fmt.Sprintf("%X", crc)
+}