bcrypt_functions.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_PASSHASH_BCRYPT_FUNCTIONS_HPP
26 #define CRYPTO3_PASSHASH_BCRYPT_FUNCTIONS_HPP
27 
29 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace passhash {
35  namespace detail {
36  template<typename BlockCipher>
37  struct bcrypt_functions : public bcrypt_policy<BlockCipher> {
39 
41 
42  std::string bcrypt_base64_encode(const uint8_t input[], size_t length) {
43  std::string b64 = base64_encode(input, length);
44 
45  while (!b64.empty() && b64[b64.size() - 1] == '=') {
46  b64 = b64.substr(0, b64.size() - 1);
47  }
48 
49  for (size_t i = 0; i != b64.size(); ++i) {
50  b64[i] = policy_type::substitution()[static_cast<uint8_t>(b64[i])];
51  }
52 
53  return b64;
54  }
55 
56  std::vector<uint8_t> bcrypt_base64_decode(const std::string& input) {
57  for (size_t i = 0; i != input.size(); ++i) {
58  input[i] = policy_type::inverted_substitution()[static_cast<uint8_t>(input[i])];
59  }
60 
61  return unlock(base64_decode(input));
62  }
63  };
64  } // namespace detail
65  } // namespace passhash
66  } // namespace crypto3
67 } // namespace nil
68 
69 #endif // CRYPTO3_BCRYPT_FUNCTIONS_HPP
boost::mpl::apply< AccumulatorSet, tag::passhash< Hash > >::type::result_type passhash(const AccumulatorSet &acc)
Definition: accumulators/passhash.hpp:121
Definition: pair.hpp:31
Definition: bcrypt_functions.hpp:37
policy_type::cipher_type cipher_type
Definition: bcrypt_functions.hpp:40
std::string bcrypt_base64_encode(const uint8_t input[], size_t length)
Definition: bcrypt_functions.hpp:42
bcrypt_policy< BlockCipher > policy_type
Definition: bcrypt_functions.hpp:38
std::vector< uint8_t > bcrypt_base64_decode(const std::string &input)
Definition: bcrypt_functions.hpp:56
Definition: bcrypt_policy.hpp:41
BlockCipher cipher_type
Definition: bcrypt_policy.hpp:44