1
0
This commit is contained in:
Sytnyk Yehor
2025-05-15 13:11:19 +03:00
parent 4c01c2fe11
commit 9fb9f66f27
26 changed files with 730 additions and 0 deletions

View File

@ -0,0 +1,37 @@
#pragma once
#ifndef CACHE_LRU_HPP
#define CACHE_LRU_HPP
#include <cstdint>
#include <list>
#include <unordered_map>
#include <vector>
namespace cache {
class lru {
public:
lru(int32_t line_count, int32_t associativity);
~lru() = default;
bool access(uint64_t address);
private:
struct block_entry {
uint64_t tag;
};
int32_t _lines;
int32_t _assoc;
std::vector<std::list<block_entry>> _cache_lines;
std::vector<std::unordered_map<uint64_t, std::list<block_entry>::iterator>>
_lookup;
};
} // namespace cache
#include "../src/cache.cpp"
#endif

View File

@ -0,0 +1,84 @@
#pragma once
#ifndef SEC_LOCKED_BUFFER_HPP
#define SEC_LOCKED_BUFFER_HPP
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <sys/mman.h>
#include <unistd.h>
namespace sec {
template <typename T, std::size_t N> class locked_buffer {
public:
locked_buffer() {
this->size_ = N * sizeof(T);
this->ptr_ = static_cast<T *>(std::malloc(this->size_));
if (!this->ptr_)
throw std::bad_alloc();
if (mlock(this->ptr_, this->size_) != 0) {
std::free(this->ptr_);
throw std::runtime_error("mlock failed");
}
}
~locked_buffer() noexcept {
cleanse();
if (this->ptr_) {
munlock(this->ptr_, this->size_);
std::free(this->ptr_);
}
}
locked_buffer(const locked_buffer &) = delete;
locked_buffer &operator=(const locked_buffer &) = delete;
locked_buffer(locked_buffer &&other) noexcept
: ptr_(other.ptr_), size_(other.size_) {
other.ptr_ = nullptr;
other.size_ = 0;
}
locked_buffer &operator=(locked_buffer &&other) noexcept {
if (this != &other) {
cleanse();
if (this->ptr_) {
munlock(this->ptr_, this->size_);
std::free(this->ptr_);
}
this->ptr_ = other.ptr_;
this->size_ = other.size_;
other.ptr_ = nullptr;
other.size_ = 0;
}
return *this;
}
T *data() noexcept { return this->ptr_; }
const T *data() const noexcept { return this->ptr_; }
std::size_t size() const noexcept { return N; }
T &operator[](std::size_t i) { return this->ptr_[i]; }
const T &operator[](std::size_t i) const { return this->ptr_[i]; }
T *begin() noexcept { return this->ptr_; }
T *end() noexcept { return this->ptr_ + N; }
const T *begin() const noexcept { return this->ptr_; }
const T *end() const noexcept { return this->ptr_ + N; }
private:
void cleanse() noexcept {
if (this->ptr_) {
std::memset(this->ptr_, 0, this->size_);
}
}
T *ptr_{nullptr};
std::size_t size_{0};
};
} // namespace sec
#endif

View File

@ -0,0 +1,44 @@
#pragma once
#ifndef PAGE_REPLACEMENT_HPP
#define PAGE_REPLACEMENT_HPP
#include <cstdint>
#include <vector>
namespace paging {
class replacer {
public:
explicit replacer(int32_t frame_count) : _frames(frame_count) {}
virtual ~replacer() = default;
virtual int32_t run(const std::vector<int32_t> &pages) = 0;
protected:
int32_t _frames;
};
class optimal_replacer : public replacer {
public:
using replacer::replacer;
int32_t run(const std::vector<int32_t> &pages) override;
};
class clock_replacer : public replacer {
public:
using replacer::replacer;
int32_t run(const std::vector<int32_t> &pages) override;
};
class lru_replacer : public replacer {
public:
using replacer::replacer;
int32_t run(const std::vector<int32_t> &pages) override;
};
} // namespace paging
#include "../src/paging.cpp"
#endif

View File

@ -0,0 +1,68 @@
#pragma once
#ifndef PASSWORD_HPP
#define PASSWORD_HPP
#include <cstdint>
#include <string>
#include <string_view>
#include "locked_buffer.hpp"
namespace sec {
enum class strength_t : uint8_t {
very_weak = 0,
weak,
moderate,
strong,
very_strong
};
class password_hash;
class password {
public:
explicit password(std::string pass);
std::string_view view() const noexcept;
static sec::password from_console();
static sec::password from_string(const std::string &pass);
sec::password_hash to_hash() const;
private:
sec::locked_buffer<char, 128> _buf;
std::size_t _len{};
};
class password_hash {
public:
explicit password_hash(std::string hash);
const std::string &get() const noexcept;
bool verify(const sec::password &pwd) const;
private:
std::string _hash;
};
class manager {
public:
static bool has_lower(const sec::password &pwd);
static bool has_upper(const sec::password &pwd);
static bool has_digit(const sec::password &pwd);
static bool has_spec_symbol(const sec::password &pwd);
static std::string hash_password(const sec::password &pwd);
static sec::strength_t evaluate_strength(const sec::password &pwd);
static uint64_t time_to_crack(const sec::password &pwd);
static std::string format_duration(uint64_t seconds);
};
} // namespace sec
#include "../src/password.cpp"
#endif