-
Notifications
You must be signed in to change notification settings - Fork 0
/
md5.go
53 lines (48 loc) · 1.05 KB
/
md5.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package sign
import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
)
var md5Cmd = &cobra.Command{
Use: "md5",
Short: `md5签名计算`,
Run: func(c *cobra.Command, args []string) {
input, _ := c.Flags().GetString("input")
if len(args) == 0 && len(input) == 0 {
c.Help()
}
if len(input) > 0 {
value, err := os.ReadFile(input)
if err != nil {
fmt.Println("sign file error, ", err)
} else {
md5sum(value)
}
}
if len(args) > 0 {
for _, content := range args {
md5sum([]byte(content))
}
}
},
}
func md5sum(content []byte) {
h := md5.New()
h.Write(content)
value := hex.EncodeToString(h.Sum(nil))
fmt.Println("source :", strings.Replace(string(content), "\n", "", -1))
fmt.Println("md5(32):", value)
fmt.Println(" :", strings.ToUpper(value))
fmt.Println("md5(16):", value[8:24])
fmt.Println(" :", strings.ToUpper(value[8:24]))
}
func init() {
md5Cmd.Flags().StringP("input", "i", "", "输入待验证的文件")
}
func NewMd5Cmd() *cobra.Command {
return md5Cmd
}