Add encrypt and decrypt function
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
go-demo/app-1
|
go-demo/app-1
|
||||||
go-demo-4/data.json
|
go-demo-4/data.json
|
||||||
go-demo-4/.env
|
go-demo-4/.env*
|
||||||
|
go-demo-4/*.vault
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
package encrypter
|
package encrypter
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
type Encrypter struct {
|
type Encrypter struct {
|
||||||
Key string
|
Key string
|
||||||
@@ -16,10 +22,37 @@ func NewEncrypter() *Encrypter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (enc *Encrypter) Encrypt(plainStr string) string {
|
func (enc *Encrypter) Encrypt(plainStr []byte) []byte {
|
||||||
return ""
|
block, err := aes.NewCipher([]byte(enc.Key))
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
aesGCM, err := cipher.NewGCM(block)
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
nonce := make([]byte, aesGCM.NonceSize())
|
||||||
|
_, err = io.ReadFull(rand.Reader, nonce)
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
return aesGCM.Seal(nonce, nonce, plainStr, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (enc *Encrypter) Decrypt(encryptedStr string) string {
|
func (enc *Encrypter) Decrypt(encryptedStr []byte) []byte {
|
||||||
return ""
|
block, err := aes.NewCipher([]byte(enc.Key))
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
aesGCM, err := cipher.NewGCM(block)
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
nonceSize := aesGCM.NonceSize()
|
||||||
|
nonce, cipherText := encryptedStr[:nonceSize], encryptedStr[nonceSize:]
|
||||||
|
plainText, err := aesGCM.Open(nil, nonce, cipherText, nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
return plainText
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
output.PrintError("Не удалось найти файл .env")
|
output.PrintError("Не удалось найти файл .env")
|
||||||
}
|
}
|
||||||
vault := account.NewVault(files.NewJsonDb("data.json"), *encrypter.NewEncrypter())
|
vault := account.NewVault(files.NewJsonDb("data.vault"), *encrypter.NewEncrypter())
|
||||||
counter := menuCounter()
|
counter := menuCounter()
|
||||||
Menu:
|
Menu:
|
||||||
for {
|
for {
|
||||||
|
|||||||
Reference in New Issue
Block a user