From 97b0b6fc0cb58e4c8828e2a29fad5031548bb7c0 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 12 Aug 2021 22:05:02 +0300 Subject: [PATCH] picloud hash key --- libs/cloud/picloudtcp.cpp | 4 ++-- libs/crypt/picrypt.cpp | 13 +++++++++++++ libs/main/crypt/picrypt.h | 3 +++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/libs/cloud/picloudtcp.cpp b/libs/cloud/picloudtcp.cpp index c1e3af3b..7f1e0d38 100644 --- a/libs/cloud/picloudtcp.cpp +++ b/libs/cloud/picloudtcp.cpp @@ -24,7 +24,7 @@ #include "pistreampacker.h" -const char hash_def_key[] = "_picrypt_"; +const char hash_cloud_key[] = "_picloud_"; PICloud::TCP::Header::Header() { @@ -43,7 +43,7 @@ void PICloud::TCP::setRole(PICloud::TCP::Role r) { void PICloud::TCP::setServerName(const PIString & server_name_) { server_name = server_name_; - suuid = PICrypt::hash(server_name_); + suuid = PICrypt::hash(PIByteArray(server_name_.data(), server_name_.size()), (const unsigned char *)hash_cloud_key, sizeof(hash_cloud_key)); } diff --git a/libs/crypt/picrypt.cpp b/libs/crypt/picrypt.cpp index f80c8daf..9b35c202 100644 --- a/libs/crypt/picrypt.cpp +++ b/libs/crypt/picrypt.cpp @@ -169,6 +169,19 @@ PIByteArray PICrypt::hash(const PIByteArray & data) { } +PIByteArray PICrypt::hash(const PIByteArray & data, const unsigned char *key, size_t keylen) { + PIByteArray hash; +#ifdef PIP_CRYPT + if (!init()) return hash; + hash.resize(crypto_generichash_BYTES); + crypto_generichash(hash.data(), hash.size(), data.data(), data.size(), key, keylen); +#else + PICRYPT_DISABLED_WARNING +#endif + return hash; +} + + size_t PICrypt::sizeHash() { #ifdef PIP_CRYPT return crypto_generichash_BYTES; diff --git a/libs/main/crypt/picrypt.h b/libs/main/crypt/picrypt.h index 12b285cc..bee2b810 100644 --- a/libs/main/crypt/picrypt.h +++ b/libs/main/crypt/picrypt.h @@ -58,6 +58,9 @@ public: //! Generate hash from bytearray static PIByteArray hash(const PIByteArray & data); + //! Generate hash from bytearray + static PIByteArray hash(const PIByteArray & data, const unsigned char * key, size_t keylen); + //! Returns hash size static size_t sizeHash();