Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/.vscode/
/demo_sha1
/test_sha1
bin/
build/
66 changes: 66 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
cmake_minimum_required(VERSION 3.18)
project("SHA1_tests")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)

get_filename_component(PARENT_DIR ../ ABSOLUTE)


if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
add_definitions(-D_WIN32)
add_compile_options("/O2")
else()
add_definitions(-D_LINUX)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
message(WARNING "Assuming system in Linux")
endif()
add_compile_options("-O3")
add_compile_options("-g")
endif()

#---------------------------------------------------------------------

add_library(SHA1_lib INTERFACE)

target_include_directories(
SHA1_lib
INTERFACE
"${CMAKE_SOURCE_DIR}"
)

#---------------------------------------------------------------------

set(TEST test_sha1)

add_executable(
"${TEST}"
test_sha1.cpp
test_sha1_file.cpp
)

target_link_libraries(
"${TEST}"
PRIVATE
SHA1_lib
)

#---------------------------------------------------------------------

# Create a 1GB size file for testing performance

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
message("Creating a 1GB file of random data in /tmp directory...")
execute_process(COMMAND dd if=/dev/urandom of=/tmp/1G_test_file.txt bs=1G count=1 iflag=fullblock
ERROR_VARIABLE error
OUTPUT_VARIABLE output
)

add_compile_definitions(TEST_FILE="/tmp/1G_test_file.txt")
endif()

#---------------------------------------------------------------------
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ RM = rm -f
all: demo_sha1 test_sha1

demo_sha1: demo_sha1.cpp sha1.cpp sha1.hpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -Wall -Wextra -std=c++11 -o $@ demo_sha1.cpp sha1.cpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -Wall -Wextra -std=c++11 -g -O3 -o $@ demo_sha1.cpp

test_sha1: test_sha1.cpp test_sha1_file.cpp sha1.hpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -Wall -Wextra -std=c++11 -o $@ test_sha1.cpp test_sha1_file.cpp sha1.cpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -Wall -Wextra -std=c++11 -g -O3 -o $@ test_sha1.cpp test_sha1_file.cpp

check: test_sha1
./test_sha1
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SHA-1 implementation in C++

## Warning

Do not use SHA-1 unless you have to!
[SHA-1 is practically broken](https://en.wikipedia.org/wiki/SHA-1#Birthday-Near-Collision_Attack_%E2%80%93_first_practical_chosen-prefix_attack). Use a hash function from the [SHA-2](https://en.wikipedia.org/wiki/SHA-2) or [SHA-3](https://en.wikipedia.org/wiki/SHA-3) family instead.

## How to build tests

Using cmake
```
mkdir build
cd build
cmake ..
make
```

## License

100% Public Domain

## Authors

- Steve Reid (Original C Code)
- [Bruce Guenter](http://untroubled.org/) (Small changes to fit into bglibs)
- [Volker Diels-Grabsch](https://njh.eu/) (Translation to simpler C++ Code)
- [Eugene Hopkinson](https://riot.so/) (Safety improvements)
- [Zlatko Michailov](http://zlatko.michailov.org) (Header-only library)
- [Dan Machado](dan-machado@yandex.com) (Refactoring)
17 changes: 0 additions & 17 deletions README.org

This file was deleted.

8 changes: 8 additions & 0 deletions performance.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
perf stat ./../bin/test_sha1
0.011223803 seconds time elapsed

perf stat ./../bin/test_sha1 --slow
6.446021922 seconds time elapsed

perf stat ./../bin/test_sha1 /tmp/1G_test_file.txt
1.885112058 seconds time elapsed
Loading