-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdebug.go
122 lines (100 loc) · 3.16 KB
/
debug.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
package sncli
import (
"encoding/base64"
"fmt"
"strings"
"github.com/gookit/color"
"github.com/jonhadfield/gosn-v2/crypto"
"github.com/jonhadfield/gosn-v2/items"
"github.com/jonhadfield/gosn-v2/session"
)
type DecryptStringInput struct {
Session session.Session
In string
UseStdOut bool
Key string
}
func DecryptString(input DecryptStringInput) (plaintext string, err error) {
key1 := input.Session.MasterKey
if input.Key != "" {
key1 = input.Key
}
// trim noise
if strings.HasPrefix(input.In, "enc_item_key") && len(input.In) > 13 {
input.In = strings.TrimSpace(input.In)[13:]
}
if strings.HasPrefix(input.In, "content") && len(input.In) > 8 {
input.In = strings.TrimSpace(input.In)[8:]
}
version, nonce, cipherText, authData := splitContent(input.In)
if version != "004" {
return plaintext, fmt.Errorf("only version 004 of encryption is supported")
}
bad, err := base64.StdEncoding.DecodeString(authData)
if err != nil {
err = fmt.Errorf("failed to base64 decode auth data: '%s' err: %+v", authData, err)
return
}
fmt.Printf("Decoded Auth Data: %+v\n", string(bad))
pb, err := crypto.DecryptCipherText(cipherText, key1, nonce, authData)
if err != nil {
return
}
return string(pb), nil
}
type OutputSessionInput struct {
Session session.Session
In string
UseStdOut bool
OutputMasterKey bool
}
func OutputSession(input OutputSessionInput) error {
fmt.Println(color.Bold.Sprintf("session"))
fmt.Printf("debug: %t\n\n", input.Session.Debug)
fmt.Println("key params")
fmt.Printf("- identifier: %s\n", input.Session.KeyParams.Identifier)
fmt.Printf("- nonce: %s\n", input.Session.KeyParams.PwNonce)
fmt.Printf("- created: %s\n", input.Session.KeyParams.Created)
fmt.Printf("- origination: %s\n", input.Session.KeyParams.Origination)
fmt.Printf("- version: %s\n", input.Session.KeyParams.Version)
fmt.Println()
if input.OutputMasterKey {
fmt.Printf("master key: %s\n", input.Session.MasterKey)
fmt.Println()
}
_, err := items.Sync(items.SyncInput{Session: &input.Session})
if err != nil {
return err
}
// output default items key
ik := input.Session.DefaultItemsKey
fmt.Println("default items key")
fmt.Printf("- uuid %s key %s created-at %d updated-at %d\n", ik.UUID, ik.ItemsKey, ik.CreatedAtTimestamp, ik.UpdatedAtTimestamp)
// output all items keys in session
fmt.Println("items keys")
for _, ik = range input.Session.ItemsKeys {
fmt.Printf("- uuid %s key %s created-at %d updated-at %d\n", ik.UUID, ik.ItemsKey, ik.CreatedAtTimestamp, ik.UpdatedAtTimestamp)
}
return nil
}
type CreateItemsKeyInput struct {
Debug bool
MasterKey string
}
// func CreateItemsKey(input CreateItemsKeyInput) error {
// ik := items.NewItemsKey()
// fmt.Printf("%+v\n", ik.ItemsKey)
//
// return nil
// }
func splitContent(in string) (version, nonce, cipherText, authenticatedData string) {
components := strings.Split(in, ":")
if len(components) < 3 {
panic(components)
}
version = components[0] // protocol version
nonce = components[1] // encryption nonce
cipherText = components[2] // ciphertext
authenticatedData = components[3] // authenticated data
return
}