You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
4.4 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "crypto/sha256"
  7. "crypto/sha512"
  8. "flag"
  9. "fmt"
  10. "golang.org/x/crypto/sha3"
  11. "hash"
  12. "io"
  13. "io/ioutil"
  14. "os"
  15. "path/filepath"
  16. "runtime"
  17. "sort"
  18. "strings"
  19. )
  20. var (
  21. args []string
  22. author bool
  23. install string
  24. )
  25. var typeList = []string{
  26. "md5sum",
  27. "sha1sum",
  28. "sha256sum",
  29. "sha512sum",
  30. "sha3sum224",
  31. "sha3sum256",
  32. "sha3sum384",
  33. "sha3sum512",
  34. }
  35. func init() {
  36. flag.BoolVar(&author, "author", false, "")
  37. flag.StringVar(&install, "install", "", "")
  38. flag.Parse()
  39. args = flag.Args()
  40. }
  41. func main() {
  42. if author {
  43. AuthorInformation()
  44. return
  45. }
  46. if "" != install {
  47. InstallShortcut(install)
  48. return
  49. }
  50. fileName := GetFileName()
  51. hashIndex := InArray(fileName, typeList)
  52. if -1 == hashIndex {
  53. if 0 == len(args) {
  54. return
  55. }
  56. hashType := args[0]
  57. hashIndex = InArray(hashType, typeList)
  58. if -1 == hashIndex {
  59. return
  60. }
  61. args = args[1:]
  62. }
  63. if 0 == len(args) {
  64. stdinBytes, _ := ioutil.ReadAll(os.Stdin)
  65. fmt.Println(StrHash(typeList[hashIndex], bytes.TrimSpace(stdinBytes)))
  66. return
  67. }
  68. hashFileList := GetFileList(args)
  69. for _, hashFile := range hashFileList {
  70. FileHash(typeList[hashIndex], hashFile)
  71. }
  72. }
  73. func InArray(value interface{}, array interface{}) int {
  74. switch array.(type) {
  75. case []string:
  76. for k, v := range array.([]string) {
  77. if v == value.(string) {
  78. return k
  79. }
  80. }
  81. }
  82. return -1
  83. }
  84. func StrHash(hashType string, hashData []byte) string {
  85. var sum interface{}
  86. switch hashType {
  87. case "md5sum":
  88. sum = md5.Sum(hashData)
  89. case "sha1sum":
  90. sum = sha1.Sum(hashData)
  91. case "sha256sum":
  92. sum = sha256.Sum256(hashData)
  93. case "sha512sum":
  94. sum = sha512.Sum512(hashData)
  95. case "sha3sum224":
  96. sum = sha3.Sum224(hashData)
  97. case "sha3sum256":
  98. sum = sha3.Sum256(hashData)
  99. case "sha3sum384":
  100. sum = sha3.Sum384(hashData)
  101. case "sha3sum512":
  102. sum = sha3.Sum512(hashData)
  103. }
  104. return fmt.Sprintf("%x", sum)
  105. }
  106. func FileHash(hashType string, hashFile string) {
  107. success := "%x %s"
  108. fileOpen, err := os.Open(hashFile)
  109. if err != nil {
  110. return
  111. }
  112. defer func() {
  113. _ = fileOpen.Close()
  114. }()
  115. fileStat, err := fileOpen.Stat()
  116. if err != nil || fileStat.IsDir() {
  117. return
  118. }
  119. readBytes := make([]byte, 8192)
  120. var hashHandle interface{}
  121. switch hashType {
  122. case "md5sum":
  123. hashHandle = md5.New()
  124. case "sha1sum":
  125. hashHandle = sha1.New()
  126. case "sha256sum":
  127. hashHandle = sha256.New()
  128. case "sha512sum":
  129. hashHandle = sha512.New()
  130. case "sha3sum224":
  131. hashHandle = sha3.New224()
  132. case "sha3sum256":
  133. hashHandle = sha3.New256()
  134. case "sha3sum384":
  135. hashHandle = sha3.New384()
  136. case "sha3sum512":
  137. hashHandle = sha3.New512()
  138. }
  139. for {
  140. n, err := fileOpen.Read(readBytes)
  141. if err != nil {
  142. if err == io.EOF && n > 0 {
  143. hashHandle.(hash.Hash).Write(readBytes[:n])
  144. }
  145. break
  146. }
  147. hashHandle.(hash.Hash).Write(readBytes[:n])
  148. }
  149. fmt.Println(fmt.Sprintf(success, hashHandle.(hash.Hash).Sum(nil), hashFile))
  150. }
  151. func GetFileName() string {
  152. filePath, _ := os.Executable()
  153. fileName := filepath.Base(filePath)
  154. fileName = strings.TrimSuffix(fileName, filepath.Ext(fileName))
  155. return fileName
  156. }
  157. func GetFileList(args []string) (fileList []string) {
  158. var tempList []string
  159. for _, v := range args {
  160. globList, _ := filepath.Glob(v)
  161. tempList = append(tempList, globList...)
  162. }
  163. sort.Strings(tempList)
  164. for i := 0; i < len(tempList); i++ {
  165. if i > 0 && tempList[i] == tempList[i-1] {
  166. continue
  167. }
  168. fileList = append(fileList, tempList[i])
  169. }
  170. return
  171. }
  172. func InstallShortcut(dst string) {
  173. execPath, err := os.Executable()
  174. if err != nil {
  175. return
  176. }
  177. filePath, err := filepath.Abs(dst)
  178. if err != nil {
  179. return
  180. }
  181. fileStat, err := os.Stat(filePath)
  182. if err != nil || !fileStat.IsDir() {
  183. return
  184. }
  185. osPathSeparator := string(os.PathSeparator)
  186. if !strings.HasSuffix(filePath, osPathSeparator) {
  187. filePath += osPathSeparator
  188. }
  189. for i := 0; i < len(typeList); i++ {
  190. symlinkPath := filePath + typeList[i]
  191. if "windows" == runtime.GOOS {
  192. symlinkPath += ".exe"
  193. }
  194. err := os.Link(execPath, symlinkPath)
  195. if err != nil {
  196. fmt.Printf("为 %s\t<<===>>\t%s 创建链接错误\n", symlinkPath, execPath)
  197. } else {
  198. fmt.Printf("为 %s\t<<===>>\t%s 创建了硬链接\n", symlinkPath, execPath)
  199. }
  200. }
  201. }
  202. func AuthorInformation() {
  203. fmt.Println("welcome to our website https://aite.xyz/")
  204. fmt.Println("----------------------------------------")
  205. fmt.Println("腾讯扣扣:88966001")
  206. fmt.Println("电子邮箱:xiaoqidun@gmail.com")
  207. fmt.Println("----------------------------------------")
  208. }