Server and Client daemons functional
This commit is contained in:
78
crypto.go
78
crypto.go
@@ -12,55 +12,6 @@ import (
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
)
|
||||
|
||||
//var cert *x509.Certificate
|
||||
//var key *rsa.PrivateKey
|
||||
//
|
||||
//func encryptBytes(data []byte) []byte {
|
||||
// if cert == nil || key == nil {
|
||||
// loadCerts()
|
||||
// }
|
||||
//
|
||||
// encrypted, err := rsa.EncryptPKCS1v15(rand.Reader, cert.PublicKey.(*rsa.PublicKey), data)
|
||||
// if err != nil {
|
||||
// fmt.Println("Error encrypting data,", err)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
// return encrypted
|
||||
//}
|
||||
//
|
||||
//func decryptBytes(data []byte) []byte {
|
||||
// if cert == nil || key == nil {
|
||||
// loadCerts()
|
||||
// }
|
||||
//
|
||||
// decrypted, err := rsa.DecryptPKCS1v15(rand.Reader, key, data)
|
||||
// if err != nil {
|
||||
// fmt.Println("Error decrypting data,", err)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
// return decrypted
|
||||
//}
|
||||
//
|
||||
//func loadCerts() {
|
||||
// var err error
|
||||
// certBytes, err := os.ReadFile(config.GetAsString("Crypto.cert_path"))
|
||||
// keyBytes, err := os.ReadFile(config.GetAsString("Crypto.key_path"))
|
||||
// if err != nil {
|
||||
// fmt.Println("Error reading cert or key,", err)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
//
|
||||
// cert, err = x509.ParseCertificate(certBytes)
|
||||
// if err != nil {
|
||||
// fmt.Println("Error parsing certificate,", err)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
// key, err = x509.ParsePKCS1PrivateKey(keyBytes)
|
||||
// if err != nil {
|
||||
// fmt.Println("Error parsing private key,", err)
|
||||
// }
|
||||
//}
|
||||
|
||||
// GenerateKey returns a base64-encoded 32-byte random key suitable to use as the
|
||||
// symmetric passphrase for age scrypt mode. Store this securely (never in Git).
|
||||
func GenerateKey() (string, error) {
|
||||
@@ -150,3 +101,32 @@ func DecryptFileXChaCha(keyB64, inPath, outPath string, aad []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DecryptFileFromBytes(keyB64 string, inBytes []byte, outPath string, aad []byte) error {
|
||||
key, err := decodeKey(keyB64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
aead, err := chacha20poly1305.NewX(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("new aead: %w", err)
|
||||
}
|
||||
|
||||
if len(inBytes) < chacha20poly1305.NonceSizeX {
|
||||
return errors.New("ciphertext too short")
|
||||
}
|
||||
|
||||
nonce := inBytes[:chacha20poly1305.NonceSizeX]
|
||||
ciphertext := inBytes[chacha20poly1305.NonceSizeX:]
|
||||
|
||||
plaintext, err := aead.Open(nil, nonce, ciphertext, aad)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt/auth failed: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(outPath, plaintext, 0640); err != nil {
|
||||
return fmt.Errorf("write output: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user