Skip to content

Commit

Permalink
refactor: use snake_case for variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
csirianni committed Dec 10, 2023
1 parent 5a503b1 commit 9a83291
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
18 changes: 9 additions & 9 deletions backend/src/cryptography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ namespace cryptography
crypto_core_ristretto255_from_hash(point, hash);

// encrypt password
unsigned char encryptedPassword[crypto_core_ristretto255_BYTES + offset];
crypto_scalarmult_ristretto255(encryptedPassword + offset, b, point);
memcpy(encryptedPassword, point, offset);
unsigned char encrypted_password[crypto_core_ristretto255_BYTES + offset];
crypto_scalarmult_ristretto255(encrypted_password + offset, b, point);
memcpy(encrypted_password, point, offset);

return std::string(encryptedPassword, encryptedPassword + crypto_core_ristretto255_BYTES + offset);
return std::string(encrypted_password, encrypted_password + crypto_core_ristretto255_BYTES + offset);
}

std::string encryptPassword(const std::string &password, unsigned char *b, size_t offset)
{
std::string rawPassword = password.substr(offset, crypto_core_ristretto255_BYTES);
const unsigned char *data = (const unsigned char *)rawPassword.data();
unsigned char encryptedPassword[crypto_core_ristretto255_BYTES];
crypto_scalarmult_ristretto255(encryptedPassword, b, data);
return std::string(encryptedPassword, encryptedPassword + crypto_core_ristretto255_BYTES);
std::string raw_password = password.substr(offset, crypto_core_ristretto255_BYTES);
const unsigned char *data = (const unsigned char *)raw_password.data();
unsigned char encrypted_password[crypto_core_ristretto255_BYTES];
crypto_scalarmult_ristretto255(encrypted_password, b, data);
return std::string(encrypted_password, encrypted_password + crypto_core_ristretto255_BYTES);
}

std::vector<std::string> encrypt(const std::unordered_set<std::string> &passwords, unsigned char *b, size_t offset)
Expand Down
36 changes: 18 additions & 18 deletions backend/tests/cryptography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ TEST_CASE("Test hashAndEncryptPassword")
crypto_core_ristretto255_scalar_invert(inverse, b);

// compute expected value for password after computation
unsigned char expectedHash[crypto_core_ristretto255_HASHBYTES];
crypto_generichash(expectedHash, sizeof expectedHash, (const unsigned char *)password.data(), password.length(), NULL, 0);
unsigned char expectedPoint[crypto_core_ristretto255_BYTES];
crypto_core_ristretto255_from_hash(expectedPoint, expectedHash);
unsigned char expected_hash[crypto_core_ristretto255_HASHBYTES];
crypto_generichash(expected_hash, sizeof expected_hash, (const unsigned char *)password.data(), password.length(), NULL, 0);
unsigned char expected_point[crypto_core_ristretto255_BYTES];
crypto_core_ristretto255_from_hash(expected_point, expected_hash);

SECTION("without leaked byte")
{
for (int i = 0; i < 30; ++i)
{
// encrypt password
std::string encryptedPasswordStr = cryptography::hashAndEncryptPassword(password, b);
unsigned char encryptedPassword[crypto_core_ristretto255_BYTES];
memcpy(encryptedPassword, encryptedPasswordStr.data(), crypto_core_ristretto255_BYTES);
std::string expected_password_str = cryptography::hashAndEncryptPassword(password, b);
unsigned char encrypted_password[crypto_core_ristretto255_BYTES];
memcpy(encrypted_password, expected_password_str.data(), crypto_core_ristretto255_BYTES);

// unencrypt the password with the inverse of b
unsigned char decryptedPassword[crypto_core_ristretto255_BYTES];
int result = crypto_scalarmult_ristretto255(decryptedPassword, inverse, encryptedPassword);
unsigned char decrypted_password[crypto_core_ristretto255_BYTES];
int result = crypto_scalarmult_ristretto255(decrypted_password, inverse, encrypted_password);
REQUIRE(result == 0);

CHECK(std::memcmp(expectedPoint, decryptedPassword, crypto_core_ristretto255_BYTES) == 0);
CHECK(std::memcmp(expected_point, decrypted_password, crypto_core_ristretto255_BYTES) == 0);
}
}

Expand All @@ -42,18 +42,18 @@ TEST_CASE("Test hashAndEncryptPassword")
{
const size_t offset = 1;
// encrypt password
std::string encryptedPasswordStr = cryptography::hashAndEncryptPassword(password, b, offset);
unsigned char encryptedPassword[crypto_core_ristretto255_BYTES + offset];
memcpy(encryptedPassword, encryptedPasswordStr.data(), crypto_core_ristretto255_BYTES + offset);
std::string encrypted_password_str = cryptography::hashAndEncryptPassword(password, b, offset);
unsigned char encrypted_password[crypto_core_ristretto255_BYTES + offset];
memcpy(encrypted_password, encrypted_password_str.data(), crypto_core_ristretto255_BYTES + offset);

// decrypt password
unsigned char decryptedPassword[crypto_core_ristretto255_BYTES + offset];
int result = crypto_scalarmult_ristretto255(decryptedPassword + offset, inverse, encryptedPassword + offset);
unsigned char decrypted_password[crypto_core_ristretto255_BYTES + offset];
int result = crypto_scalarmult_ristretto255(decrypted_password + offset, inverse, encrypted_password + offset);
REQUIRE(result == 0);
memcpy(decryptedPassword, encryptedPassword, offset);
memcpy(decrypted_password, encrypted_password, offset);

CHECK(std::memcmp(expectedPoint, decryptedPassword + offset, crypto_core_ristretto255_BYTES) == 0);
CHECK(expectedPoint[0] == decryptedPassword[0]);
CHECK(std::memcmp(expected_point, decrypted_password + offset, crypto_core_ristretto255_BYTES) == 0);
CHECK(expected_point[0] == decrypted_password[0]);
}
}
}

0 comments on commit 9a83291

Please sign in to comment.