123 lines
2.3 KiB
Go
123 lines
2.3 KiB
Go
![]() |
package main
|
||
|
|
||
|
import (
|
||
|
"crypto/md5"
|
||
|
"crypto/sha1"
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"github.com/oylshe1314/framework/util"
|
||
|
"golang.org/x/crypto/md4"
|
||
|
"golang.org/x/crypto/sha3"
|
||
|
"hash"
|
||
|
"os"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type strAry []string
|
||
|
|
||
|
func (this *strAry) Set(str string) error {
|
||
|
*this = append(*this, str)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (this *strAry) String() string {
|
||
|
return strings.Join(*this, " ")
|
||
|
}
|
||
|
|
||
|
const usage = `The hash calculator usage:
|
||
|
Options:
|
||
|
-a
|
||
|
A string to specify the algorithm of the hash value calculating(Case insensitive).
|
||
|
Provided algorithms include MD4/MD5/SHA1/SHA224/SHA256/SHA384/SHA512, default algorithms MD5.
|
||
|
|
||
|
-s
|
||
|
Specicy a string that you want to calculate the hash value.
|
||
|
|
||
|
-f
|
||
|
A string to specify the path of the file that you want to calculate the hash value.
|
||
|
|
||
|
-d
|
||
|
A string to specify a directory, The calculator will be calculated the hash value of all files in the directory specified.
|
||
|
|
||
|
-i
|
||
|
Specify if output the information for calculating hash valule
|
||
|
|
||
|
-m
|
||
|
Specify if merge all strings, files specified, and files in directories specified to calculate a final hash value.
|
||
|
The s/f/d options could be specified multiple and used with other options at the same time.
|
||
|
`
|
||
|
|
||
|
func main() {
|
||
|
|
||
|
var algo string
|
||
|
var strs strAry
|
||
|
var dirs strAry
|
||
|
var files strAry
|
||
|
|
||
|
var info bool
|
||
|
var merge bool
|
||
|
|
||
|
flag.StringVar(&algo, "a", "", "")
|
||
|
flag.Var(&strs, "s", "")
|
||
|
flag.Var(&dirs, "d", "")
|
||
|
flag.Var(&files, "f", "")
|
||
|
flag.BoolVar(&info, "i", false, "")
|
||
|
flag.BoolVar(&merge, "m", false, "")
|
||
|
|
||
|
flag.Usage = func() {
|
||
|
fmt.Print(usage)
|
||
|
}
|
||
|
|
||
|
flag.Parse()
|
||
|
|
||
|
if len(strs) == 0 && len(files) == 0 && len(dirs) == 0 {
|
||
|
flag.Usage()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var h hash.Hash
|
||
|
if algo == "" {
|
||
|
algo = "md5"
|
||
|
} else {
|
||
|
algo = strings.ToLower(algo)
|
||
|
}
|
||
|
|
||
|
switch algo {
|
||
|
case "md4":
|
||
|
h = md4.New()
|
||
|
case "md5":
|
||
|
h = md5.New()
|
||
|
case "sha1":
|
||
|
h = sha1.New()
|
||
|
case "sha224":
|
||
|
h = sha3.New224()
|
||
|
case "sha256":
|
||
|
h = sha3.New256()
|
||
|
case "sha384":
|
||
|
h = sha3.New384()
|
||
|
case "sha512":
|
||
|
h = sha3.New512()
|
||
|
default:
|
||
|
fmt.Println("Unsupported hash algorithm")
|
||
|
os.Exit(1)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var hashs, infos, err = util.HashAll(h, merge, strs, dirs, files)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
os.Exit(1)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if !info {
|
||
|
for i := range hashs {
|
||
|
fmt.Println(hashs[i])
|
||
|
}
|
||
|
} else {
|
||
|
for i := range hashs {
|
||
|
fmt.Println(hashs[i], infos[i])
|
||
|
}
|
||
|
}
|
||
|
}
|