diff --git a/daemon/src/dht/dhtaccount.cpp b/daemon/src/dht/dhtaccount.cpp
index 8ff471d02178a0d59b89e4df7beec5a61d5997c7..8c22c2c3fc8a107fb4adb69e8bfa0e0a1dc40bc3 100644
--- a/daemon/src/dht/dhtaccount.cpp
+++ b/daemon/src/dht/dhtaccount.cpp
@@ -345,35 +345,11 @@ DHTAccount::checkIdentityPath()
 dht::crypto::Identity
 DHTAccount::loadIdentity() const
 {
-    std::vector<char> buffer;
-    std::vector<char> buffer_crt;
+    std::vector<uint8_t> buffer;
+    std::vector<uint8_t> buffer_crt;
     try {
-        {
-            std::ifstream file(privkeyPath_, std::ios::binary);
-            if (!file)
-                throw std::runtime_error("Can't read private key file.");
-            file.seekg(0, std::ios::end);
-            std::streamsize size = file.tellg();
-            if (size > std::numeric_limits<unsigned>::max())
-                throw std::runtime_error("Can't read private key file.");
-            buffer.resize(size);
-            file.seekg(0, std::ios::beg);
-            if (!file.read(buffer.data(), size))
-                throw std::runtime_error("Can't load private key.");
-        }
-        {
-            std::ifstream file(certPath_, std::ios::binary);
-            if (!file)
-                throw std::runtime_error("Can't read certificate file.");
-            file.seekg(0, std::ios::end);
-            std::streamsize size = file.tellg();
-            if (size > std::numeric_limits<unsigned>::max())
-                throw std::runtime_error("Can't read certificate file.");
-            buffer_crt.resize(size);
-            file.seekg(0, std::ios::beg);
-            if (!file.read(buffer_crt.data(), size))
-                throw std::runtime_error("Can't load certificate.");
-        }
+        buffer = fileutils::loadFile(privkeyPath_);
+        buffer_crt = fileutils::loadFile(certPath_);
     }
     catch (const std::exception& e) {
         SFL_ERR("Error loading identity: %s", e.what());
@@ -421,25 +397,10 @@ DHTAccount::loadIdentity() const
 void
 DHTAccount::saveIdentity(const dht::crypto::Identity id) const
 {
-    if (id.first) {
-        auto buffer = id.first->serialize();
-        std::ofstream file(privkeyPath_, std::ios::trunc | std::ios::binary);
-        if (!file.is_open()) {
-            SFL_ERR("Could not write key to %s", privkeyPath_.c_str());
-            return;
-        }
-        file.write((char*)buffer.data(), buffer.size());
-    }
-
-    if (id.second) {
-        auto buffer = id.second->getPacked();
-        std::ofstream file(certPath_, std::ios::trunc | std::ios::binary);
-        if (!file.is_open()) {
-            SFL_ERR("Could not write key to %s", certPath_.c_str());
-            return;
-        }
-        file.write((char*)buffer.data(), buffer.size());
-    }
+    if (id.first)
+        fileutils::saveFile(privkeyPath_, id.first->serialize());
+    if (id.second)
+        fileutils::saveFile(certPath_, id.second->getPacked());
 }
 
 template <typename T>
diff --git a/daemon/src/fileutils.cpp b/daemon/src/fileutils.cpp
index 02a009aa857daee0d8c769991725b697f97d0aaf..c2776351c056ba970ff06c04c0d1a2189556d566 100644
--- a/daemon/src/fileutils.cpp
+++ b/daemon/src/fileutils.cpp
@@ -54,6 +54,8 @@
 #include <sstream>
 #include <fstream>
 #include <iostream>
+#include <stdexcept>
+#include <limits>
 
 #include <cstdlib>
 #include <cstring>
@@ -211,6 +213,35 @@ bool isDirectoryWritable(const std::string &directory)
     return access(directory.c_str(), W_OK) == 0;
 }
 
+std::vector<uint8_t>
+loadFile(const std::string& path)
+{
+    std::vector<uint8_t> buffer;
+    std::ifstream file(path, std::ios::binary);
+    if (!file)
+        throw std::runtime_error("Can't read file: "+path);
+    file.seekg(0, std::ios::end);
+    std::streamsize size = file.tellg();
+    if (size > std::numeric_limits<unsigned>::max())
+        throw std::runtime_error("File is too big: "+path);
+    buffer.resize(size);
+    file.seekg(0, std::ios::beg);
+    if (!file.read((char*)buffer.data(), size))
+        throw std::runtime_error("Can't load file: "+path);
+    return buffer;
+}
+
+void
+saveFile(const std::string& path, const std::vector<uint8_t>& data)
+{
+    std::ofstream file(path, std::ios::trunc | std::ios::binary);
+    if (!file.is_open()) {
+        SFL_ERR("Could not write data to %s", path.c_str());
+        return;
+    }
+    file.write((char*)data.data(), data.size());
+}
+
 static size_t
 dirent_buf_size(DIR * dirp)
 {
diff --git a/daemon/src/fileutils.h b/daemon/src/fileutils.h
index 17602d6f1320adb7b210dcc4f319a038b64a5398..5379f0d143299bd96e00ee36a7267246ec9d84c6 100644
--- a/daemon/src/fileutils.h
+++ b/daemon/src/fileutils.h
@@ -65,6 +65,9 @@ namespace fileutils {
      */
     std::vector<std::string> readDirectory(const std::string &dir);
 
+    std::vector<uint8_t> loadFile(const std::string& path);
+    void saveFile(const std::string& path, const std::vector<uint8_t>& data);
+
     struct FileHandle {
         int fd;
         const std::string name;