bufferClass
Class for managing encrypted buffers. More...
Public Functions
Type | Name |
---|---|
buffer (const const_buffer< const_key, Hash, const_size > &src) noexcept | |
buffer (const ::std::string &src) noexcept | |
buffer (const buffer &src)=default | |
buffer (const buffer &&src) noexcept | |
buffer & | operator= (const buffer &other) |
buffer & | operator= (buffer &&other) noexcept |
char | operator[] (const size_t index) const noexcept |
size_t | size () const |
bool | empty () const noexcept |
void | append (const buffer &cs) |
size_t | get_hash () const |
~buffer () noexcept |
Private Properties
Type | Name |
---|---|
size_t | key_ |
size_t | hash_ |
Friends
Type | Name |
---|---|
bool | operator== (const buffer &lhs, const buffer &rhs) noexcept |
bool | operator!= (const buffer &lhs, const buffer &rhs) noexcept |
bool | operator< (const buffer &lhs, const buffer &rhs) noexcept |
bool | operator<= (const buffer &lhs, const buffer &rhs) noexcept |
bool | operator> (const buffer &lhs, const buffer &rhs) noexcept |
bool | operator>= (const buffer &lhs, const buffer &rhs) noexcept |
Detailed Description
The buffer
class extends std::vector<char>
and is designed to handle encrypted data securely. It provides a set of features for encoding, decoding, and managing the encrypted content, leveraging the underlying dynamic array capabilities of std::vector<char>
. This class is a key component in systems where data needs to be stored and transmitted securely, with built-in support for encryption and decryption operations.
Key Features:
Inheritance :
Inherits from
std::vector<char>
, giving it all the capabilities of a dynamic array, including automatic memory management, resizing, and element access.Adds additional layers of security by integrating encryption and decryption mechanisms.
Data Security :
The
buffer
class automatically encrypts data on input and decrypts it on access, ensuring that the data remains secure throughout its lifecycle.Uses a secure encryption key (
key_
) and a hash value (hash_
) to maintain data integrity and prevent unauthorized access or tampering.
Constructors :
Supports construction from constant buffers (
const_buffer
), strings (std::string
), and otherbuffer
objects, with both copy and move semantics.The constructors ensure that the data is correctly encrypted and that the key and hash are appropriately set.
Operators :
Provides several overloaded operators for element access (
operator[]
), comparison (==
,!=
,<
,<=
,>
,>=
), and type conversion (tostd::string
).These operators facilitate easy use of the
buffer
in a wide range of contexts, including comparisons and conversions, while maintaining security.
Example:
int
Public Functions
buffer (const const_buffer< const_key, Hash, const_size > &src) noexcept
Constructs a buffer
object from a const_buffer
.
Definition
libantispy::buffer::buffer
Detailed Description
This constructor initializes the buffer
object using the contents of a const_buffer
, which is a constant encrypted buffer. The encrypted data is directly copied into the buffer
, and the encryption key and hash value are also copied, ensuring that the data remains secure.
Details:
Key and Hash Initialization :
The constructor initializes the
key_
member with the provided constant key (const_key
). This key is used for encryption and decryption operations.The
hash_
member is initialized with the hash value retrieved from theconst_buffer
usingsrc.get_hash()
. This hash is crucial for verifying the integrity of the data.
Data Copying :
The constructor copies the encrypted data from the
const_buffer
into thebuffer
. This is done by creating a temporarystd::vector<char>
with the data range fromsrc.raw_data()->data
tosrc.raw_data()->data + const_size
.The temporary vector is then swapped with the
buffer
's internal vector usingswap(*this)
. This ensures efficient data transfer with minimal overhead.
Noexcept Guarantee :
- The constructor is marked as
noexcept
, which indicates that it guarantees not to throw any exceptions. This makes it suitable for use in performance-critical or exception-sensitive code.
- The constructor is marked as
Example:
int
Template Parameters
const_key
: The encryption key used by theconst_buffer
.const_size
: The size of the data in theconst_buffer
.Hash
: The hash value associated with theconst_buffer
.
Parameters
src
: The sourceconst_buffer
containing the encrypted data to be copied into thebuffer
.
buffer (const ::std::string &src) noexcept
Constructs a buffer
object from a std::string
.
Definition
libantispy::buffer::buffer
Detailed Description
This constructor initializes the buffer
object using the contents of a provided std::string
. The string data is encrypted and stored in the buffer, and a unique key and hash value are generated for the encryption process.
Details:
Encryption :
The constructor generates a random encryption key using
detail::key<char>::make_non_zero_random()
. This key is used to encode the string data.The key is ensured to be non-zero, enhancing security by avoiding trivial encryption (e.g., XOR with zero has no effect).
Hashing :
The fnv1a hashing algorithm is applied to the string to compute a unique hash value. This hash is used for quick comparisons and to verify the integrity of the data.
The hash is calculated over the entire string, including the null terminator (
src.length() + 1
), ensuring that the full content of the string is considered.
Encoding :
The string is encrypted by the
detail::buffer::encode
function, which processes the string using the generated key.The resulting encrypted data is stored in the buffer, replacing any previous contents of the buffer.
Noexcept Guarantee :
- The constructor is marked as
noexcept
, indicating that it guarantees not to throw any exceptions. This is important for performance and reliability, especially in low-level or performance-critical code.
- The constructor is marked as
Example:
int
Parameters
src
: The sourcestd::string
containing the data to be encrypted and stored in the buffer.
buffer (const buffer &src)=default
Copy constructor for the buffer
class.
Definition
libantispy::buffer::buffer
Detailed Description
This constructor creates a new buffer
object as a copy of an existing buffer
object. The default copy constructor is used, which automatically copies all the members of the src
object to the new object.
Details:
Functionality :
The copy constructor initializes the new
buffer
object by performing a member-wise copy of thesrc
buffer.This means that the contents of the underlying
std::vector<char>
, along with thekey_
andhash_
members, are duplicated in the new object.
Usage :
Safety :
Post-Conditions :
After the copy operation, the new
buffer
object (this
) contains the same data as thesrc
object.The original
src
buffer remains unchanged and independent of the new copy.
Example:
int
Parameters
src
: The sourcebuffer
object to copy from.
buffer (const buffer &&src) noexcept
Move constructor for the buffer
class.
Definition
libantispy::buffer::buffer
Detailed Description
This constructor initializes a buffer
object by transferring the ownership of resources from another buffer
object. The move constructor ensures that the current object takes over the data, encryption key, and hash from the src
buffer, leaving src
in a valid but unspecified state.
Details:
Functionality :
The move constructor is used to efficiently transfer the contents of one
buffer
object (src
) to another, without the need to perform a deep copy.This operation is typically faster than copying because it involves only pointer manipulations rather than duplicating the actual data.
Logic :
Key and Hash Transfer :
- The constructor initializes the
key_
andhash_
members of the current object by directly assigning them from thesrc
object. This step copies the encryption key and hash value fromsrc
to the currentbuffer
.
- The constructor initializes the
Resource Transfer :
The
std::vector<char>().swap(*this);
call clears the current vector by swapping it with an empty vector.The
static_cast<::std::vector<char>>(src).swap(*this);
line then swaps the data from thesrc
vector into the current object. This operation effectively transfers the data fromsrc
tothis
.The
static_cast
is used to treatsrc
as anstd::vector<char>
, enabling the swap operation.The
NOLINT(cppcoreguidelines-slicing)
comment suppresses a lint warning about potential slicing whensrc
is cast tostd::vector<char>
. Slicing does not occur in this context because the move operation correctly handles all relevant members.
Usage :
Safety :
- The constructor is marked
noexcept
, indicating that it does not throw exceptions. This is important for performance-sensitive applications, as it allows safe usage in contexts where exception handling might be costly or undesired.
- The constructor is marked
Post-Conditions :
- After the move, the
src
buffer is left in a valid but unspecified state. It can be destructed or assigned a new value, but its contents should not be relied upon.
- After the move, the
Example:
int
Parameters
src
: The sourcebuffer
object to move from.
buffer & operator= (const buffer &other)
Copy assignment operator for the buffer
class.
Definition
buffer &
Detailed Description
This operator allows for the assignment of one buffer
object to another, effectively duplicating the contents and state of the other
buffer into the current buffer ( this
). The copy assignment operator is crucial in scenarios where a deep copy of the buffer's data and encryption key is needed.
Details:
Functionality :
- The copy assignment operator duplicates the contents of the
other
buffer and assigns them to the current buffer (this
), ensuring that the current buffer becomes an exact copy of theother
.
- The copy assignment operator duplicates the contents of the
Logic :
Self-Assignment Check :
The first step is to check whether the current object (
this
) is the same as theother
object.If they are the same, the function simply returns
*this
, as no operation is needed.
Copy Data :
If the objects are different, the contents of the
other
buffer are copied into the current buffer.This is done by creating a temporary
std::vector<char>
initialized with the range of elements from theother
buffer and then swapping it with the current buffer.This ensures that the current buffer is updated with the new data in an exception-safe manner.
Copy Key :
The encryption key (
key_
) from theother
buffer is assigned to the current buffer'skey_
.This step is essential to ensure that the encryption/decryption process can be performed correctly on the copied data.
Return Current Object :
- The function returns a reference to the current buffer (
*this
), allowing for method chaining and further operations.
- The function returns a reference to the current buffer (
Usage :
Safety :
The self-assignment check ensures that the operator behaves correctly even when the same object is assigned to itself, preventing unnecessary operations and potential issues.
The use of the
swap
method provides strong exception safety, as it ensures that the current buffer's state is only updated if all operations succeed.
Example:
int
Parameters
other
: The sourcebuffer
object to copy from. The contents and encryption key ofother
are duplicated.
Returns
A reference to the current buffer
object ( *this
), now containing a copy of other
.
buffer & operator= (buffer &&other) noexcept
Move assignment operator for the buffer
class.
Definition
buffer &
Detailed Description
This operator allows for efficient transfer of resources from one buffer
object to another, leveraging move semantics to avoid unnecessary copying. The move assignment operator is crucial in scenarios where performance is critical, and ownership of the buffer's resources needs to be transferred rather than duplicated.
Details:
Functionality :
- The move assignment operator transfers the contents and resources from the
other
buffer to the current buffer (this
), effectively moving the data and invalidating theother
buffer's state.
- The move assignment operator transfers the contents and resources from the
Logic :
Self-Assignment Check :
The first step is to check whether the current object (
this
) is the same as theother
object.If they are the same, the function simply returns
*this
, as no operation is needed.
Clear Current Buffer :
If the objects are different, the current buffer is cleared by swapping it with an empty
std::vector<char>
.This ensures that the current buffer's contents are released before the move.
Move Data :
The
swap
function is used to transfer the contents of theother
buffer to the current buffer.This operation is efficient because it involves swapping pointers rather than copying data.
Transfer Key :
The encryption key (
key_
) from theother
buffer is assigned to the current buffer'skey_
.This step ensures that the encryption/decryption process can continue seamlessly with the moved data.
Return Current Object :
- The function returns a reference to the current buffer (
*this
), allowing for method chaining and further operations.
- The function returns a reference to the current buffer (
Usage :
Safety :
The operator is marked as
noexcept
, ensuring that it does not throw exceptions. This is important for maintaining strong exception safety guarantees, particularly in environments where exception handling must be avoided.The self-assignment check ensures that the operator behaves correctly even when the same object is assigned to itself.
Example:
int
Parameters
other
: The sourcebuffer
object to move from. After the move,other
is left in a valid but unspecified state.
Returns
A reference to the current buffer
object ( *this
), now containing the moved resources.
char operator[] (const size_t index) const noexcept
Index operator for accessing elements of the encrypted buffer.
Definition
char
Detailed Description
The operator[]
function provides access to elements within the encrypted buffer by decrypting them on-the-fly. This operator is crucial for retrieving individual characters from the buffer while ensuring that the data remains secure during access.
Details:
Logic :
The function uses the given
index
to access the corresponding encrypted element in the buffer.The encrypted value is then decrypted using the
encryptor<char>::decrypt
method, which requires:The encrypted character retrieved from the buffer.
The encryption key (
key_
) that was used to encrypt the buffer.The
index
itself as an additional parameter to the decryption function.
The result is the decrypted character at the specified index.
Usage :
This operator allows for secure access to the buffer's contents, especially when you need to read individual characters in a context where the data must remain protected, such as in secure communications or encrypted file handling.
It can be used in loops, conditionals, or any context where indexed access to the buffer's content is required.
Performance :
- While the operator ensures data security by decrypting each accessed element, the decryption process introduces some overhead compared to direct access in a non-encrypted context. However, this is a necessary trade-off for maintaining the confidentiality of the data.
Safety :
The operator is marked as
noexcept
, indicating that it does not throw exceptions, which is beneficial in scenarios where exception safety is a concern.The
WIN_SUPRESS_4244
macro is used to suppress specific compiler warnings related to potential data loss in type conversions, ensuring clean compilation without warnings while being mindful of data integrity.
Example:
int
Parameters
index
: The index of the element to access.
Returns
The decrypted character at the specified index.
size_t size () const
Returns the size of the buffer.
Definition
size_t
Detailed Description
The size()
function provides the current number of elements in the buffer. It is an essential function for determining the length or capacity of the buffer in terms of the number of characters it holds.
Details:
Logic :
The function internally calls the
size()
method of the underlyingstd::vector<char>
base class.This method returns the number of elements currently stored in the buffer, which reflects the actual size of the data.
Usage :
This function is useful when you need to determine the length of the buffer, especially when processing or manipulating its contents.
The size returned by this function can be used to iterate over the buffer, allocate memory, or perform size-related checks.
Performance :
- Since this function directly returns the size of the vector, it is typically a constant-time operation, meaning it has O(1) complexity.
Example:
int
Returns
The number of elements (characters) in the buffer.
bool empty () const noexcept
Checks whether the buffer is empty.
Definition
bool
Detailed Description
The empty()
function determines if the current buffer contains any data. It provides a convenient way to check if the buffer is empty, i.e., if it has a size of zero.
Details:
Logic :
The function internally calls the
size()
method to retrieve the size of the buffer.It then compares the size to zero. If the size is zero, the buffer is considered empty, and the function returns
true
.If the size is greater than zero, the function returns
false
, indicating that the buffer contains data.
Noexcept :
- The function is marked as
noexcept
, indicating that it is guaranteed not to throw any exceptions. This makes it safe to use in contexts where exception safety is critical.
- The function is marked as
Use Case :
This function is particularly useful in conditional statements where operations depend on whether the buffer is empty or not.
It simplifies code that needs to handle empty buffers differently from non-empty ones.
Example:
int
Returns
true
if the buffer is empty (i.e., size is zero); false
otherwise.
void append (const buffer &cs)
Appends the contents of another buffer to this buffer.
Definition
void
Detailed Description
The append()
function allows the current buffer to append the contents of another buffer
object to itself. This is useful when you want to combine or concatenate two buffers into a single one, effectively merging their contents.
Details:
Process :
The function first converts the current buffer (
*this
) into astd::string
by using thestatic_cast
operator.It then converts the provided buffer (
cs
) into astd::string
as well.The contents of the
cs
buffer are appended to thestr
string, which now contains the combined data from both buffers.Finally, the
new (this) string(str);
statement reconstructs the current buffer with the new combined data, effectively replacing its contents with the concatenated result.
In-Place Modification :
The
new (this) string(str);
syntax performs in-place reconstruction of the current buffer object. This approach ensures that the buffer's contents are updated without requiring the creation of a new buffer object.This method takes advantage of placement new to reconstruct the buffer with the updated string content.
Performance :
- The operation of appending a buffer involves memory allocation and copying, especially since the buffers are first converted to
std::string
objects. While this approach is straightforward, it might not be the most efficient for large buffers.
- The operation of appending a buffer involves memory allocation and copying, especially since the buffers are first converted to
Use Case :
This function is particularly useful when managing dynamic data, where buffers need to be extended or combined during runtime.
It simplifies buffer management by allowing direct concatenation without requiring external manipulation of the buffer's internal data.
Parameters
cs
: Thebuffer
object whose contents are to be appended to the current buffer.
size_t get_hash () const
Retrieves the hash value of the buffer.
Definition
size_t
Detailed Description
The get_hash()
function returns the hash value that was calculated and stored within the buffer object. This hash is typically used to verify the integrity of the buffer's contents or to uniquely identify the buffer's data.
Details:
Hash Calculation :
- The
hash_
value is computed when the buffer is constructed, either during initialization with aconst_buffer
or astd::string
. The hash typically uses a hashing algorithm like FNV-1a or another method provided by the implementation.
- The
Purpose :
- The hash is useful for quickly comparing buffers, checking data integrity, or for use in cryptographic operations where a consistent, reproducible identifier for the data is required.
Performance :
- This function is marked with
ANTISPY_INLINE
to suggest inlining by the compiler, which can help reduce the function call overhead when accessing the hash value. However, the actual inlining is subject to the compiler's optimization decisions.
- This function is marked with
Return Value :
- The function returns the stored hash value, which is a
size_t
type. This value can be used for any subsequent operations where the buffer's hash is needed.
- The function returns the stored hash value, which is a
Returns
The hash value of the buffer, stored as a size_t
.
~buffer () noexcept
Destructor for the buffer
class.
Definition
Detailed Description
The destructor is responsible for securely cleaning up the buffer
object when it is no longer needed. It ensures that any sensitive data stored in the buffer is securely erased to prevent unauthorized access or data leakage.
Details:
Key and Hash Clearing :
- The
key_
andhash_
member variables, which are used for encryption and integrity checking, are securely cleared by XORing them with themselves. This operation effectively sets their values to zero, ensuring that these sensitive pieces of information are not left in memory after the buffer is destroyed.
- The
Data Erasure :
The buffer's content is securely erased using
memset
, which sets the memory occupied by the buffer's data to zero. Thememset
function is applied to the entire range of the buffer from the beginning to the end.This step is crucial for preventing residual data from being recovered from memory after the buffer is destroyed.
Memory Deallocation :
- The memory allocated for the buffer is deallocated by swapping the current
std::vector<char>
with an empty vector. This ensures that the memory used by the buffer is released back to the system, preventing memory leaks.
- The memory allocated for the buffer is deallocated by swapping the current
Security Considerations :
The destructor implements best practices for securely handling and destroying sensitive data, ensuring that encryption keys, hashes, and buffer content do not remain in memory after the buffer is destroyed.
The use of XOR and
memset
helps protect against potential memory dumps or forensic analysis that could otherwise expose sensitive information.
Exception Safety:
- The destructor is marked
noexcept
, indicating that it does not throw any exceptions. This ensures that the cleanup process is guaranteed to complete without interruption, even in environments where exceptions are not allowed.
Private Properties
size_t key_
The encryption key used for securing the buffer's content.
Definition
size_t
Detailed Description
The key_
member variable holds the encryption key used for encrypting and decrypting the data stored within the buffer
class. This key is a crucial part of the buffer's security mechanism, as it ensures that the data is securely transformed between its encrypted and decrypted states.
Details:
Type :
size_t
Usage : The
key_
is used in conjunction with the buffer's contents to perform encryption and decryption operations. When data is written to the buffer, it is encrypted using this key. When data is read from the buffer, it is decrypted using the same key.Initialization :
**In Constructor from
std::string
** : Thekey_
is initialized to a randomly generated non-zero value using thedetail::key<char>::make_non_zero_random()
method. This ensures that the key is unique and unpredictable for each instance of the buffer.**In Constructor from
const_buffer
**: Thekey_
is set to the key used in theconst_buffer
, ensuring consistency between the compile-time encrypted data and its runtime representation.
Security :
The
key_
is XORed with itself during the destruction of the buffer to securely erase it.The encryption key is never directly exposed; it is used internally to protect the buffer's data.
Scope : This member is private, meaning it is only accessible within the
buffer
class.Context : The key is used in tandem with the buffer's content and a hashing mechanism (
hash_
) to ensure the integrity and confidentiality of the data.
Example Usage:
int
In the example above, the key_
would be used to encrypt "Sensitive Information" when constructing the buffer
, and then used again to decrypt the data when accessing it.
size_t hash_
Hash value representing the buffer's content.
Definition
size_t
Detailed Description
The hash_
member variable stores a hash value that uniquely represents the content of the buffer. This hash is used for verifying the integrity of the buffer's content, ensuring that data has not been altered or corrupted.
Details:
Type :
size_t
Purpose :
The
hash_
is crucial for comparingbuffer
objects, as it allows quick equality checks by comparing hash values rather than the entire buffer content.It is also used for validating the integrity of the buffer, ensuring that the content matches the expected value.
Initialization :
**In Constructor from
std::string
** : Thehash_
is computed using a hash function likelibantispy::fnv1a<char>()
, which generates a unique hash for the string content, ensuring that even small changes in the content result in a different hash value.**In Constructor from
const_buffer
**: Thehash_
is set to the precomputed hash value from the const_buffer , ensuring consistency between the original and the current buffer.
Usage :
The hash is primarily used in comparison operators (
==
,!=
, etc.) to quickly determine if two buffers are identical without needing to compare each byte of their content.It is also used in scenarios where the buffer needs to be validated or stored in hash-based containers.
Security Considerations :
The hash value provides a layer of security by ensuring that any tampering with the buffer content can be detected through a mismatch in the hash value.
It is also used to ensure that encrypted buffers are correctly processed and matched with their original content.
Friends
bool operator== (const buffer &lhs, const buffer &rhs) noexcept
Equality operator for comparing two buffer
objects.
Definition
bool
Detailed Description
The operator==
function is used to compare two buffer
objects to determine if they are equal. This operator is vital in scenarios where checking the equivalence of encrypted buffers is required, such as in data integrity verification or during cryptographic operations.
Details:
Logic :
The function compares the hash values of the two buffers,
lhs
andrhs
.If the hash values are identical, the function returns
true
, indicating that the buffers are considered equal.If the hash values differ, the function returns
false
, indicating that the buffers are not equal.This comparison method relies on the assumption that the hash values accurately represent the content of the buffers.
Since hash values are generally much smaller in size compared to the buffer's content, this comparison is efficient.
Usage :
This operator is essential in conditions where it is necessary to confirm that two buffers are identical, such as in cryptographic checks, input validation, or data synchronization processes.
It can be used in control flow statements, assertions, or anywhere an equality check between buffers is required.
Performance :
The performance of this operator is expected to be very efficient since it only compares two size_t values (the hash values).
This makes it significantly faster than comparing the entire content of the buffers directly.
Example:
int
Parameters
lhs
: The left-hand sidebuffer
object to compare.rhs
: The right-hand sidebuffer
object to compare.
Returns
true
if lhs
is equal to rhs
; false
if they are not equal.
bool operator!= (const buffer &lhs, const buffer &rhs) noexcept
Inequality operator for comparing two buffer
objects.
Definition
bool
Detailed Description
The operator!=
function is used to compare two buffer
objects to determine if they are not equal. This operator plays a crucial role in scenarios where it is important to identify differences between encrypted buffers, such as validating data integrity or detecting changes.
Details:
Logic :
The function leverages the equality operator
operator==
to check if the two buffers are equal.If the buffers are equal,
operator==
returnstrue
, and this function negates that result, returningfalse
.Conversely, if the buffers are not equal,
operator==
returnsfalse
, and this function negates that result, returningtrue
.The use of
operator==
ensures that the comparison is based on the underlying encrypted data and its hash value.
Usage :
This operator is essential in conditions where it is necessary to confirm that two buffers are not the same, such as in data validation, input comparison, or during decryption checks.
It can be used in control flow statements, assertions, or anywhere an inequality check between buffers is required.
Performance :
The performance of this operator is directly tied to the
operator==
function, as it is built upon it.Since the comparison is generally based on hash values and potentially the buffer's content, it is expected to be efficient.
Example:
int
Parameters
Returns
true
if lhs
is not equal to rhs
; false
if they are equal.
bool operator< (const buffer &lhs, const buffer &rhs) noexcept
Less-than operator for comparing two buffer
objects.
Definition
bool
Detailed Description
The operator<
function is used to compare two buffer
objects to determine if one is strictly less than the other. This operator is essential in situations where you need to establish an order between two encrypted buffers, such as when sorting or implementing data structures that require ordered elements.
Details:
Logic :
The function first checks if the two buffers are not equal by using the
operator!=
function.If the buffers are not equal, the function proceeds to convert both buffers to
std::string
objects by invoking theoperator std::string()
function.The comparison is then performed on the decrypted string representations of the buffers.
If the string representation of the left-hand side (
lhs
) buffer is less than that of the right-hand side (rhs
) buffer, the function returnstrue
; otherwise, it returnsfalse
.If the buffers are equal, the function returns
false
, as neither is less than the other.
Usage :
This operator is commonly used in sorting algorithms, ordered containers (e.g.,
std::set
,std::map
), and other contexts where determining the order between buffers is necessary.It can also be used in conditional checks to enforce specific ordering constraints between encrypted data buffers.
Performance :
The performance of this operator depends on the cost of the string conversion and the string comparison.
Converting the buffer to a string involves decrypting the buffer's contents, which can be an O(n) operation, where n is the length of the string.
The string comparison itself is also O(n), making the overall comparison O(n) in the worst case.
Example:
int
Parameters
Returns
true
if lhs
is strictly less than rhs
; false
otherwise.
bool operator<= (const buffer &lhs, const buffer &rhs) noexcept
Less-than-or-equal-to operator for comparing two buffer
objects.
Definition
bool
Detailed Description
The operator<=
function is used to compare two buffer
objects to determine if one is less than or equal to the other. This operator is especially useful in scenarios where you need to verify if the contents of one encrypted buffer are either less than or equal to another, which can be relevant in sorting or conditional checks.
Details:
Logic :
The function first checks if the two buffers are equal by using the
operator==
function.If the buffers are equal, the function immediately returns
true
.If the buffers are not equal, it converts both buffers to
std::string
objects by invoking theoperator std::string()
function.The comparison is then made on the decrypted string representations of the buffers.
If the left-hand side (
lhs
) buffer is less than the right-hand side (rhs
) buffer in terms of their string values, the function returnstrue
; otherwise, it returnsfalse
.
Usage :
This operator is useful when you need to determine whether one buffer is less than or equal to another.
It can be used in sorting algorithms, conditional checks, and other scenarios where buffer ordering is important.
Performance :
The performance of this operator depends on the complexity of the comparison and the conversion of the buffers to strings.
The string comparison involves a character-by-character comparison of the decrypted contents, which can be an O(n) operation, where n is the length of the strings.
Example:
int
Parameters
Returns
true
if lhs
is less than or equal to rhs
; false
otherwise.
bool operator> (const buffer &lhs, const buffer &rhs) noexcept
Greater-than operator for comparing two buffer
objects.
Definition
bool
Detailed Description
The operator>
function is used to compare two buffer
objects to determine if one is greater than the other. This operator is particularly useful in scenarios where you need to compare the contents of two encrypted buffers to establish their relative order or precedence.
Details:
Logic :
The function first checks if the two buffers are not equal by using the
operator!=
function.If the buffers are not equal, it then converts both buffers to
std::string
objects by invoking theoperator std::string()
function.The comparison is made on the decrypted string representations of the buffers.
If the left-hand side (
lhs
) buffer is greater than the right-hand side (rhs
) buffer in terms of their string values, the function returnstrue
; otherwise, it returnsfalse
.
Usage :
This operator is useful when you need to determine whether one buffer is strictly greater than another.
It can be used in sorting algorithms, conditional checks, and other scenarios where buffer ordering is important.
Performance :
The performance of this operator depends on the complexity of the comparison and the conversion of the buffers to strings.
The string comparison involves a character-by-character comparison of the decrypted contents, which can be an O(n) operation, where n is the length of the strings.
Example:
int
Parameters
Returns
true
if lhs
is greater than rhs
; false
otherwise.
bool operator>= (const buffer &lhs, const buffer &rhs) noexcept
Greater-than-or-equal-to operator for comparing two buffer
objects.
Definition
bool
Detailed Description
The operator>=
function compares two buffer
objects to determine if one is greater than or equal to the other. This operator is particularly useful when sorting or comparing buffers based on their encrypted content.
Details:
Logic :
The function first checks if the two buffers are equal by using the
operator==
function.If the buffers are not equal, it then converts both buffers to
std::string
objects by invoking theoperator std::string()
function.The comparison is made on the decrypted string representations of the buffers.
If the left-hand side (
lhs
) buffer is greater than or equal to the right-hand side (rhs
) buffer in terms of their string values, the function returnstrue
; otherwise, it returnsfalse
.
Usage :
This operator is useful when you need to compare two buffers to determine their relative ordering or check if one buffer is at least as large as another.
It is often used in sorting algorithms, conditional checks, and other scenarios where buffer comparison is required.
Performance :
The performance of this operator depends on the complexity of the comparison and the conversion of the buffers to strings.
The string comparison involves a character-by-character comparison of the decrypted contents, which can be an O(n) operation, where n is the length of the strings.
Example:
int
Parameters
Returns
true
if lhs
is greater than or equal to rhs
; false
otherwise.