git-svn-id: svn://db.shs.com.ru/pip@355 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-04-14 12:34:45 +00:00
parent be1f348da6
commit d07c71e97d
18 changed files with 66 additions and 58 deletions

View File

@@ -28,7 +28,7 @@ const char hash_def_key[] = "_picrypt_";
PICrypt::PICrypt() {
#ifdef PIP_CRYPT
sodium_init();
if (!sodium_init()) piCout << "[PICrypt]" << "Error while initialize sodium!";
nonce_.resize(crypto_secretbox_NONCEBYTES);
key_.resize(crypto_secretbox_KEYBYTES);
randombytes_buf(key_.data(), key_.size());
@@ -77,7 +77,7 @@ PIByteArray PICrypt::crypt(const PIByteArray & data, PIByteArray key) {
if (key.size() != crypto_secretbox_KEYBYTES)
key.resize(crypto_secretbox_KEYBYTES, ' ');
//return PIByteArray();
sodium_init();
if (!sodium_init()) return retba;
PIByteArray n;
retba.resize(data.size() + crypto_secretbox_MACBYTES);
n.resize(crypto_secretbox_NONCEBYTES);
@@ -123,7 +123,7 @@ PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, PIByteArray key, bo
if (ok) *ok = false;
return PIByteArray();
}
sodium_init();
if (!sodium_init()) return retba;
PIByteArray n;
n.resize(crypto_secretbox_NONCEBYTES);
retba.resize(crypt_data.size() - n.size() - crypto_secretbox_MACBYTES);
@@ -144,7 +144,7 @@ PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, PIByteArray key, bo
PIByteArray PICrypt::hash(const PIString & secret) {
PIByteArray hash;
#ifdef PIP_CRYPT
sodium_init();
if (!sodium_init()) return hash;
hash.resize(crypto_generichash_BYTES);
PIByteArray s(secret.data(), secret.size());
crypto_generichash(hash.data(), hash.size(), s.data(), s.size(), (const uchar*)hash_def_key, sizeof(hash_def_key) - 1);
@@ -159,7 +159,7 @@ ullong PICrypt::shorthash(const PIString& s, PIByteArray key) {
ullong hash = 0;
#ifdef PIP_CRYPT
if (crypto_shorthash_BYTES != sizeof(hash)) piCout << "[PICrypt]" << "internal error: bad hash size";
sodium_init();
if (!sodium_init()) return hash;
if (key.size() != crypto_shorthash_KEYBYTES) {
piCout << "[PICrypt]" << "invalid key size" << key.size() << ", shoud be" << crypto_shorthash_KEYBYTES << ", filled zeros";
key.resize(crypto_shorthash_KEYBYTES, 0);
@@ -176,7 +176,7 @@ ullong PICrypt::shorthash(const PIString& s, PIByteArray key) {
PIByteArray PICrypt::generateKey() {
PIByteArray hash;
#ifdef PIP_CRYPT
sodium_init();
if (!sodium_init()) return hash;
hash.resize(crypto_secretbox_KEYBYTES);
randombytes_buf(hash.data(), hash.size());
#else
@@ -186,7 +186,7 @@ PIByteArray PICrypt::generateKey() {
}
int PICrypt::sizeKey() {
size_t PICrypt::sizeKey() {
#ifdef PIP_CRYPT
return crypto_secretbox_KEYBYTES;
#else
@@ -196,7 +196,7 @@ int PICrypt::sizeKey() {
}
int PICrypt::sizeCrypt() {
size_t PICrypt::sizeCrypt() {
#ifdef PIP_CRYPT
return crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
#else

View File

@@ -61,10 +61,10 @@ public:
static PIByteArray generateKey();
//! Returns key size
static int sizeKey();
static size_t sizeKey();
//! Returns size which be added to data size in encryption process
static int sizeCrypt();
static size_t sizeCrypt();
private:
PIByteArray nonce_, key_;

View File

@@ -115,7 +115,7 @@ public:
static _CVector filled(const Type & v) {_CVector vv; PIMV_FOR(i, 0) vv[i] = v; return vv;}
private:
void resize(const Type & new_value = Type()) {for (int i = 0; i < Size; ++i) c[i] = new_value;}
void resize(const Type & new_value = Type()) {for (uint i = 0; i < Size; ++i) c[i] = new_value;}
Type c[Size];