-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4995e03
commit 317feec
Showing
32 changed files
with
855 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.vagrant/ | ||
*.log | ||
Vagrantfile | ||
bootstrap.sh | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
|
||
project(SHA256) | ||
|
||
add_library(sha256 | ||
src/SHA256.cpp | ||
) | ||
target_include_directories(sha256 PUBLIC "${PROJECT_SOURCE_DIR}/include") | ||
|
||
add_executable(sha256-example | ||
src/main.cpp | ||
) | ||
target_include_directories(sha256-example PUBLIC "${PROJECT_SOURCE_DIR}/include") | ||
target_link_libraries(sha256-example sha256) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Jérémy LAMBERT (SystemGlitch) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
CXX ?= g++ | ||
CPPFLAGS ?= -O3 -Wall -Wextra -fPIC -std=c++17 | ||
|
||
COM_COLOR = \033[0;34m | ||
OBJ_COLOR = \033[0;36m | ||
OK_COLOR = \033[1;32m | ||
ERROR_COLOR = \033[1;31m | ||
WARN_COLOR = \033[1;33m | ||
NO_COLOR = \033[m | ||
|
||
OK_STRING = "[OK]" | ||
ERROR_STRING = "[ERROR]" | ||
WARN_STRING = "[WARNING]" | ||
COM_STRING = "Compiling" | ||
|
||
SRC_DIR := src | ||
HEADER_DIR := include | ||
BIN_DIR := bin | ||
OBJ_DIR := $(BIN_DIR)/obj | ||
MAIN := $(SRC_DIR)/main.cpp | ||
SOURCES := $(filter-out $(SRC_DIR)/main.cpp, $(wildcard $(SRC_DIR)/*.cpp)) | ||
OBJECTS := $(SOURCES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o) | ||
EXECUTABLE := $(BIN_DIR)/SHA256 | ||
|
||
clean: COUNT=$(words $(wildcard $(OBJ_DIR)/*.o)) | ||
|
||
all: $(EXECUTABLE) | ||
|
||
$(EXECUTABLE): $(OBJECTS) $(MAIN) | ||
@script -q -e -c '$(CXX) -I$(HEADER_DIR) -o $@ $(OBJECTS) $(MAIN)' $@.log > /dev/null; \ | ||
RESULT=$$?; \ | ||
sed -i '1d;$$d' $@.log; \ | ||
sed -i '/^[[:space:]]*$$/d' $@.log; \ | ||
if [ $$RESULT -ne 0 ]; then \ | ||
printf "%-60b%b" "$(COM_COLOR)Building$(OBJ_COLOR) $@" "$(ERROR_COLOR)$(ERROR_STRING)$(NO_COLOR)\n"; \ | ||
elif [ -s $@.log ]; then \ | ||
printf "%-60b%b" "$(COM_COLOR)Building$(OBJ_COLOR) $@" "$(WARN_COLOR)$(WARN_STRING)$(NO_COLOR)\n"; \ | ||
else \ | ||
printf "%-60b%b" "$(COM_COLOR)Building$(OBJ_COLOR) $@" "$(OK_COLOR)$(OK_STRING)$(NO_COLOR)\n"; \ | ||
printf "%b" "$(OK_COLOR)Build success$(NO_COLOR)\n"; \ | ||
fi; \ | ||
cat $@.log; \ | ||
rm -f $@.log; \ | ||
|
||
$(OBJECTS): $(OBJ_DIR)/%.o : $(SRC_DIR)/%.cpp | ||
@script -q -e -c '$(CXX) $(CPPFLAGS) -c -I$(HEADER_DIR) $< -o $@' $@.log > /dev/null; \ | ||
RESULT=$$?; \ | ||
sed -i '1d;$$d' $@.log; \ | ||
sed -i '/^[[:space:]]*$$/d' $@.log; \ | ||
if [ $$RESULT -ne 0 ]; then \ | ||
printf "%-60b%b" "$(COM_COLOR)$(COM_STRING)$(OBJ_COLOR) $@" "$(ERROR_COLOR)$(ERROR_STRING)$(NO_COLOR)\n"; \ | ||
elif [ -s $@.log ]; then \ | ||
printf "%-60b%b" "$(COM_COLOR)$(COM_STRING)$(OBJ_COLOR) $@" "$(WARN_COLOR)$(WARN_STRING)$(NO_COLOR)\n"; \ | ||
else \ | ||
printf "%-60b%b" "$(COM_COLOR)$(COM_STRING)$(OBJ_COLOR) $@" "$(OK_COLOR)$(OK_STRING)$(NO_COLOR)\n"; \ | ||
fi; \ | ||
cat $@.log; \ | ||
rm -f $@.log; \ | ||
|
||
clean: | ||
@if [ $(COUNT) -ne 0 ] || [ -f $(EXECUTABLE) ]; then \ | ||
if [ $(COUNT) -ne 0 ]; then \ | ||
printf "%b" "$(COM_COLOR)Cleaning objects$(NO_COLOR)\n"; \ | ||
rm -f $(OBJ_DIR)/*.o; \ | ||
fi; \ | ||
if [ -f $(EXECUTABLE) ]; then \ | ||
printf "%b" "$(COM_COLOR)Removing executable$(NO_COLOR)\n"; \ | ||
rm -f $(EXECUTABLE); \ | ||
fi; \ | ||
printf "%b" "$(OK_COLOR)Clean complete$(NO_COLOR)\n"; \ | ||
else \ | ||
printf "%b" "$(OK_COLOR)Nothing to clean$(NO_COLOR)\n"; \ | ||
fi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# SHA256 | ||
|
||
A C++ SHA256 implementation. | ||
|
||
## Build | ||
|
||
**Minimum C++11.** | ||
|
||
Just run `make all`. There are no dependencies. | ||
|
||
## ⚠️ DISCLAIMER | ||
|
||
This library has been developed for research and learning purposes. It **has not been audited** for security nor compliance with the standard. It is not advised to use it in projects where security is important. Use wide-spread and reliable libraries such as [OpenSSL](https://www.openssl.org/) instead. | ||
|
||
## Example usage | ||
|
||
### Sample program | ||
|
||
Provide as many strings as you want. The program will hash all of them in order. | ||
|
||
``` | ||
$ ./SHA256 "string" "string2" | ||
473287f8298dba7163a897908958f7c0eae733e25d2e027992ea2edc9bed2fa8 | ||
b993212a26658c9077096b804cdfb92ad21cf1e199e272c44eb028e45d07b6e0 | ||
``` | ||
|
||
### As a library | ||
|
||
```cpp | ||
#include "SHA256.h" | ||
|
||
//... | ||
|
||
string s = "hello world"; | ||
SHA256 sha; | ||
sha.update(s); | ||
std::array<uint8_t, 32> digest = sha.digest(); | ||
|
||
std::cout << SHA256::toString(digest) << std::endl; | ||
``` | ||
|
||
## Using tipi.build to install SHA256 | ||
|
||
`SHA256` can be easily used with the [tipi.build](https://tipi.build) dependency manager, by adding the following to a `.tipi/deps`: | ||
|
||
```json | ||
{ | ||
"System-Glitch/SHA256": { } | ||
} | ||
``` | ||
|
||
An example to try is available in `https://github.com/tipi-deps/example-System-Glitch-SHA256` (change the target name appropriately to `linux` or `macos` or `windows`): | ||
|
||
```bash | ||
tipi . -t <target> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SHA256 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#ifndef SHA256_H | ||
#define SHA256_H | ||
|
||
#include <array> | ||
#include <cstdint> | ||
#include <string> | ||
|
||
class SHA256 { | ||
|
||
public: | ||
SHA256(); | ||
void update(const uint8_t* data, size_t length); | ||
void update(const std::string& data); | ||
std::array<uint8_t, 32> digest(); | ||
|
||
static std::string toString(const std::array<uint8_t, 32>& digest); | ||
|
||
public: | ||
uint8_t m_data[64]; | ||
uint32_t m_blocklen; | ||
uint64_t m_bitlen; | ||
uint32_t m_state[8];//A, B, C, D, E, F, G, H | ||
|
||
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}; | ||
|
||
static uint32_t rotr(uint32_t x, uint32_t n); | ||
static uint32_t choose(uint32_t e, uint32_t f, uint32_t g); | ||
static uint32_t majority(uint32_t a, uint32_t b, uint32_t c); | ||
static uint32_t sig0(uint32_t x); | ||
static uint32_t sig1(uint32_t x); | ||
void transform(); | ||
void pad(); | ||
void revert(std::array<uint8_t, 32>& hash); | ||
}; | ||
|
||
#endif |
Oops, something went wrong.