Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (107 loc) · 2.59 KB
/
Copy pathmain.go
File metadata and controls
134 lines (107 loc) · 2.59 KB
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
package main
import (
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/btcsuite/btcd/btcec/v2"
bitstream "github.com/pyropy/bitstream-go/pkg"
)
var (
action string
inPath string
outPath string
preimage string
paymentHash string
chunkSize int64
pk *btcec.PrivateKey
)
func init() {
flag.StringVar(&action, "action", "encrypt", "encrypt or decrypt")
flag.StringVar(&inPath, "in", "", "File to encrypt/decrypt")
flag.StringVar(&outPath, "out", "", "Path to output encrypted/decrypted file")
flag.StringVar(&preimage, "preimage", "", "Preimage to use for encryption/decryption")
flag.StringVar(&paymentHash, "hash", "", "Payment hash")
flag.Int64Var(&chunkSize, "size", 32, "Size of chunks to encrypt/decrypt")
pkBytes, err := hex.DecodeString("80faa7d1c150a903a4028bf87ca8800aff507b24df90e8434bfa1f34d639c053")
if err != nil {
panic(err)
}
pk, _ = btcec.PrivKeyFromBytes(pkBytes)
}
// TODO: Implement encrypt and decrypt cli commands
func main() {
flag.Parse()
var err error
switch action {
case "encrypt":
err = encrypt(inPath, outPath, preimage, chunkSize)
case "decrypt":
err = decrypt(inPath, outPath, preimage, chunkSize)
}
if err != nil {
panic(err)
}
}
func encrypt(inPath, outPath, preimage string, chunkSize int64) error {
f, err := os.Open(inPath)
if err != nil {
return err
}
defer f.Close()
out, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
preimageBytes, err := hex.DecodeString(preimage)
if err != nil {
panic(err)
}
paymentHashBytes, err := hex.DecodeString(paymentHash)
if err != nil {
return err
}
err = bitstream.Encrypt(pk, paymentHashBytes, preimageBytes, f, out, chunkSize)
if err != nil {
return err
}
return nil
}
func decrypt(inPath, outPath, preimage string, chunkSize int64) error {
f, err := os.Open(inPath)
if err != nil {
return err
}
defer f.Close()
preimageBytes, err := hex.DecodeString(preimage)
if err != nil {
return err
}
paymentHashBytes, err := hex.DecodeString(paymentHash)
if err != nil {
return err
}
decryptedFile, index, err := bitstream.Decrypt(paymentHashBytes, preimageBytes, f, chunkSize)
if err != nil {
if index != -1 {
proof, err := bitstream.GenerateProof(f, chunkSize, 2*index)
if err != nil {
return err
}
root := fmt.Sprintf("%x", proof.MerkleProof.Root)
log.Println("Generated proof", "root", root)
}
return err
}
decrypted, err := io.ReadAll(decryptedFile)
if err != nil {
return err
}
err = os.WriteFile(outPath, decrypted, 0644)
if err != nil {
return err
}
return nil
}