Add GetBit() method to Block
This commit is contained in:
parent
db0add6e6e
commit
e552e1a6f8
@ -123,6 +123,9 @@ namespace Leonetienne::GCrypt {
|
||||
//! Will zero all data
|
||||
void Reset();
|
||||
|
||||
//! Will return the state of any given bit
|
||||
[[nodiscard]] bool GetBit(const std::size_t index) const;
|
||||
|
||||
//! Returns 32-bit chunks of data, indexed by matrix coordinates (0-3)
|
||||
[[nodiscard]] std::uint32_t& Get(const std::uint8_t row, const std::uint8_t column);
|
||||
//! Returns 32-bit chunks of data, indexed by matrix coordinates (0-3)
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "GCrypt/Block.h"
|
||||
#include <iostream>
|
||||
#include "GCrypt/Config.h"
|
||||
#include <sstream>
|
||||
#include <bitset>
|
||||
@ -447,6 +448,16 @@ namespace Leonetienne::GCrypt {
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool Block::GetBit(const std::size_t index) const {
|
||||
// Fetch index of integer the bit is located in
|
||||
const std::size_t intIndex = index / CHUNK_SIZE_BITS;
|
||||
|
||||
// Fetch bit index relative to that int
|
||||
const std::size_t relBitIndex = index - (intIndex * CHUNK_SIZE_BITS);
|
||||
|
||||
return data[intIndex] & (1 << (CHUNK_SIZE_BITS - relBitIndex - 1));
|
||||
}
|
||||
|
||||
std::uint32_t& Block::Get(const std::uint8_t row, const std::uint8_t column){
|
||||
return data[MAT_INDEX(row, column)];
|
||||
}
|
||||
|
@ -633,3 +633,18 @@ TEST_CASE(__FILE__"/multiple-combined-shifts-and-additions-can-be-undone", "[Blo
|
||||
REQUIRE(a == initial_a);
|
||||
}
|
||||
|
||||
// Tests that the get-bit method works
|
||||
TEST_CASE(__FILE__"/get-bit", "[Block]") {
|
||||
// Setup
|
||||
Block a = Key::FromPassword("Halleluja");
|
||||
|
||||
// Exercise
|
||||
std::stringstream ss;
|
||||
for (std::size_t i = 0; i < 512; i++) {
|
||||
ss << a.GetBit(i);
|
||||
}
|
||||
|
||||
// Verify
|
||||
REQUIRE(ss.str() == a.ToString());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user