-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (153 loc) · 4.16 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
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"flag"
"fmt"
"io"
"log"
"os"
)
func main() {
filename := flag.String("file", "", "Path to the file")
sign := flag.Bool("S", false, "If you want to sign the file")
verify := flag.Bool("V", false, "If you want to verify the signature")
publicPath := flag.String("key", "", "Name of the Public Key")
flag.Parse()
//fmt.Printf("Filename: '%s', Sign: %v, Verify: %v\n", *filename, *sign, *verify)
if *filename == "" {
fmt.Printf("\nYou must provide a file using the -file <file_localization>.\n")
fmt.Println("And use -S (to sign) or -V (to verify) and -key <pub_key_localization>")
os.Exit(1)
}
if *sign {
file, err := os.Open(*filename)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
os.Exit(1)
}
defer file.Close()
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
log.Fatal(err)
}
hash := hasher.Sum(nil)
hashStr := hex.EncodeToString(hash)
fmt.Printf("SHA-256 hash of %s: %s\n", *filename, hashStr)
privateKey, publicKey, err := generateKeyPair("output-kopia.txt")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Public Key: %x\n", publicKey)
signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hash)
if err != nil {
fmt.Printf("Error signing data: %v\n", err)
os.Exit(1)
}
fmt.Printf("Signature: %x\n", signature)
err = os.WriteFile("signature.bin", signature, 0644)
if err != nil {
fmt.Printf("Error saving signature: %v\n", err)
os.Exit(1)
}
fmt.Println("Signature saved to signature.bin")
}
if *verify {
file, err := os.Open(*filename)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
os.Exit(1)
}
defer file.Close()
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
log.Fatal(err)
}
hash := hasher.Sum(nil)
hashStr := hex.EncodeToString(hash)
fmt.Printf("SHA-256 hash of %s: %s\n", *filename, hashStr)
publicKey, err := loadPublicKey(*publicPath)
if err != nil {
log.Fatal(err)
}
signature, err := os.ReadFile("signature.bin")
if err != nil {
log.Fatal(err)
}
err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hash, signature)
if err != nil {
fmt.Printf("Signature verification failed: %v\n", err)
os.Exit(1)
}
fmt.Println("Signature verification succeeded")
}
}
func generateKeyPair(fileName string) (*rsa.PrivateKey, *rsa.PublicKey, error) {
numbers, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
privateKey, err := rsa.GenerateKey(numbers, 2048)
if err != nil {
return nil, nil, err
}
// Save the private key to a PEM file
privateKeyFile, err := os.Create("private_key.pem")
if err != nil {
return nil, nil, err
}
defer privateKeyFile.Close()
privateKeyPEM := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
return nil, nil, err
}
fmt.Println("Private key saved to private_key.pem")
// Marshal the public key to PKIX format
publicKeyBytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
if err != nil {
return nil, nil, err
}
// Save the public key to a PEM file
publicKeyFile, err := os.Create("public_key.pem")
if err != nil {
return nil, nil, err
}
defer publicKeyFile.Close()
publicKeyPEM := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
if err := pem.Encode(publicKeyFile, publicKeyPEM); err != nil {
return nil, nil, err
}
fmt.Println("Public key saved to public_key.pem")
return privateKey, &privateKey.PublicKey, nil
}
func loadPublicKey(filename string) (*rsa.PublicKey, error) {
pubKeyPEM, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
block, _ := pem.Decode(pubKeyPEM)
if block == nil || block.Type != "PUBLIC KEY" {
return nil, fmt.Errorf("failed to decode PEM block containing public key")
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
switch pub := pub.(type) {
case *rsa.PublicKey:
return pub, nil
default:
return nil, fmt.Errorf("unexpected type of public key")
}
}