Leosac  0.8.0
Open Source Access Control
Scrypt.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "tools/scrypt/Scrypt.hpp"
21 #include "tools/scrypt/Random.hpp"
22 
23 ScryptParam Scrypt::default_ = {.N = 16384, .r = 8, .p = 1, .len = 64};
24 
25 ScryptResult Scrypt::Hash(const std::vector<uint8_t> &in,
26  const std::vector<uint8_t> &salt, const ScryptParam &param)
27 {
28  ScryptResult res;
29  res.p = param;
30  res.salt = salt;
31 
32  std::vector<uint8_t> out;
33  out.resize(param.len);
34 
35  int ret = libscrypt_scrypt(in.data(), in.size(), salt.data(), salt.size(),
36  param.N, param.r, param.p, out.data(), out.size());
37  if (ret != 0)
38  throw std::runtime_error("FAILED TO SCRYPT");
39  res.hash = out;
40 
41  return res;
42 }
43 
44 ScryptResult Scrypt::Hash(const std::vector<uint8_t> &in, const ScryptParam &param)
45 {
46  return Hash(in, Random::GetBytes(16), param);
47 }
48 
49 bool Scrypt::Verify(const std::vector<uint8_t> &in, const ScryptResult &expected)
50 {
51  auto out = Hash(in, expected.salt, expected.p);
52  return out.hash == expected.hash;
53 }
54 
56 {
57  return N == o.N && p == o.p && r == o.r && len == o.len;
58 }
59 
61 {
62  return p == o.p && salt == o.salt && hash == o.hash;
63 }
64 
66 {
67  return !(*this == o);
68 }
ScryptParam::N
uint64_t N
Definition: Scrypt.hpp:39
Scrypt::Verify
static bool Verify(const std::vector< uint8_t > &in, const ScryptResult &expected)
Verify that the input in, when hashed, correspond to the expected ScryptResult.
Definition: Scrypt.cpp:49
ScryptParam::len
uint32_t len
Definition: Scrypt.hpp:42
ScryptResult::operator==
bool operator==(const ScryptResult &o) const
Definition: Scrypt.cpp:60
ScryptParam
Definition: Scrypt.hpp:35
ScryptParam::operator==
bool operator==(const ScryptParam &o) const
Definition: Scrypt.cpp:55
ScryptResult::hash
std::vector< uint8_t > hash
The hash.
Definition: Scrypt.hpp:63
ScryptResult::salt
std::vector< uint8_t > salt
Salt used for generation.
Definition: Scrypt.hpp:58
ScryptResult
Definition: Scrypt.hpp:45
Random.hpp
ScryptResult::operator!=
bool operator!=(const ScryptResult &o) const
Definition: Scrypt.cpp:65
Scrypt::default_
static ScryptParam default_
Definition: Scrypt.hpp:169
Random::GetBytes
static ByteVector GetBytes(size_t n)
Generate n random bytes.
Definition: Random.cpp:29
ScryptParam::p
uint32_t p
Definition: Scrypt.hpp:41
ScryptParam::r
uint32_t r
Definition: Scrypt.hpp:40
Scrypt.hpp
Scrypt::Hash
static ScryptResult Hash(const std::vector< uint8_t > &in, const std::vector< uint8_t > &salt, const ScryptParam &param=default_)
Wrapper around low-level hash function.
Definition: Scrypt.cpp:25
ScryptResult::p
ScryptParam p
Parameters used to generated the hash.
Definition: Scrypt.hpp:53