diff --git a/src/math/picrypt.cpp b/src/math/picrypt.cpp index 6ff2c15c..32c8b22f 100644 --- a/src/math/picrypt.cpp +++ b/src/math/picrypt.cpp @@ -146,6 +146,22 @@ PIByteArray PICrypt::hash(const PIString & secret) { } +ullong PICrypt::shorthash(const PIString& s, PIByteArray key) { + ullong hash; +#ifdef PIP_CRYPT + if (crypto_shorthash_BYTES != sizeof(hash)) piCout << "[PICrypt]" << "internal error: bad hash size"; + sodium_init(); + if (key.size() != crypto_shorthash_KEYBYTES) { + key.resize(crypto_shorthash_KEYBYTES); + randombytes_buf(key.data(), key.size()); + } + PIByteArray in(s.data(), s.size()); + crypto_shorthash((uchar *)&hash, in.data(), in.size(), key.data()); +#endif + return hash; +} + + PIByteArray PICrypt::generateKey() { PIByteArray hash; #ifdef PIP_CRYPT diff --git a/src/math/picrypt.h b/src/math/picrypt.h index 0dfc62fe..767b1349 100644 --- a/src/math/picrypt.h +++ b/src/math/picrypt.h @@ -54,6 +54,9 @@ public: //! Generate hash from keyphrase "secret", may be used as a key for encryption static PIByteArray hash(const PIString & secret); + //! Generate short hash from string "s", may be used for hash table + static ullong shorthash(const PIString & s, PIByteArray key = PIByteArray()); + //! Generate random key static PIByteArray generateKey();