ihash/ihash.go
xiaoqidun c56c6c7e55
Some checks reported errors
continuous-integration/drone/push Build was killed
continuous-integration/drone/tag Build is passing
feat(新增算法): 支撑sha384sum
2021-09-07 13:50:47 +08:00

238 lines
4.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"flag"
"fmt"
"golang.org/x/crypto/sha3"
"hash"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
)
const (
md5sum = "md5sum"
sha1sum = "sha1sum"
sha256sum = "sha256sum"
sha384sum = "sha384sum"
sha512sum = "sha512sum"
sha3sum224 = "sha3sum224"
sha3sum256 = "sha3sum256"
sha3sum384 = "sha3sum384"
sha3sum512 = "sha3sum512"
)
var (
args []string
author bool
install string
)
var typeList = []string{
md5sum,
sha1sum,
sha256sum,
sha384sum,
sha512sum,
sha3sum224,
sha3sum256,
sha3sum384,
sha3sum512,
}
func init() {
flag.BoolVar(&author, "author", false, "")
flag.StringVar(&install, "install", "", "")
flag.Parse()
args = flag.Args()
}
func main() {
if author {
AuthorInformation()
return
}
if "" != install {
InstallShortcut(install)
return
}
fileName := GetFileName()
hashIndex := InArray(fileName, typeList)
if -1 == hashIndex {
if 0 == len(args) {
return
}
hashType := args[0]
hashIndex = InArray(hashType, typeList)
if -1 == hashIndex {
return
}
args = args[1:]
}
if 0 == len(args) {
stdinBytes, _ := ioutil.ReadAll(os.Stdin)
fmt.Println(StrHash(typeList[hashIndex], bytes.TrimSpace(stdinBytes)))
return
}
hashFileList := GetFileList(args)
for _, hashFile := range hashFileList {
FileHash(typeList[hashIndex], hashFile)
}
}
func InArray(value interface{}, array interface{}) int {
switch array.(type) {
case []string:
for k, v := range array.([]string) {
if v == value.(string) {
return k
}
}
}
return -1
}
func StrHash(hashType string, hashData []byte) string {
var sum interface{}
switch hashType {
case md5sum:
sum = md5.Sum(hashData)
case sha1sum:
sum = sha1.Sum(hashData)
case sha256sum:
sum = sha256.Sum256(hashData)
case sha384sum:
sum = sha512.Sum384(hashData)
case sha512sum:
sum = sha512.Sum512(hashData)
case sha3sum224:
sum = sha3.Sum224(hashData)
case sha3sum256:
sum = sha3.Sum256(hashData)
case sha3sum384:
sum = sha3.Sum384(hashData)
case sha3sum512:
sum = sha3.Sum512(hashData)
}
return fmt.Sprintf("%x", sum)
}
func FileHash(hashType string, hashFile string) {
success := "%x %s"
fileOpen, err := os.Open(hashFile)
if err != nil {
return
}
defer func() {
_ = fileOpen.Close()
}()
fileStat, err := fileOpen.Stat()
if err != nil || fileStat.IsDir() {
return
}
readBytes := make([]byte, 8192)
var hashHandle interface{}
switch hashType {
case md5sum:
hashHandle = md5.New()
case sha1sum:
hashHandle = sha1.New()
case sha256sum:
hashHandle = sha256.New()
case sha384sum:
hashHandle = sha512.New384()
case sha512sum:
hashHandle = sha512.New()
case sha3sum224:
hashHandle = sha3.New224()
case sha3sum256:
hashHandle = sha3.New256()
case sha3sum384:
hashHandle = sha3.New384()
case sha3sum512:
hashHandle = sha3.New512()
}
for {
n, err := fileOpen.Read(readBytes)
if err != nil {
if err == io.EOF && n > 0 {
hashHandle.(hash.Hash).Write(readBytes[:n])
}
break
}
hashHandle.(hash.Hash).Write(readBytes[:n])
}
fmt.Println(fmt.Sprintf(success, hashHandle.(hash.Hash).Sum(nil), hashFile))
}
func GetFileName() string {
filePath, _ := os.Executable()
fileName := filepath.Base(filePath)
fileName = strings.TrimSuffix(fileName, filepath.Ext(fileName))
return fileName
}
func GetFileList(args []string) (fileList []string) {
var tempList []string
for _, v := range args {
globList, _ := filepath.Glob(v)
tempList = append(tempList, globList...)
}
sort.Strings(tempList)
for i := 0; i < len(tempList); i++ {
if i > 0 && tempList[i] == tempList[i-1] {
continue
}
fileList = append(fileList, tempList[i])
}
return
}
func InstallShortcut(dst string) {
execPath, err := os.Executable()
if err != nil {
return
}
filePath, err := filepath.Abs(dst)
if err != nil {
return
}
fileStat, err := os.Stat(filePath)
if err != nil || !fileStat.IsDir() {
return
}
osPathSeparator := string(os.PathSeparator)
if !strings.HasSuffix(filePath, osPathSeparator) {
filePath += osPathSeparator
}
for i := 0; i < len(typeList); i++ {
symlinkPath := filePath + typeList[i]
if "windows" == runtime.GOOS {
symlinkPath += ".exe"
}
err := os.Link(execPath, symlinkPath)
if err != nil {
fmt.Printf("为 %s\t<<===>>\t%s 创建链接错误\n", symlinkPath, execPath)
} else {
fmt.Printf("为 %s\t<<===>>\t%s 创建了硬链接\n", symlinkPath, execPath)
}
}
}
func AuthorInformation() {
fmt.Println("welcome to our website https://aite.xyz/")
fmt.Println("----------------------------------------")
fmt.Println("腾讯扣扣88966001")
fmt.Println("电子邮箱xiaoqidun@gmail.com")
fmt.Println("----------------------------------------")
}