-
Notifications
You must be signed in to change notification settings - Fork 32
/
crypto.go
202 lines (154 loc) · 3.21 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package processors
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"golang.org/x/crypto/bcrypt"
)
// MD5 encode string to md5.
type MD5 struct{}
func (p MD5) Name() string {
return "md5"
}
func (p MD5) Alias() []string {
return []string{"md5-sum"}
}
func (p MD5) Transform(data []byte, _ ...Flag) (string, error) {
hasher := md5.New()
hasher.Write(data)
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func (p MD5) Flags() []Flag {
return nil
}
func (p MD5) Title() string {
return "MD5 Sum"
}
func (p MD5) Description() string {
return "Get the MD5 checksum of your text"
}
func (p MD5) FilterValue() string {
return p.Title()
}
// SHA1 encode string to sha1.
type SHA1 struct{}
func (p SHA1) Name() string {
return "sha1"
}
func (p SHA1) Alias() []string {
return []string{"sha1-sum"}
}
func (p SHA1) Transform(data []byte, _ ...Flag) (string, error) {
h := sha1.New()
h.Write(data)
bs := h.Sum(nil)
return fmt.Sprintf("%x", bs), nil
}
func (p SHA1) Flags() []Flag {
return nil
}
func (p SHA1) Title() string {
return "SHA1 Sum"
}
func (p SHA1) Description() string {
return "Get the SHA1 checksum of your text"
}
func (p SHA1) FilterValue() string {
return p.Title()
}
// SHA256 encode string to sha256.
type SHA256 struct{}
func (p SHA256) Name() string {
return "sha256"
}
func (p SHA256) Alias() []string {
return []string{"sha256-sum"}
}
func (p SHA256) Transform(data []byte, _ ...Flag) (string, error) {
h := sha256.New()
h.Write(data)
bs := h.Sum(nil)
return fmt.Sprintf("%x", bs), nil
}
func (p SHA256) Flags() []Flag {
return nil
}
func (p SHA256) Title() string {
return "SHA256 Sum"
}
func (p SHA256) Description() string {
return "Get the SHA256 checksum of your text"
}
func (p SHA256) FilterValue() string {
return p.Title()
}
// SHA512 encode string to sha256.
type SHA512 struct{}
func (p SHA512) Name() string {
return "sha512"
}
func (p SHA512) Alias() []string {
return []string{"sha512-sum"}
}
func (p SHA512) Transform(data []byte, _ ...Flag) (string, error) {
h := sha512.New()
h.Write(data)
bs := h.Sum(nil)
return fmt.Sprintf("%x", bs), nil
}
func (p SHA512) Flags() []Flag {
return nil
}
func (p SHA512) Title() string {
return "SHA512 Sum"
}
func (p SHA512) Description() string {
return "Get the SHA512 checksum of your text"
}
func (p SHA512) FilterValue() string {
return p.Title()
}
// Bcrypt encode string to Bcrypt.
type Bcrypt struct{}
func (p Bcrypt) Name() string {
return "bcrypt"
}
func (p Bcrypt) Alias() []string {
return []string{"bcrypt-hash"}
}
func (p Bcrypt) Transform(data []byte, f ...Flag) (string, error) {
var rounds int
for _, flag := range f {
if flag.Short == "r" {
r, ok := flag.Value.(int)
if ok {
rounds = r
}
}
}
bytes, err := bcrypt.GenerateFromPassword(data, rounds)
return string(bytes), err
}
func (p Bcrypt) Flags() []Flag {
return []Flag{
{
Name: "number-of-rounds",
Short: "r",
Desc: "Number of rounds",
Value: 10,
Type: FlagUint,
},
}
}
func (p Bcrypt) Title() string {
return "Bcrypt Hash"
}
func (p Bcrypt) Description() string {
return "Get the Bcrypt hash of your text"
}
func (p Bcrypt) FilterValue() string {
return p.Title()
}