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 "third_party/encryption/pkcs5_pbkdf2.h" | |||||
CStr8 HashPassword(const CStr8& password, const CStr8& salt) | |||||
{ | |||||
if (password.empty()) | |||||
return password; | |||||
ENSURE(sodium_init() >= 0); | |||||
const int DIGESTSIZE = crypto_hash_sha256_BYTES; | |||||
constexpr int ITERATIONS = 1737; | |||||
cassert(DIGESTSIZE == 32); | |||||
static const unsigned char salt_base[DIGESTSIZE] = { | |||||
244, 243, 249, 244, 32, 33, 19, 35, 16, 11, 12, 13, 14, 15, 16, 17, | |||||
18, 19, 20, 32, 33, 244, 224, 127, 129, 130, 140, 153, 88, 123, 234, 123 }; | |||||
// initialize the salt buffer | |||||
unsigned char salt_buffer[DIGESTSIZE] = { 0 }; | |||||
crypto_hash_sha256_state state; | |||||
crypto_hash_sha256_init(&state); | |||||
crypto_hash_sha256_update(&state, salt_base, sizeof(salt_base)); | |||||
crypto_hash_sha256_update(&state, reinterpret_cast<const u8*>(salt.data()), salt.size()); | |||||
crypto_hash_sha256_final(&state, salt_buffer); | |||||
// PBKDF2 to create the buffer | |||||
unsigned char encrypted[DIGESTSIZE]; | |||||
pbkdf2(encrypted, (unsigned char*)password.c_str(), password.length(), salt_buffer, DIGESTSIZE, ITERATIONS); | |||||
return CStr(Hexify(encrypted, DIGESTSIZE)).UpperCase(); | |||||
} |
Wildfire Games · Phabricator