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