refactor: make linter happy
also add a few unused coprocessor instructions Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
@@ -3,54 +3,55 @@
|
||||
#include <fmt/ostream.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace logger {
|
||||
inline std::ostream& stream = std::clog;
|
||||
using fmt::print;
|
||||
using std::clog;
|
||||
|
||||
namespace logger {
|
||||
namespace ansi {
|
||||
static constexpr char RED[] = "\033[31m";
|
||||
static constexpr char YELLOW[] = "\033[33m";
|
||||
static constexpr char MAGENTA[] = "\033[35m";
|
||||
static constexpr char WHITE[] = "\033[37m";
|
||||
static constexpr char BOLD[] = "\033[1m";
|
||||
static constexpr char RESET[] = "\033[0m";
|
||||
static constexpr std::string_view RED = "\033[31m";
|
||||
static constexpr std::string_view YELLOW = "\033[33m";
|
||||
static constexpr std::string_view MAGENTA = "\033[35m";
|
||||
static constexpr std::string_view WHITE = "\033[37m";
|
||||
static constexpr std::string_view BOLD = "\033[1m";
|
||||
static constexpr std::string_view RESET = "\033[0m";
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void
|
||||
log_raw(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
fmt::println(stream, fmt, std::forward<Args>(args)...);
|
||||
fmt::println(clog, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void
|
||||
log_debug(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
fmt::print(stream, "{}{}[DEBUG] ", ansi::MAGENTA, ansi::BOLD);
|
||||
print(clog, "{}{}[DEBUG] ", ansi::MAGENTA, ansi::BOLD);
|
||||
log_raw(fmt, std::forward<Args>(args)...);
|
||||
fmt::print(stream, ansi::RESET);
|
||||
print(clog, ansi::RESET);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void
|
||||
log_info(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
fmt::print(stream, "{}[INFO] ", ansi::WHITE);
|
||||
print(clog, "{}[INFO] ", ansi::WHITE);
|
||||
log_raw(fmt, std::forward<Args>(args)...);
|
||||
fmt::print(stream, ansi::RESET);
|
||||
print(clog, ansi::RESET);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void
|
||||
log_warn(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
fmt::print(stream, "{}[WARN] ", ansi::YELLOW);
|
||||
print(clog, "{}[WARN] ", ansi::YELLOW);
|
||||
log_raw(fmt, std::forward<Args>(args)...);
|
||||
fmt::print(stream, ansi::RESET);
|
||||
print(clog, ansi::RESET);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void
|
||||
log_error(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
fmt::print(stream, "{}{}[ERROR] ", ansi::RED, ansi::BOLD);
|
||||
print(clog, "{}{}[ERROR] ", ansi::RED, ansi::BOLD);
|
||||
log_raw(fmt, std::forward<Args>(args)...);
|
||||
fmt::print(stream, ansi::RESET);
|
||||
print(clog, ansi::RESET);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,3 +0,0 @@
|
||||
lib_sources += files(
|
||||
'utils.cc'
|
||||
)
|
@@ -1,113 +0,0 @@
|
||||
#include "utils.hh"
|
||||
#include <bit>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
namespace crypto {
|
||||
|
||||
using std::rotr;
|
||||
|
||||
std::string
|
||||
sha256(const uint8_t* data, const size_t size) {
|
||||
// Assuming 1 byte = 8 bits
|
||||
std::stringstream string;
|
||||
size_t k = 512 - (size * 8 + 65) % 512;
|
||||
size_t L = size + (65 + k) / 8;
|
||||
size_t i, j;
|
||||
bool c = 0;
|
||||
|
||||
static constexpr uint32_t K[64] = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
||||
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
||||
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
||||
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
||||
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
||||
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
||||
};
|
||||
|
||||
uint32_t h[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
||||
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
|
||||
|
||||
for (i = 0; i < L; i += 64) {
|
||||
size_t n = (size > i ? size - i : 0);
|
||||
uint32_t h0[8];
|
||||
uint32_t w[64] = { 0 };
|
||||
|
||||
if (n == 64)
|
||||
c = 1;
|
||||
|
||||
if (n >= 64) {
|
||||
for (j = 0; j < 16; j++) {
|
||||
w[j] = data[i + j * 4] << 24 | data[i + j * 4 + 1] << 16 |
|
||||
data[i + j * 4 + 2] << 8 | data[i + j * 4 + 3];
|
||||
}
|
||||
} else {
|
||||
uint8_t cur[64] = { 0 };
|
||||
|
||||
if (n) {
|
||||
std::copy(data + i, data + size, cur);
|
||||
cur[n] = 0x80;
|
||||
} else if (c) {
|
||||
cur[n] = 0x80;
|
||||
}
|
||||
|
||||
if (n < 54) {
|
||||
for (j = 56; j < 64; j++) {
|
||||
cur[j] = (size * 8 >> ((63 - j) * 8)) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
for (j = 0; j < 16; j++) {
|
||||
w[j] = cur[j * 4] << 24 | cur[j * 4 + 1] << 16 |
|
||||
cur[j * 4 + 2] << 8 | cur[j * 4 + 3];
|
||||
}
|
||||
}
|
||||
|
||||
for (j = 16; j < 64; j++) {
|
||||
uint32_t s0 =
|
||||
rotr(w[j - 15], 7) ^ rotr(w[j - 15], 18) ^ (w[j - 15] >> 3);
|
||||
uint32_t s1 =
|
||||
rotr(w[j - 2], 17) ^ rotr(w[j - 2], 19) ^ (w[j - 2] >> 10);
|
||||
|
||||
w[j] = w[j - 16] + w[j - 7] + s0 + s1;
|
||||
}
|
||||
|
||||
std::copy(h, h + 8, h0);
|
||||
|
||||
for (j = 0; j < 64; j++) {
|
||||
uint32_t s1 = rotr(h0[4], 6) ^ rotr(h0[4], 11) ^ rotr(h0[4], 25);
|
||||
uint32_t ch = (h0[4] & h0[5]) ^ (~h0[4] & h0[6]);
|
||||
uint32_t t1 = h0[7] + s1 + ch + K[j] + w[j];
|
||||
uint32_t s0 = rotr(h0[0], 2) ^ rotr(h0[0], 13) ^ rotr(h0[0], 22);
|
||||
uint32_t maj = (h0[0] & h0[1]) ^ (h0[0] & h0[2]) ^ (h0[1] & h0[2]);
|
||||
uint32_t t2 = s0 + maj;
|
||||
|
||||
h0[7] = h0[6];
|
||||
h0[6] = h0[5];
|
||||
h0[5] = h0[4];
|
||||
h0[4] = h0[3] + t1;
|
||||
h0[3] = h0[2];
|
||||
h0[2] = h0[1];
|
||||
h0[1] = h0[0];
|
||||
h0[0] = t1 + t2;
|
||||
}
|
||||
|
||||
for (j = 0; j < 8; j++)
|
||||
h[j] += h0[j];
|
||||
}
|
||||
|
||||
string << std::hex << std::setfill('0');
|
||||
|
||||
for (j = 0; j < 8; j++)
|
||||
for (i = 0; i < 4; i++)
|
||||
string << std::setw(2) << ((h[j] >> (24 - i * 8)) & 0xFF);
|
||||
|
||||
return string.str();
|
||||
}
|
||||
}
|
@@ -1,10 +1,121 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <fmt/core.h>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
// Why I wrote this myself? I do not know
|
||||
// I will off myself 😹😹😹😹
|
||||
namespace crypto {
|
||||
|
||||
using std::rotr;
|
||||
|
||||
template<typename std::size_t N>
|
||||
std::string
|
||||
sha256(const uint8_t* data, const size_t size);
|
||||
sha256(std::array<uint8_t, N>& data) {
|
||||
// Assuming 1 byte = 8 bits
|
||||
std::string string;
|
||||
size_t k = 512 - (N * 8 + 65) % 512;
|
||||
size_t L = N + (65 + k) / 8;
|
||||
size_t i = 0, j = 0;
|
||||
bool c = 0;
|
||||
|
||||
static constexpr std::array<uint32_t, 64> K = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
||||
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
||||
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
||||
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
||||
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
||||
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
||||
};
|
||||
|
||||
std::array<uint32_t, 8> h = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372,
|
||||
0xa54ff53a, 0x510e527f, 0x9b05688c,
|
||||
0x1f83d9ab, 0x5be0cd19 };
|
||||
|
||||
for (i = 0; i < L; i += 64) {
|
||||
size_t n = (N > i ? N - i : 0);
|
||||
|
||||
std::array<uint32_t, 8> h0 = { 0 };
|
||||
std::array<uint32_t, 64> w = { 0 };
|
||||
|
||||
if (n == 64)
|
||||
c = 1;
|
||||
|
||||
if (n >= 64) {
|
||||
for (j = 0; j < 16; j++) {
|
||||
w[j] = data[i + j * 4] << 24 | data[i + j * 4 + 1] << 16 |
|
||||
data[i + j * 4 + 2] << 8 | data[i + j * 4 + 3];
|
||||
}
|
||||
} else {
|
||||
std::array<uint32_t, 64> cur = { 0 };
|
||||
|
||||
if (n) {
|
||||
std::copy(data.begin() + i, data.begin() + N, cur.begin());
|
||||
cur[n] = 0x80;
|
||||
} else if (c) {
|
||||
cur[n] = 0x80;
|
||||
}
|
||||
|
||||
if (n < 54) {
|
||||
for (j = 56; j < 64; j++) {
|
||||
cur[j] = (N * 8 >> ((63 - j) * 8)) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
for (j = 0; j < 16; j++) {
|
||||
w[j] = cur[j * 4] << 24 | cur[j * 4 + 1] << 16 |
|
||||
cur[j * 4 + 2] << 8 | cur[j * 4 + 3];
|
||||
}
|
||||
}
|
||||
|
||||
for (j = 16; j < 64; j++) {
|
||||
uint32_t s0 =
|
||||
rotr(w[j - 15], 7) ^ rotr(w[j - 15], 18) ^ (w[j - 15] >> 3);
|
||||
uint32_t s1 =
|
||||
rotr(w[j - 2], 17) ^ rotr(w[j - 2], 19) ^ (w[j - 2] >> 10);
|
||||
|
||||
w[j] = w[j - 16] + w[j - 7] + s0 + s1;
|
||||
}
|
||||
|
||||
std::copy(h.begin(), h.end(), h0.begin());
|
||||
|
||||
for (j = 0; j < 64; j++) {
|
||||
uint32_t s1 = rotr(h0[4], 6) ^ rotr(h0[4], 11) ^ rotr(h0[4], 25);
|
||||
uint32_t ch = (h0[4] & h0[5]) ^ (~h0[4] & h0[6]);
|
||||
uint32_t t1 = h0[7] + s1 + ch + K[j] + w[j];
|
||||
uint32_t s0 = rotr(h0[0], 2) ^ rotr(h0[0], 13) ^ rotr(h0[0], 22);
|
||||
uint32_t maj = (h0[0] & h0[1]) ^ (h0[0] & h0[2]) ^ (h0[1] & h0[2]);
|
||||
uint32_t t2 = s0 + maj;
|
||||
|
||||
h0[7] = h0[6];
|
||||
h0[6] = h0[5];
|
||||
h0[5] = h0[4];
|
||||
h0[4] = h0[3] + t1;
|
||||
h0[3] = h0[2];
|
||||
h0[2] = h0[1];
|
||||
h0[1] = h0[0];
|
||||
h0[0] = t1 + t2;
|
||||
}
|
||||
|
||||
for (j = 0; j < 8; j++)
|
||||
h[j] += h0[j];
|
||||
}
|
||||
|
||||
for (j = 0; j < 8; j++)
|
||||
for (i = 0; i < 4; i++)
|
||||
fmt::format_to(std::back_inserter(string),
|
||||
"{:02x}",
|
||||
((h[j] >> (24 - i * 8)) & 0xFF));
|
||||
|
||||
return string;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user