mirror of
https://github.com/rwinkhart/rcw.git
synced 2026-08-27 20:36:30 -04:00
Give error handling control to importing applications
This commit is contained in:
+14
-2
@@ -28,6 +28,10 @@ import (
|
||||
// Store:
|
||||
// Hash of passphrase (prevent user from losing data by accidentally providing incorrect passphrase during encryption)
|
||||
// Order of algorithms (determined randomly at keyfile generation)
|
||||
// Security:
|
||||
// Play with nonce sizes and Argon2 parameters to find the best speed-security balance
|
||||
// Standalone cmd:
|
||||
// Usable as symmetric-only GPG replacement
|
||||
|
||||
func main() {
|
||||
switch len(os.Args) {
|
||||
@@ -37,8 +41,16 @@ func main() {
|
||||
case 3:
|
||||
// decrypt file
|
||||
encBytes, _ := os.ReadFile("encrypted-example.txt")
|
||||
decBytes := wrappers.DecryptCha(encBytes, []byte(os.Args[2]))
|
||||
decBytes = wrappers.DecryptAES(decBytes, []byte(os.Args[2]))
|
||||
decBytes, err := wrappers.DecryptCha(encBytes, []byte(os.Args[2]))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
decBytes, err = wrappers.DecryptAES(decBytes, []byte(os.Args[2]))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(string(decBytes))
|
||||
case 4:
|
||||
// encrypt data (from cli args)
|
||||
|
||||
+7
-9
@@ -4,7 +4,7 @@ import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
@@ -12,7 +12,7 @@ const (
|
||||
nonceSizeAES = 12 // GCM standard nonce size is 12 bytes
|
||||
)
|
||||
|
||||
// EncryptAES encrypts data using AES-256-GCM
|
||||
// EncryptAES encrypts data using AES-256-GCM.
|
||||
func EncryptAES(data []byte, passphrase []byte) []byte {
|
||||
// generate a random salt
|
||||
salt := make([]byte, saltSize)
|
||||
@@ -43,11 +43,10 @@ func EncryptAES(data []byte, passphrase []byte) []byte {
|
||||
return result
|
||||
}
|
||||
|
||||
// DecryptAES decrypts data using AES-256-GCM
|
||||
func DecryptAES(encryptedData []byte, passphrase []byte) []byte {
|
||||
// DecryptAES decrypts data using AES256-GCM.
|
||||
func DecryptAES(encryptedData []byte, passphrase []byte) ([]byte, error) {
|
||||
if len(encryptedData) < saltSize+nonceSizeAES {
|
||||
fmt.Println("Encrypted data is too short")
|
||||
return nil
|
||||
return nil, errors.New("AES256-GCM: Encrypted data is too short")
|
||||
}
|
||||
|
||||
// extract salt, nonce, and ciphertext
|
||||
@@ -67,9 +66,8 @@ func DecryptAES(encryptedData []byte, passphrase []byte) []byte {
|
||||
// decrypt the data
|
||||
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("Decryption failed (possibly wrong passphrase): %s", err.Error())
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return plaintext
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
+7
-9
@@ -2,7 +2,7 @@ package wrappers
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
@@ -12,7 +12,7 @@ const (
|
||||
nonceSizeCha = chacha20poly1305.NonceSizeX
|
||||
)
|
||||
|
||||
// EncryptCha encrypts data using ChaCha20-Poly1305
|
||||
// EncryptCha encrypts data using ChaCha20-Poly1305.
|
||||
func EncryptCha(data []byte, passphrase []byte) []byte {
|
||||
// generate a random salt
|
||||
salt := make([]byte, saltSize)
|
||||
@@ -41,11 +41,10 @@ func EncryptCha(data []byte, passphrase []byte) []byte {
|
||||
return result
|
||||
}
|
||||
|
||||
// DecryptCha decrypts data using ChaCha20-Poly1305
|
||||
func DecryptCha(encryptedData []byte, passphrase []byte) []byte {
|
||||
// DecryptCha decrypts data using ChaCha20-Poly1305.
|
||||
func DecryptCha(encryptedData []byte, passphrase []byte) ([]byte, error) {
|
||||
if len(encryptedData) < saltSize+nonceSizeCha {
|
||||
fmt.Println("Encrypted data is too short")
|
||||
return nil
|
||||
return nil, errors.New("ChaCha20-Poly1305: Encrypted data is too short")
|
||||
}
|
||||
|
||||
// extract salt, nonce, and ciphertext
|
||||
@@ -62,9 +61,8 @@ func DecryptCha(encryptedData []byte, passphrase []byte) []byte {
|
||||
// decrypt the data
|
||||
plaintext, err := stream.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
fmt.Printf("Decryption failed (possibly wrong passphrase): %s", err.Error())
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return plaintext
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user