-
Notifications
You must be signed in to change notification settings - Fork 0
/
keygen.go
85 lines (73 loc) · 1.74 KB
/
keygen.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
/**
* This file is a part of the poni project and is licensed under the MIT license.
* See LICENSE.md for details.
*
* keygen.go
* Contains the keygen command.
*
* @created 2023-08-23
*/
package main
import (
"flag"
"fmt"
)
// keygenHandler
// Handles the keygen command.
// Returns nothing.
func keygenHandler(args []string) {
var password string
var salt string
var help bool
command := flag.NewFlagSet("keygen", flag.ExitOnError)
command.StringVar(&password, "p", "", "The password to use for key generation")
command.StringVar(&salt, "s", "", "The salt to use for key generation")
command.BoolVar(&help, "h", false, "Show the keygen help message")
err := command.Parse(args)
if err != nil {
return
}
if help {
keygenUsage()
return
}
if password == "" {
keygenUsage()
return
}
if salt == "" {
if len(password) < 10 {
fmt.Println("For your security passwords must be at least 10 characters long")
return
}
key, keySalt, err := deriveKey(password)
if err != nil {
fmt.Println("Failed to generate key")
return
}
fmt.Println("Key:", armor(key))
fmt.Println("Salt:", armor(keySalt))
} else {
decodedSalt, err := dearmor(salt)
if err != nil {
fmt.Println("Failed to decode salt")
return
}
key, err := deriveKeyWithSalt(password, decodedSalt)
if err != nil {
fmt.Println("Failed to generate key")
return
}
fmt.Println("Key:", armor(key))
}
}
// keygenUsage
// Prints the keygen help message.
// Returns nothing.
func keygenUsage() {
fmt.Println("Usage: poni keygen -p <password> [-s <salt>]")
fmt.Println()
fmt.Println(` -p <password> A password or passphrase to use for key generation
[-s <salt>] A salt to use for key generation`)
fmt.Println()
}