Merge pull request #3 from EntireTwix/BetterHash

Better hash function
This commit is contained in:
William Katz 2021-04-19 14:27:42 -07:00 committed by GitHub
commit af6fff0793
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 11 deletions

View file

@ -33,7 +33,7 @@ make -j<threads>
sudo ./bank <admin password> <saving frequency in minutes> <threads>
```
you also have to edit the config file to add your cert locations, I personally use cert bot
**you also have to edit the config file to add your cert locations**, I personally use cert bot
### Connected Services
@ -64,7 +64,7 @@ Ideas:
- written in **C++**, arguably the fastest language
- **multi-threaded**
- **parallel hashmaps** a far superior HashMap implementation to the STD, that also benefit from multi-threaded
- **Passwords are Hashed**, allowing each user to be 12 bytes, which is allows hashmap to be flat
- **Passwords are Hashed**, allowing each user to be 12 bytes, which is allows hashmap to be flat. This hashing is also very fast at 31.5 GB/s on a i7-9700K
### Safety
@ -77,8 +77,10 @@ Ideas:
- **Web front-end**
- **RESTful** API for connected services like a market, gambling, or anything else you can think of
- able to be used millions of blocks away, across dimensions, servers, **vanilla or modded**. In contrast to an in-game modded implementation that would be range limited.
- **Logging**
#### Dependencies
- [Parallel HashMap](https://github.com/greg7mdp/parallel-hashmap/tree/master)
- [drogon web framework (and all its dependencies)](https://github.com/an-tao/drogon/tree/master)
- [Drogon](https://github.com/an-tao/drogon/tree/master)
- [XXHASH](https://github.com/Cyan4973/xxHash)

View file

@ -54,7 +54,7 @@ public:
bool DelUser(const std::string &name, const std::string &attempt)
{
std::shared_lock<std::shared_mutex> lock{size_lock};
return users.erase_if(name, [&attempt](const User &u) { return (XXH64(attempt.data(), attempt.size(), 1) == u.password); });
return users.erase_if(name, [&attempt](const User &u) { return (XXH3_64bits(attempt.data(), attempt.size()) == u.password); });
}
bool AdminDelUser(const std::string &name, const std::string &attempt)
{
@ -75,7 +75,7 @@ public:
bool state = false;
std::shared_lock<std::shared_mutex> lock{send_funds_l}; //because SendFunds requires 3 locking operations
users.modify_if(a_name, [&state, amount, &attempt](User &a) {
if (state = (a.balance >= amount) && (a.password == XXH64(attempt.data(), attempt.size(), 1)))
if (state = (a.balance >= amount) && (a.password == XXH3_64bits(attempt.data(), attempt.size())))
{
a.balance -= amount;
}
@ -130,7 +130,7 @@ public:
{
int_fast8_t res = -1;
users.if_contains(name, [&res, &attempt](const User &u) {
res = u.password == XXH64(attempt.data(), attempt.size(), 1);
res = u.password == XXH3_64bits(attempt.data(), attempt.size());
});
return res;
}
@ -138,10 +138,10 @@ public:
{
int_fast8_t res = -1;
users.modify_if(name, [&res, &attempt, &new_pass](User &u) {
res = (u.password == XXH64(attempt.data(), attempt.size(), 1));
res = (u.password == XXH3_64bits(attempt.data(), attempt.size()));
if (res)
{
u.password = XXH64(new_pass.data(), new_pass.size(), 1);
u.password = XXH3_64bits(new_pass.data(), new_pass.size());
}
});
return res;

View file

@ -13,7 +13,7 @@ struct User
*
* @param init_pass initial password
*/
User(std::string &&init_pass) : password(XXH64(init_pass.data(), init_pass.size(), 1)) {}
User(std::string &&init_pass) : password(XXH3_64bits(init_pass.data(), init_pass.size())) {}
/**
* @brief User Constructor for admins
@ -21,7 +21,7 @@ struct User
* @param init_bal initial balance
* @param init_pass initial password
*/
User(uint_fast32_t init_bal, std::string &&init_pass, bool state = false) : balance(init_bal), password(XXH64(init_pass.data(), init_pass.size(), 1)), is_admin(state) {}
User(uint_fast32_t init_bal, std::string &&init_pass, bool state = false) : balance(init_bal), password(XXH3_64bits(init_pass.data(), init_pass.size())), is_admin(state) {}
User(uint_fast32_t init_bal, uint64_t init_pass, bool state = false) : balance(init_bal), password(init_pass), is_admin(state) {}
Json::Value Serialize() const

View file

@ -44,7 +44,7 @@ int main(int argc, char **argv)
[](const drogon::HttpRequestPtr &req, const drogon::HttpResponsePtr &resp) {
resp->addHeader("Access-Control-Allow-Origin", "*");
});
app().loadConfigFile("../config.json").addListener("0.0.0.0", 80).registerController(API).setThreadNum(std::stoul(argv[3])).enableRunAsDaemon().run();
app().loadConfigFile("../config.json").registerController(API).setThreadNum(std::stoul(argv[3])).enableRunAsDaemon().run();
return 0;
}