nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
password.cc
Go to the documentation of this file.
1#include "server/password.h"
2
3#include <sodium.h>
4
5#include <array>
6#include <string>
7
8#include "absl/log/absl_check.h"
9#include "absl/status/status.h"
10#include "server/tracing.h"
11
12namespace nanommo {
13
14namespace {
15
16void InitSodium() {
17 ABSL_CHECK(sodium_init() >= 0) << "couldn't initialise libsodium";
18}
19
20} // namespace
21
22absl::StatusOr<std::string> HashPassword(std::string_view password) {
23 InitSodium();
24 // Slow on purpose, so it's a span of its own.
25 Span span = StartChildSpan("argon2id hash", SpanKind::kInternal);
26 std::array<char, crypto_pwhash_STRBYTES> hash{};
27 // What libsodium suggests for logins: two passes over 64 MiB of memory.
28 if (crypto_pwhash_str(hash.data(), password.data(), password.size(),
29 crypto_pwhash_OPSLIMIT_INTERACTIVE,
30 crypto_pwhash_MEMLIMIT_INTERACTIVE) != 0) {
31 return absl::ResourceExhaustedError("ran out of memory hashing a password");
32 }
33 return std::string(hash.data());
34}
35
36bool VerifyPassword(const std::string& hash, std::string_view password) {
37 InitSodium();
38 Span span = StartChildSpan("argon2id verify", SpanKind::kInternal);
39 return crypto_pwhash_str_verify(hash.c_str(), password.data(),
40 password.size()) == 0;
41}
42
43} // namespace nanommo
Definition chat.cc:9
absl::StatusOr< std::string > HashPassword(std::string_view password)
Definition password.cc:22
bool VerifyPassword(const std::string &hash, std::string_view password)
Definition password.cc:36
Span StartChildSpan(std::string_view name, SpanKind kind)
Definition tracing.cc:404