add shorthash to PICrypt

git-svn-id: svn://db.shs.com.ru/pip@130 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2015-05-15 12:19:14 +00:00
parent b2c9023deb
commit a4fadea95f
2 changed files with 19 additions and 0 deletions

View File

@@ -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 PICrypt::generateKey() {
PIByteArray hash; PIByteArray hash;
#ifdef PIP_CRYPT #ifdef PIP_CRYPT

View File

@@ -54,6 +54,9 @@ public:
//! Generate hash from keyphrase "secret", may be used as a key for encryption //! Generate hash from keyphrase "secret", may be used as a key for encryption
static PIByteArray hash(const PIString & secret); 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 //! Generate random key
static PIByteArray generateKey(); static PIByteArray generateKey();