Changeset View
Changeset View
Standalone View
Standalone View
source/ps/Hashing.cpp
- This file was added.
/* Copyright (C) 2021 Wildfire Games. | |||||
* This file is part of 0 A.D. | |||||
* | |||||
* 0 A.D. is free software: you can redistribute it and/or modify | |||||
* it under the terms of the GNU General Public License as published by | |||||
* the Free Software Foundation, either version 2 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* 0 A.D. is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "precompiled.h" | |||||
#include "ps/CStr.h" | |||||
#include "ps/Util.h" | |||||
#include <sodium.h> | |||||
CStr8 FastHash(const CStr8& string, const CStr8& salt) | |||||
{ | |||||
if (string.empty()) | |||||
return string; | |||||
ENSURE(sodium_init() >= 0); | |||||
constexpr int DIGESTSIZE = crypto_generichash_BYTES; | |||||
static_assert(DIGESTSIZE >= crypto_generichash_BYTES_MIN); | |||||
static_assert(DIGESTSIZE <= crypto_generichash_BYTES_MAX); | |||||
static_assert(DIGESTSIZE >= crypto_generichash_KEYBYTES_MIN); | |||||
static_assert(DIGESTSIZE <= crypto_generichash_KEYBYTES_MAX); | |||||
static_assert(DIGESTSIZE == 32); | |||||
// First generate a fixed-size salt from out variable-sized one (libsodium requires it). | |||||
unsigned char salt_buffer[DIGESTSIZE] = { | |||||
235, 82, 29, 20, 135, 168, 184, 97, 7, 240, 48, 109, 8, 34, 158, 32, | |||||
81, 103, 151, 245, 85, 171, 200, 246, 13, 51, 236, 118, 163, 197, 54, 227 | |||||
}; | |||||
crypto_generichash_state state; | |||||
crypto_generichash_init(&state, salt_buffer, DIGESTSIZE, DIGESTSIZE); | |||||
crypto_generichash_update(&state, reinterpret_cast<const unsigned char*>(salt.c_str()), salt.size()); | |||||
crypto_generichash_final(&state, salt_buffer, DIGESTSIZE); | |||||
// Now that we have a fixed-length key, use that to hash the password. | |||||
unsigned char output[DIGESTSIZE] = { 0 }; | |||||
crypto_generichash(output, DIGESTSIZE, reinterpret_cast<const unsigned char*>(string.c_str()), string.size(), salt_buffer, DIGESTSIZE); | |||||
return CStr(Hexify(output, DIGESTSIZE)).UpperCase(); | |||||
} |
Wildfire Games · Phabricator