/
crypto.go
82 lines (70 loc) · 1.71 KB
/
crypto.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cmd
import (
"crypto/md5"
"crypto/sha512"
"fmt"
"os"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var Filename string
func init() {
cryptoCmd.AddCommand(
cryptoMD5Cmd,
cryptoSHA256Cmd,
)
cryptoMD5Cmd.Flags().String("text", "", "string value")
}
var cryptoCmd = &cobra.Command{
Use: "crypto",
Short: "crypto package utilities",
Long: "crypto package utilities",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
var cryptoMD5Cmd = &cobra.Command{
Use: "md5",
Example: "md5 file_path ...",
Short: "md5 hash function on files in arguments",
Long: "md5 hash function on files in arguments",
Run: func(cmd *cobra.Command, args []string) {
sValue, err := cmd.Flags().GetString("text")
if err == nil && sValue != "" {
fmt.Printf("%s\tMD5: %x\n", sValue, md5.Sum([]byte(sValue)))
return
}
if len(args) == 0 {
cmd.Help()
return
}
for _, filename := range args {
file, err := os.ReadFile(filename)
if err != nil {
log.Info().Err(err).Str("filename", filename).Msgf("Error reading file!")
continue
}
fmt.Printf("%s\tMD5: %x\n", filename, md5.Sum(file))
}
},
}
var cryptoSHA256Cmd = &cobra.Command{
Use: "sha512",
Example: "sha512 example_text",
Short: "sha512/256 hash function on files in arguments",
Long: "sha512/256 hash function on files in arguments",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
return
}
for _, filename := range args {
file, err := os.ReadFile(filename)
if err != nil {
log.Info().Err(err).Str("filename", filename).Msgf("Error reading file!")
continue
}
fmt.Printf("%s\tSHA512/256: %x\n", filename, sha512.Sum512_256(file))
}
},
}