Leosac  0.8.0
Open Source Access Control
Scrypt.hpp
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 #pragma once
21 
22 #include <cstdlib>
23 extern "C" {
24 #include <libscrypt.h>
25 }
26 
27 #include <boost/algorithm/hex.hpp>
28 #include <boost/algorithm/string/classification.hpp>
29 #include <boost/algorithm/string/split.hpp>
30 #include <iostream>
31 #include <sstream>
32 #include <stdexcept>
33 #include <vector>
34 
36 {
37  bool operator==(const ScryptParam &o) const;
38 
39  uint64_t N;
40  uint32_t r;
41  uint32_t p;
42  uint32_t len;
43 };
44 
46 {
47  bool operator==(const ScryptResult &o) const;
48  bool operator!=(const ScryptResult &o) const;
49 
54 
58  std::vector<uint8_t> salt;
59 
63  std::vector<uint8_t> hash;
64 };
65 
70 template <typename SerializedType>
72 {
73  public:
74  virtual SerializedType Serialize(const ScryptResult &in) const = 0;
75  virtual ScryptResult UnSerialize(const SerializedType &in) const = 0;
76 
77  static std::string Hex(const std::vector<uint8_t> &in)
78  {
79  std::string res;
80  boost::algorithm::hex(in.begin(), in.end(), back_inserter(res));
81  return res;
82  }
83 
84  static std::vector<uint8_t> UnHex(const std::string &in)
85  {
86  std::vector<uint8_t> res;
87  boost::algorithm::unhex(in.begin(), in.end(), back_inserter(res));
88  return res;
89  }
90 
91  static std::vector<uint8_t> ToByteVector(const std::string &in)
92  {
93  return std::vector<uint8_t>(in.begin(), in.end());
94  }
95 };
96 
98 {
99  public:
100  virtual std::string Serialize(const ScryptResult &in) const override
101  {
102  std::stringstream ss;
103  ss << in.p.N << ":";
104  ss << in.p.r << ":";
105  ss << in.p.p << ":";
106  ss << Hex(in.salt) << ":";
107  ss << Hex(in.hash);
108 
109  return ss.str();
110  }
111 
112  virtual ScryptResult UnSerialize(const std::string &in) const override
113  {
114  ScryptResult result;
115  std::vector<std::string> bla;
116  boost::algorithm::split(bla, in, boost::algorithm::is_any_of(":"));
117 
118  if (bla.size() != 5)
119  throw std::runtime_error("Cannot unserialize.");
120 
121  result.p.N = std::stoi(bla[0]);
122  result.p.r = std::stoi(bla[1]);
123  result.p.p = std::stoi(bla[2]);
124  result.salt = UnHex(bla[3]);
125  result.hash = UnHex(bla[4]);
126  result.p.len = result.hash.size();
127 
128  return result;
129  }
130 };
131 
132 class Scrypt
133 {
134  public:
143  static ScryptResult Hash(const std::vector<uint8_t> &in,
144  const std::vector<uint8_t> &salt,
145  const ScryptParam &param = default_);
146 
156  static ScryptResult Hash(const std::vector<uint8_t> &in,
157  const ScryptParam &param = default_);
158 
166  static bool Verify(const std::vector<uint8_t> &in, const ScryptResult &expected);
167 
168  private:
170 };
ScryptParam::N
uint64_t N
Definition: Scrypt.hpp:39
ScryptResultSerializer::UnHex
static std::vector< uint8_t > UnHex(const std::string &in)
Definition: Scrypt.hpp:84
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
ScryptResultSerializer
Interface to serialize a ScryptResult to a SerializedType object.
Definition: Scrypt.hpp:71
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
ScryptResultSerializer::Hex
static std::string Hex(const std::vector< uint8_t > &in)
Definition: Scrypt.hpp:77
ScryptResultSerializer::UnSerialize
virtual ScryptResult UnSerialize(const SerializedType &in) const =0
ScryptParam::operator==
bool operator==(const ScryptParam &o) const
Definition: Scrypt.cpp:55
StringScryptResultSerializer::UnSerialize
virtual ScryptResult UnSerialize(const std::string &in) const override
Definition: Scrypt.hpp:112
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
ScryptResult::operator!=
bool operator!=(const ScryptResult &o) const
Definition: Scrypt.cpp:65
StringScryptResultSerializer::Serialize
virtual std::string Serialize(const ScryptResult &in) const override
Definition: Scrypt.hpp:100
Scrypt::default_
static ScryptParam default_
Definition: Scrypt.hpp:169
ScryptParam::p
uint32_t p
Definition: Scrypt.hpp:41
ScryptResultSerializer::Serialize
virtual SerializedType Serialize(const ScryptResult &in) const =0
Scrypt
Definition: Scrypt.hpp:132
ScryptResultSerializer::ToByteVector
static std::vector< uint8_t > ToByteVector(const std::string &in)
Definition: Scrypt.hpp:91
StringScryptResultSerializer
Definition: Scrypt.hpp:97
ScryptParam::r
uint32_t r
Definition: Scrypt.hpp:40
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