-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
455 lines (390 loc) · 13.8 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net/mail"
"os"
"strconv"
"strings"
"time"
"gocryptopgp/pkg/gpgutils"
)
const (
// prefix = "keys"
pubkey = "keys/testando-com-pass_public.asc"
privkey = "keys/testando-com-pass_SECRET.asc"
// name = "teste da silva"
// comment = "comentario do teste"
// email = "teste@teste"
// keySize = 2048
// passphrase = "teste"
)
func main() {
// err := gpgutils.EncryptMessageArmored(pubkey, "file-tests/teste.txt")
// if err != nil {
// fmt.Println(err)
// }
// err = gpgutils.DecryptMessageArmored(privkey, "file-tests/teste.txt.gpg", "teste123")
// if err != nil {
// fmt.Println(err)
// }
// hashutils.OpenAndCompareHashFiles("file-tests/teste.txt.gpg", "file-tests/teste.txt")
// os.Exit(1)
// gpgutils.KeyPairWriter(prefix, name, comment, email, passphrase, nil, keySize)
// gpgutils.EncryptSignMessageArmored(pubkey, privkey, passphrase, filePath)
// err := gpgutils.DecryptVerifyMessageArmored(pubkey, privkey, passphrase, encFilePath)
var (
filePath string
name string
comment string
email string
keySize int
passphrase string
newPassphrase string
expirationInput string
expirationTime time.Time
)
var privateKey, publicKey, keyOut, fileIn, fileOut string
// flags da aplicação
flag.StringVar(&privateKey, "privateKey", "", "[Directory name of private key]")
flag.StringVar(&publicKey, "publicKey", "", "[Directory name to public key]")
flag.Parse()
if flag.NArg() == 0 || (flag.Arg(0) != "encrypt" && flag.Arg(0) != "decrypt" && flag.Arg(0) != "keygen") {
errorMessage := "Error: Subcommand " + flag.Arg(0) + " is not available"
fmt.Println(errorMessage)
usage()
return
}
switch {
case flag.Arg(0) == "encrypt":
if publicKey == "" {
fmt.Println("Error: -publicKey is required")
usage()
return
}
fs := flag.NewFlagSet("encrypt [flags]", flag.ExitOnError)
fs.StringVar(&fileIn, "fileIn", "", "[Input file to encrypt]")
fs.StringVar(&fileOut, "fileOut", "", "[Output encrypted file]")
sygn := fs.Bool("sign", false, "[Declare passphrase to sygn encrypted file]")
fs.Parse(flag.Args()[1:])
if *sygn {
if privateKey == "" {
fmt.Println("Error: -privateKey is required")
usage()
return
}
for passphrase == "" {
fmt.Print("Passphrase para assinatura: ")
fmt.Scanln(&passphrase)
}
err := gpgutils.EncryptSignMessageArmored(publicKey, privateKey, passphrase, fileIn, fileOut)
if err != nil {
log.Fatal(err.Error())
}
} else {
err := gpgutils.EncryptMessageArmored(publicKey, fileIn, fileOut)
if err != nil {
log.Fatal(err.Error())
}
}
case flag.Arg(0) == "decrypt":
if privateKey == "" {
fmt.Println("Error: -privateKey is required")
usage()
return
}
fs := flag.NewFlagSet("decrypt [flags]", flag.ExitOnError)
fs.StringVar(&fileIn, "fileIn", "", "[Input file to decrypt]")
fs.StringVar(&fileOut, "fileOut", "", "[Output decrypted file]")
verify := fs.Bool("verify", false, "[Declare passphrase to verify encrypted file]")
fs.Parse(flag.Args()[1:])
if *verify {
if publicKey == "" {
fmt.Println("Error: -publicKey is required")
usage()
return
}
for passphrase == "" {
fmt.Print("Passphrase: ")
fmt.Scanln(&passphrase)
}
err := gpgutils.DecryptVerifyMessageArmored(publicKey, privateKey, passphrase, fileIn, fileOut)
if err != nil {
log.Fatal(err.Error())
}
} else {
fmt.Print("Passphrase (''): ")
fmt.Scanln(&passphrase)
err := gpgutils.DecryptMessageArmored(privateKey, fileIn, passphrase, fileOut)
if err != nil {
log.Fatal(err.Error())
}
}
case flag.Arg(0) == "keygen":
fs := flag.NewFlagSet("keygen [Generates a new public/private key pair]", flag.ExitOnError)
fs.StringVar(&keyOut, "d", "./keys", "[Output directory of key files]")
pass := fs.Bool("passphrase", false, "[Define passphrase]")
expTime := fs.Bool("expiration", false, "[Define key expiration time]")
newPass := fs.Bool("newPassphrase", false, "[Define passphrase]")
verify := fs.Bool("verify", false, "[Define passphrase]")
fs.Parse(flag.Args()[1:])
if *verify {
err := gpgutils.VerifyKeyPair(publicKey, privateKey)
if err != nil {
log.Fatal(err)
}
os.Exit(1)
}
if *newPass {
if privateKey == "" {
fmt.Println("Error: -privateKey is required")
usage()
return
}
fmt.Print("Old Passphrase: ")
fmt.Scanln(&passphrase)
fmt.Print("New Passphrase: ")
fmt.Scanln(&newPassphrase)
err := gpgutils.UpdatePrivateKeyPassphrase(privateKey, passphrase, newPassphrase)
if err != nil {
log.Fatal(err)
}
os.Exit(1)
}
fmt.Println("gpg (Test GPG) 1.0.0; Copyright (C) 2023")
fmt.Println("Test GPG needs to construct a user ID to identify your key.")
fmt.Println("")
// Solicitar o caminho do arquivo para salvar a chave
fmt.Print("Diretório e Prefixo das chaves ('./default-key'): ")
fmt.Scanln(&filePath)
if filePath == "" {
filePath = "./default-key"
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("Nome Completo: ")
name, _ = reader.ReadString('\n')
fmt.Print("Comentário: ")
comment, _ = reader.ReadString('\n')
for {
fmt.Print("Endereço de Email: ")
fmt.Scanln(&email)
if ok := validMailAddress(email); !ok {
fmt.Println("Email válido, digite novamente")
continue
} else {
break
}
}
fmt.Printf("Você selecionou este identificador de utilizador:\n \"%s (%s) <%s>\"\n", strings.TrimSpace(name), strings.TrimSpace(comment), strings.TrimSpace(email))
loop:
for {
fmt.Print("Mudar (N)ome, (C)omentário, (E)mail ou (O)k/(S)air? ")
option, _ := reader.ReadString('\n')
option = strings.TrimSpace(strings.ToLower(option))
switch option {
case "n":
fmt.Print("Nome Completo: ")
name, _ = reader.ReadString('\n')
case "c":
fmt.Print("Comentário: ")
comment, _ = reader.ReadString('\n')
case "e":
for {
fmt.Print("Endereço de Email: ")
fmt.Scanln(&email)
if ok := validMailAddress(email); !ok {
fmt.Println("Email válido, digite novamente")
continue
} else {
break
}
}
case "o":
break loop
case "s":
log.Fatal("gpg: Geração de chave cancelada. ")
default:
fmt.Println("Opção inválida. Tente novamente.")
}
fmt.Printf("Você selecionou este identificador de utilizador:\n \"%s (%s) <%s>\"\n", strings.TrimSpace(name), strings.TrimSpace(comment), strings.TrimSpace(email))
}
name = strings.TrimSpace(name)
comment = strings.TrimSpace(comment)
email = strings.TrimSpace(email)
for keySize != 1024 && keySize != 2048 && keySize != 4096 {
fmt.Println("Numero de bits para tamanho das chaves:\n[1024 bits] [2048 bits] [4096 bits]")
fmt.Print("Digite o tamanho (2048): ")
fmt.Scanln(&keySize)
if keySize == 0 {
keySize = 2048
}
}
if *pass {
fmt.Print("Passphrase (''): ")
fmt.Scanln(&passphrase)
}
for {
if *expTime {
fmt.Println(`
Por favor especifique por quanto tempo a chave deve ser válida.
0 = chave não expira
<n>d = chave expira em n dias
<n>w = chave expira em n semanas
<n>m = chave expira em n meses
<n>y = chave expira em n anos`)
fmt.Print("A chave é valida por? (0) ")
fmt.Scanln(&expirationInput)
if expirationInput != "" {
dataExp, err := parseDuracaoChave(expirationInput)
if err != nil {
fmt.Println("Entrada inválida:", err)
continue
}
fmt.Printf("A Chave expira em %s Hora oficial do Brasil\n", dataExp.Format("02/01/2006 15:04:05"))
expirationTime = dataExp
break
} else {
expirationInput = "0"
fmt.Println("A Chave não expira.")
break
}
}
}
err := gpgutils.KeyPairWriter(filePath, name, comment, email, passphrase, &expirationTime, keySize)
if err != nil {
log.Fatal(err)
}
default:
usage()
return
}
}
// Analisar a entrada de duração da chave e retornar os valores de duração e unidade
func parseDuracaoChave(duracaoChave string) (time.Time, error) {
if duracaoChave == "" {
return time.Now(), nil
}
unidade := string(duracaoChave[len(duracaoChave)-1])
duracaoStr := duracaoChave[:len(duracaoChave)-1]
duracao, err := strconv.Atoi(duracaoStr)
if err != nil {
return time.Time{}, fmt.Errorf("ocorreu um erro ao obter a duração")
}
switch unidade {
case "d":
return time.Now().AddDate(0, 0, duracao), nil
case "w":
return time.Now().AddDate(0, 0, duracao*7), nil
case "m":
return time.Now().AddDate(0, duracao, 0), nil
case "y":
return time.Now().AddDate(duracao, 0, 0), nil
default:
return time.Now(), fmt.Errorf("ocorreu um erro ao obter a unidade")
}
}
func validMailAddress(address string) bool {
if address == "" {
return true
}
_, err := mail.ParseAddress(address)
if err != nil {
return false
}
return true
}
func usage() {
fmt.Println()
fmt.Println("Available flags before subcommands encrypt or decrypt:")
fmt.Println(" -privateKey string: specify privateKey")
fmt.Println(" -publicKey string: specify publicKey")
fmt.Println()
fmt.Println("Available flags after subcommand encrypt:")
fmt.Println(" -file string: specify file to encrypting process")
fmt.Println()
fmt.Println("Available flags after subcommand encrypt:")
fmt.Println(" -file string: specify file to decrypting process")
fmt.Println()
fmt.Println("Available flags after subcommand keygen:")
fmt.Println(" -passphrase string: Define if passphrase is on")
fmt.Println(" -expiration string: Define if keys expiration time is on")
fmt.Println()
}
// fmt.Println(`Supported algorithms:
// Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
// Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
// CAMELLIA128, CAMELLIA192, CAMELLIA256
// Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
// Compression: Uncompressed, ZIP, ZLIB, BZIP2
// Syntax: gpg [options] [files]
// Sign, check, encrypt or decrypt
// Default operation depends on the input data
// Commands:
// -s, --sign make a signature
// --clear-sign make a clear text signature
// -b, --detach-sign make a detached signature
// -e, --encrypt encrypt data
// -c, --symmetric encryption only with symmetric cipher
// -d, --decrypt decrypt data (default)
// --verify verify a signature
// -k, --list-keys list keys
// --list-signatures list keys and signatures
// --check-signatures list and check key signatures
// --fingerprint list keys and fingerprints
// -K, --list-secret-keys list secret keys
// --generate-key generate a new key pair
// --quick-generate-key quickly generate a new key pair
// --quick-add-uid quickly add a new user-id
// --quick-revoke-uid quickly revoke a user-id
// --quick-set-expire quickly set a new expiration date
// --full-generate-key full featured key pair generation
// --generate-revocation generate a revocation certificate
// --delete-keys remove keys from the public keyring
// --delete-secret-keys remove keys from the secret keyring
// --quick-sign-key quickly sign a key
// --quick-lsign-key quickly sign a key locally
// --quick-revoke-sig quickly revoke a key signature
// --sign-key sign a key
// --lsign-key sign a key locally
// --edit-key sign or edit a key
// --change-passphrase change a passphrase
// --export export keys
// --send-keys export keys to a keyserver
// --receive-keys import keys from a keyserver
// --search-keys search for keys on a keyserver
// --refresh-keys update all keys from a keyserver
// --import import/merge keys
// --card-status print the card status
// --edit-card change data on a card
// --change-pin change a card's PIN
// --update-trustdb update the trust database
// --print-md print message digests
// --server run in server mode
// --tofu-policy VALUE set the TOFU policy for a key
// Options controlling the configuration:
// --default-key NAME use NAME as default secret key
// --encrypt-to NAME encrypt to user ID NAME as well
// --group SPEC set up email aliases
// --openpgp use strict OpenPGP behavior
// -n, --dry-run do not make any changes
// -i, --interactive prompt before overwriting
// Options controlling the output:
// -o, --output FILE write output to FILE
// -z N set compress level to N (0 disables)
// Options controlling key import and export:
// --auto-key-locate MECHANISMS use MECHANISMS to locate keys by mail address
// --auto-key-import import missing key from a signature
// --include-key-block include the public key in signatures
// --disable-dirmngr disable all access to the dirmngr
// Options to specify keys:
// -r, --recipient USER-ID encrypt for USER-ID
// -u, --local-user USER-ID use USER-ID to sign or decrypt
// (See the man page for a complete listing of all commands and options)
// Examples:
// -se -r Bob [file] sign and encrypt for user Bob
// --clear-sign [file] make a clear text signature
// --detach-sign [file] make a detached signature
// --list-keys [names] show keys
// --fingerprint [names] show fingerprints`)