diff --git a/src/storagebackend.cpp b/src/storagebackend.cpp index ec87e33b3c2ae08eb8632a855408034138b8a032..ce27dfae0e46cb7914b86e68977b3befb73e2f27 100644 --- a/src/storagebackend.cpp +++ b/src/storagebackend.cpp @@ -5,6 +5,8 @@ #include "transport/mysqlbackend.h" #include "transport/pqxxbackend.h" +#include "Swiften/Swiften.h" + namespace Transport { StorageBackend *StorageBackend::createBackend(Config *config, std::string &error) { @@ -47,4 +49,56 @@ StorageBackend *StorageBackend::createBackend(Config *config, std::string &error return storageBackend; } +std::string StorageBackend::encryptPassword(const std::string &password, const std::string &key) { + std::string encrypted; + encrypted.resize(password.size()); + for (int i = 0; i < password.size(); i++) { + char c = password[i]; + char keychar = key[i % key.size()]; + c += keychar; + encrypted[i] = c; + } + + encrypted = Swift::Base64::encode(Swift::createByteArray(encrypted)); + return encrypted; +} + +std::string StorageBackend::decryptPassword(std::string &encrypted, const std::string &key) { + encrypted = Swift::byteArrayToString(Swift::Base64::decode(encrypted)); + std::string password; + password.resize(encrypted.size()); + for (int i = 0; i < encrypted.size(); i++) { + char c = encrypted[i]; + char keychar = key[i % key.size()]; + c -= keychar; + password[i] = c; + } + + return password; +} + +std::string StorageBackend::serializeGroups(const std::vector &groups) { + std::string ret; + BOOST_FOREACH(const std::string &group, groups) { + ret += group + "\n"; + } + if (!ret.empty()) { + ret.erase(ret.end() - 1); + } + return ret; +} + +std::vector StorageBackend::deserializeGroups(std::string &groups) { + std::vector ret; + if (groups.empty()) { + return ret; + } + + boost::split(ret, groups, boost::is_any_of("\n")); + if (ret.back().empty()) { + ret.erase(ret.end() - 1); + } + return ret; +} + }