-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
200 lines (183 loc) · 4.3 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
package main
import (
"encoding/hex"
"fmt"
"github.com/devguardio/identity/go"
"github.com/spf13/cobra"
"log"
"os"
)
var usersa = false
var domain = ""
func main() {
log.SetFlags(log.Lshortfile)
var rootCmd = cobra.Command{
Use: "identitykit",
Short: "\ncryptographic identity toolkit",
Version: "1",
}
rootCmd.AddCommand(tlsCmd())
compat := &cobra.Command{
Use: "convert <id>",
Short: "conversion commands",
Aliases: []string{"cv", "conv"},
}
compat.AddCommand(&cobra.Command{
Use: "hex2secret <hex>",
Short: "convert a hex encoded secret seed to an ik secret",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
decoded, err := hex.DecodeString(args[0])
if err != nil {
panic(err)
}
if len(decoded) < 32 {
panic("must be at least 32 bytes long")
}
var sk identity.Secret
copy(sk[:], decoded[:32])
fmt.Println(sk.ToString())
},
})
compat.AddCommand(&cobra.Command{
Use: "hex2identity <hex>",
Short: "convert a hex encoded public key seed to an ik identity",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
decoded, err := hex.DecodeString(args[0])
if err != nil {
panic(err)
}
if len(decoded) < 32 {
panic("must be at least 32 bytes long")
}
var sk identity.Identity
copy(sk[:], decoded[:32])
fmt.Println(sk.String())
},
})
compat.AddCommand(&cobra.Command{
Use: "id32to58 <id>",
Short: "convert a b32 identity to a legacy b58",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
id, err := identity.IdentityFromString(args[0])
if err != nil {
panic(err)
}
fmt.Println(id.String58())
},
})
compat.AddCommand(&cobra.Command{
Use: "secret2public <Secret>",
Short: "convert a secret to an identity",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
s, err := identity.SecretFromString(args[0])
if err != nil {
panic(err)
}
id, err := s.Identity()
if err != nil {
panic(err)
}
fmt.Println(id.String())
},
})
compat.AddCommand(&cobra.Command{
Use: "secret2address <Secret>",
Short: "convert a secret to an address",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
s, err := identity.SecretFromString(args[0])
if err != nil {
panic(err)
}
xs := s.XSecret()
if err != nil {
panic(err)
}
fmt.Println(xs.XPublic().String())
},
})
rootCmd.AddCommand(compat)
rootCmd.PersistentFlags().BoolVarP(&usersa, "rsa", "r", false, "use rsa instead of ed25519")
rootCmd.PersistentFlags().StringVarP(&domain, "domain", "u", "", "use vault in separate user specific domain")
rootCmd.AddCommand(&cobra.Command{
Use: "identity ",
Aliases: []string{"id"},
Short: "print my identity",
Run: func(cmd *cobra.Command, args []string) {
var vault = identity.Vault()
if domain != "" {
vault = vault.Domain(domain)
}
if usersa {
id, err := vault.RSAPublic()
if err != nil {
panic(err)
}
fmt.Println(id)
} else {
id, err := vault.Identity()
if err != nil {
panic(err)
}
fmt.Println(id)
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "address",
Aliases: []string{"xp", "addr"},
Short: "print my DH address",
Run: func(cmd *cobra.Command, args []string) {
var vault = identity.Vault()
if domain != "" {
vault = vault.Domain(domain)
}
if usersa {
panic("rsa doesn't work with diffie-hellman")
} else {
id, err := vault.XPublic()
if err != nil {
panic(err)
}
fmt.Println(id)
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "init",
Short: "initialize empty vault",
Run: func(cmd *cobra.Command, args []string) {
var vault = identity.Vault()
if domain != "" {
vault = vault.Domain(domain)
}
err := vault.Init(true)
if err != nil {
panic(err)
}
id, err := vault.Identity()
if err != nil {
panic(err)
}
fmt.Println(id)
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "mksecret",
Short: "generate a secret",
Run: func(cmd *cobra.Command, args []string) {
sk, err := identity.CreateSecret()
if err != nil {
panic(err)
}
fmt.Println(sk.ToString())
},
})
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}