| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 3 | // | ||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 6 | // | ||
| 7 | // Official repository: https://github.com/cppalliance/capy | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_CAPY_BCRYPT_RESULT_HPP | ||
| 11 | #define BOOST_CAPY_BCRYPT_RESULT_HPP | ||
| 12 | |||
| 13 | #include <boost/capy/detail/config.hpp> | ||
| 14 | #include <boost/capy/bcrypt/version.hpp> | ||
| 15 | #include <boost/core/detail/string_view.hpp> | ||
| 16 | #include <boost/system/error_code.hpp> | ||
| 17 | #include <cstddef> | ||
| 18 | |||
| 19 | namespace boost { | ||
| 20 | namespace capy { | ||
| 21 | namespace bcrypt { | ||
| 22 | |||
| 23 | /** Fixed-size buffer for bcrypt hash output. | ||
| 24 | |||
| 25 | Stores a bcrypt hash string (max 60 chars) in an | ||
| 26 | inline buffer with no heap allocation. | ||
| 27 | |||
| 28 | @par Example | ||
| 29 | @code | ||
| 30 | bcrypt::result r = bcrypt::hash("password", 10); | ||
| 31 | core::string_view sv = r; // or r.str() | ||
| 32 | std::cout << r.c_str(); // null-terminated | ||
| 33 | @endcode | ||
| 34 | */ | ||
| 35 | class result | ||
| 36 | { | ||
| 37 | char buf_[61]; // 60 chars + null terminator | ||
| 38 | unsigned char size_; | ||
| 39 | |||
| 40 | public: | ||
| 41 | /** Default constructor. | ||
| 42 | |||
| 43 | Constructs an empty result. | ||
| 44 | */ | ||
| 45 | 22 | result() noexcept | |
| 46 | 22 | : size_(0) | |
| 47 | { | ||
| 48 | 22 | buf_[0] = '\0'; | |
| 49 | 22 | } | |
| 50 | |||
| 51 | /** Return the hash as a string_view. | ||
| 52 | */ | ||
| 53 | core::string_view | ||
| 54 | 23 | str() const noexcept | |
| 55 | { | ||
| 56 | 23 | return core::string_view(buf_, size_); | |
| 57 | } | ||
| 58 | |||
| 59 | /** Implicit conversion to string_view. | ||
| 60 | */ | ||
| 61 | operator core::string_view() const noexcept | ||
| 62 | { | ||
| 63 | return str(); | ||
| 64 | } | ||
| 65 | |||
| 66 | /** Return null-terminated C string. | ||
| 67 | */ | ||
| 68 | char const* | ||
| 69 | 1 | c_str() const noexcept | |
| 70 | { | ||
| 71 | 1 | return buf_; | |
| 72 | } | ||
| 73 | |||
| 74 | /** Return pointer to data. | ||
| 75 | */ | ||
| 76 | char const* | ||
| 77 | data() const noexcept | ||
| 78 | { | ||
| 79 | return buf_; | ||
| 80 | } | ||
| 81 | |||
| 82 | /** Return size in bytes (excludes null terminator). | ||
| 83 | */ | ||
| 84 | std::size_t | ||
| 85 | 5 | size() const noexcept | |
| 86 | { | ||
| 87 | 5 | return size_; | |
| 88 | } | ||
| 89 | |||
| 90 | /** Check if result is empty. | ||
| 91 | */ | ||
| 92 | bool | ||
| 93 | 4 | empty() const noexcept | |
| 94 | { | ||
| 95 | 4 | return size_ == 0; | |
| 96 | } | ||
| 97 | |||
| 98 | /** Check if result contains valid data. | ||
| 99 | */ | ||
| 100 | explicit | ||
| 101 | 2 | operator bool() const noexcept | |
| 102 | { | ||
| 103 | 2 | return size_ != 0; | |
| 104 | } | ||
| 105 | |||
| 106 | private: | ||
| 107 | friend BOOST_CAPY_DECL result gen_salt(unsigned, version); | ||
| 108 | friend BOOST_CAPY_DECL result hash(core::string_view, unsigned, version); | ||
| 109 | friend BOOST_CAPY_DECL result hash(core::string_view, core::string_view, system::error_code&); | ||
| 110 | |||
| 111 | 19 | char* buf() noexcept { return buf_; } | |
| 112 | 19 | void set_size(unsigned char n) noexcept | |
| 113 | { | ||
| 114 | 19 | size_ = n; | |
| 115 | 19 | buf_[n] = '\0'; | |
| 116 | 19 | } | |
| 117 | }; | ||
| 118 | |||
| 119 | } // bcrypt | ||
| 120 | } // capy | ||
| 121 | } // boost | ||
| 122 | |||
| 123 | #endif | ||
| 124 | |||
| 125 |