mirror of
https://github.com/rwinkhart/rcw.git
synced 2026-08-27 20:36:30 -04:00
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package daemon
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/rwinkhart/rcw/wrappers"
|
|
)
|
|
|
|
var Timeout = 300 // seconds for RPC server timeout; configurable
|
|
|
|
var daemonHash []byte
|
|
var globalPassword []byte
|
|
|
|
// RCWService provides an RPC method.
|
|
type RCWService struct{}
|
|
|
|
// DecryptRequest is the RPC method that decrypts the incoming data using
|
|
// the global password and returns the decrypted data
|
|
func (h *RCWService) DecryptRequest(encBytes []byte, reply *[]byte) error {
|
|
var err error
|
|
*reply, err = wrappers.Decrypt(encBytes, globalPassword, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncryptRequest is the RPC method that encrypts the incoming data using
|
|
// the global password and returns the encrypted data
|
|
func (h *RCWService) EncryptRequest(decBytes []byte, reply *[]byte) error {
|
|
*reply = wrappers.Encrypt(decBytes, globalPassword, true, false)
|
|
return nil
|
|
}
|
|
|
|
// getFileHash returns the SHA256 hash of the file at the given path.
|
|
func getFileHash(path string) ([]byte, error) {
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, errors.New("unable to read path (" + path + ") for hashing: " + err.Error())
|
|
}
|
|
defer file.Close()
|
|
hash := sha256.New()
|
|
io.Copy(hash, file)
|
|
return hash.Sum(nil), nil
|
|
}
|