-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.go
146 lines (136 loc) · 4.24 KB
/
transform.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
package transform
import (
"core/compression"
"core/cryptogost"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/klauspost/compress/zstd"
)
func RecoveryFile(path string, filename string, CompressionStat bool, ExpansionFormat string, CryptoName string, CryptoPassword string) {
inputFile, err := os.Open(path)
if err != nil {
log.Fatal("Ошибка:", err)
}
path_encrypto := "./tmp/server/zt/" + strings.Split(filename, ".")[0] + ".zst"
path_encoded := "./org/" + strings.Split(filename, ".")[0] + ExpansionFormat
encryptoFile, err := os.Create(path_encrypto)
if err != nil {
log.Fatal("Ошибка:", err)
}
encryptoFile.Close()
encodedFile, err := os.Create(path_encoded)
if err != nil {
log.Fatal("Ошибка:", err)
}
defer encodedFile.Close()
switch CryptoName {
case "Магма":
cryptogost.DecryptK(path, CryptoPassword, path_encrypto)
case "Кузнечик":
cryptogost.DecryptK(path, CryptoPassword, path_encrypto)
default:
encryptoFile, err := os.OpenFile(path_encrypto, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
fmt.Println("Ошибка при копирование не шифрованного файла:", err)
return
}
_, err = io.Copy(encryptoFile, inputFile)
if err != nil {
fmt.Println("Ошибка при передаче файлаfg:", err)
return
}
encryptoFile.Close()
}
inputFile.Close()
encryptoFile, err = os.OpenFile(path_encrypto, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
fmt.Println("Ошибка при открытие перед кодирование:", err)
return
}
if CompressionStat {
if err != nil {
log.Fatal("Ошибка:", err)
}
err = compression.DecompressOne(encryptoFile, encodedFile)
if err != nil {
log.Fatal("Ошибка:", err)
}
} else {
_, err = io.Copy(encodedFile, encryptoFile)
if err != nil {
fmt.Println("Ошибка при передаче файла:", err)
return
}
}
}
func CodeFile(path string, filename string, CompressionLevel int, CryptoName string, CryptoPassword string) (string, string) {
inputFile, err := os.Open(path)
if err != nil {
log.Fatal("Ошибка:", err)
}
defer inputFile.Close()
path_encoded := "./tmp/client/zt/" + strings.Split(filename, ".")[0] + ".zst"
path_encrypto := "./tmp/client/crypto/" + strings.Split(filename, ".")[0] + ".gost"
encryptoFile, err := os.Create(path_encrypto)
if err != nil {
log.Fatal("Ошибка:", err)
}
encryptoFile.Close()
encodedFile, err := os.Create(path_encoded)
if err != nil {
log.Fatal("Ошибка:", err)
}
defer encodedFile.Close()
if CompressionLevel == -1 {
_, err = io.Copy(encodedFile, inputFile)
if err != nil {
fmt.Println("Ошибка при передаче файла:", err)
return "", ""
}
} else {
err = compression.CompressOne(inputFile, encodedFile, zstd.EncoderLevelFromZstd(CompressionLevel))
if err != nil {
log.Fatal("Ошибка:", err)
}
}
inputFile.Close()
encodedFile.Close()
path_itog := path_encrypto
name_itog := strings.Split(filename, ".")[0] + ".gost"
switch CryptoName {
case "Магма":
cryptogost.EncryptM(path_encoded, CryptoPassword, path_encrypto)
case "Кузнечик":
cryptogost.EncryptK(path_encoded, CryptoPassword, path_encrypto)
default:
path_itog = "./tmp/client/crypto/" + strings.Split(filename, ".")[0] + ".zst"
name_itog = strings.Split(filename, ".")[0] + ".zst"
CopItog(path_itog, path_encoded)
}
return path_itog, name_itog
}
func CopItog(path_itog, path_encoded string) {
enFile, err := os.Create(path_itog)
if err != nil {
fmt.Println("Ошибка при копирование не шифрованного файла:", err)
return
}
defer enFile.Close()
encFile, err := os.Open(path_encoded)
if err != nil {
fmt.Println("Ошибка при копирование не шифрованного файла:", err)
return
}
defer encFile.Close()
nkl, err := io.Copy(enFile, encFile)
if err != nil {
fmt.Println("Ошибка при передаче не шифрованного файла:", err)
return
}
fmt.Print(nkl)
encFile.Close()
enFile.Close()
}