-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
283 lines (248 loc) Β· 6.17 KB
/
main.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"crypto"
"crypto/rand"
"embed"
"encoding/hex"
"fmt"
"github.com/zserge/lorca"
"log"
"net"
"net/http"
"os"
"os/signal"
"runtime"
simplersa "simple-rsa/lib-simplersa"
)
//go:embed www
var fs embed.FS
var key_bits, key_nprimes int
var priv *simplersa.PrivateKey
func GenerateRSAKey(nprimes, bits int) {
var err error
key_nprimes, key_bits = nprimes, bits
priv, err = simplersa.GenerateMultiPrimeKey(rand.Reader, nprimes, bits)
if err != nil {
return
}
//return priv.N.String(), priv.D.String(), string(priv.E)
}
func ResetRSAKey() {
priv = nil
}
func GetN(hex bool) string {
if priv == nil {
return ""
}
if hex {
return fmt.Sprintf("0x%x", priv.N)
}
return priv.N.String()
}
func GetD(hex bool) string {
if priv == nil {
return ""
}
if hex {
return fmt.Sprintf("0x%x", priv.D)
}
return priv.D.String()
}
func GetE(hex bool) string {
if priv == nil {
return ""
}
if hex {
return fmt.Sprintf("0x%x", priv.E)
}
return fmt.Sprintf("%v", priv.E)
}
func GetPrimes(hex bool) []string {
if priv == nil {
return []string{"Please Generate A RSA Key"}
}
var strPrimeList = []string{}
if hex {
for _, p := range priv.Primes {
strPrimeList = append(strPrimeList, fmt.Sprintf("0x%x", p))
}
return strPrimeList
}
for _, p := range priv.Primes {
strPrimeList = append(strPrimeList, p.String())
}
return strPrimeList
}
var (
ErrNoKey = "Please Generate a RSA Key \U0001FA84\U0001FA84\U0001FA84"
ErrDecrypt = "Decrypt Error π’π’π’"
ErrEncrypt = "Encrypt Error π’π’π’"
ErrSign = "Sign Error π’π’π’"
VerifyTrue = "βοΈ Signature is Correct πππ "
VerifyFalse = "β Signature is Wrong βββ "
)
var str2hash map[string]crypto.Hash
func getCryptoHash(hashName string) crypto.Hash {
if str2hash == nil {
str2hash = make(map[string]crypto.Hash)
for h := crypto.MD4; h <= crypto.BLAKE2b_512; h++ {
str2hash[h.String()] = h
}
}
if hash, ok := str2hash[hashName]; ok {
return hash
} else {
return crypto.SHA256
}
}
func Encrypt(plaintext string, isUseOAEP bool, OAEPLabel string, hashName string) string {
if priv == nil {
return ErrNoKey
}
var (
ciphertext []byte
err error
)
msg, rng, hash := []byte(plaintext), rand.Reader, getCryptoHash(hashName)
if isUseOAEP {
var label []byte
if OAEPLabel != "" {
label = []byte(OAEPLabel)
}
ciphertext, err = simplersa.EncryptOAEP(hash.New(), rng, &priv.PublicKey, msg, label)
} else {
ciphertext, err = simplersa.EncryptPKCS1v15(rng, &priv.PublicKey, msg)
}
if err != nil {
return ErrEncrypt
}
return fmt.Sprintf("%x", ciphertext)
}
func Decrypt(c string, isUseOAEP bool, OAEPLabel string, hashName string) string {
if priv == nil {
return ErrNoKey
}
var (
plaintext []byte
err error
)
ciphertext, err := hex.DecodeString(c)
if err != nil {
return ""
}
rng, hash := rand.Reader, getCryptoHash(hashName)
if isUseOAEP {
var label []byte
if OAEPLabel != "" {
label = []byte(OAEPLabel)
}
plaintext, err = simplersa.DecryptOAEP(hash.New(), rng, priv, ciphertext, label)
} else {
plaintext, err = simplersa.DecryptPKCS1v15(rng, priv, ciphertext)
}
if err != nil {
return ErrDecrypt
}
return fmt.Sprintf("%s", plaintext)
}
func Sign(plaintext string, hashName string, isUsePSS bool, saltLength int) string {
if priv == nil {
return ErrNoKey
}
msg, rng, hash := []byte(plaintext), rand.Reader, getCryptoHash(hashName)
var signature []byte
var err error
//fmt.Println("Sign: hashName", hashName,hash.String(), "isUsePSS", isUsePSS, "saltLength", saltLength)
hashFunc := hash.New()
hashFunc.Write(msg)
digest := hashFunc.Sum(nil)
if isUsePSS {
if saltLength < -1 {
saltLength = 0
}
signature, err = simplersa.SignPSS(rng, priv, hash, digest[:], &simplersa.PSSOptions{SaltLength: saltLength, Hash: hash})
} else {
signature, err = simplersa.SignPKCS1v15(rng, priv, hash, digest[:])
}
if err != nil {
return ErrSign
}
return fmt.Sprintf("%x", signature)
}
func Verify(plaintext string, signature string, hashName string, isUsePSS bool, saltLength int) string {
if priv == nil {
return ErrNoKey
}
var err error
msg, hash := []byte(plaintext), getCryptoHash(hashName)
sig, err := hex.DecodeString(signature)
if err != nil {
return VerifyFalse
}
//fmt.Println("Verify: hashName", hashName, hash.String(), "isUsePSS", isUsePSS, "saltLength", saltLength)
hashFunc := hash.New()
hashFunc.Write(msg)
digest := hashFunc.Sum(nil)
if isUsePSS {
if saltLength < -1 {
saltLength = 0
}
err = simplersa.VerifyPSS(&priv.PublicKey, hash, digest[:], sig, &simplersa.PSSOptions{SaltLength: saltLength, Hash: hash})
} else {
err = simplersa.VerifyPKCS1v15(&priv.PublicKey, hash, digest[:], sig)
}
if err != nil {
return VerifyFalse
}
return VerifyTrue
}
func ChangeParallel(state bool) {
log.Println("Parallel Mode:", state)
simplersa.ParaCalc = state
}
func main() {
log.Printf("OS: %s, Arch: %s\n", runtime.GOOS, runtime.GOARCH)
args := []string{}
if runtime.GOOS == "linux" {
args = append(args, "--class=Lorca")
}
ui, err := lorca.New("", "", 1200, 800, args...)
if err != nil {
log.Fatal(err)
}
defer ui.Close()
// A simple way to know when UI is ready (uses body.onload event in JS)
ui.Bind("start", func() {
log.Println("UI is ready")
})
// Create and bind Go object to the UI
ui.Bind("getN", GetN)
ui.Bind("getD", GetD)
ui.Bind("getE", GetE)
ui.Bind("getPrimes", GetPrimes)
ui.Bind("generateRSAKey", GenerateRSAKey)
ui.Bind("resetRSAKey", ResetRSAKey)
ui.Bind("encrypt", Encrypt)
ui.Bind("decrypt", Decrypt)
ui.Bind("sign", Sign)
ui.Bind("verify", Verify)
ui.Bind("changeParallel", ChangeParallel)
// Load HTML.
// You may also use `data:text/html,<base64>` approach to load initial HTML,
// e.g: ui.Load("data:text/html," + url.PathEscape(html))
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
go http.Serve(ln, http.FileServer(http.FS(fs)))
ui.Load(fmt.Sprintf("http://%s/www", ln.Addr()))
// Wait until the interrupt signal arrives or browser window is closed
sigc := make(chan os.Signal)
signal.Notify(sigc, os.Interrupt)
select {
case <-sigc:
case <-ui.Done():
}
log.Println("exiting...")
}