diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..fdedc39 --- /dev/null +++ b/.clang-format @@ -0,0 +1,40 @@ +# C and C++ +--- +AlignEscapedNewlines: Left +AlignTrailingComments: true +BasedOnStyle: LLVM +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterFunction: false + AfterEnum: false + AfterNamespace: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: true +BreakConstructorInitializers: BeforeComma +ColumnLimit: 120 +IndentAccessModifiers: false +IndentCaseLabels: true +IndentPPDirectives: None +IndentRequiresClause: false +IndentWidth: 2 +InsertNewlineAtEOF: true +# clang clang-format 19 +# KeepEmptyLines: +# AtEndOfFile: true +LineEnding: LF +NamespaceIndentation: None +QualifierAlignment: Right +ReflowComments: false +RemoveSemicolon: true +RequiresClausePosition: WithFollowing +RequiresExpressionIndentation: OuterScope +SpaceAfterTemplateKeyword: false +TabWidth: 2 diff --git a/.gitignore b/.gitignore index 6416eaa..dc58011 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.vscode/ /demo_sha1 +/test_clang1 /test_sha1 diff --git a/Makefile b/Makefile index 41fc3c9..780f643 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,23 @@ -CROSS = -CXX = $(CROSS)g++ -RM = rm -f +CROSS = +CXX = $(CROSS)g++ +RM = rm -f +CXX_CLANG = clang++ +CXX_EXTRA_FLAGS = -Werror -Weverything -Wno-c++98-compat-pedantic -Wno-unsafe-buffer-usage -Wno-switch-default 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 -Wconversion -o $@ demo_sha1.cpp 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 -Wconversion -o $@ test_sha1.cpp test_sha1_file.cpp sha1.cpp + +test_clang1: test_sha1.cpp test_sha1_file.cpp sha1.hpp + $(CXX_CLANG) $(CXX_EXTRA_FLAGS) -o $@ test_sha1.cpp test_sha1_file.cpp sha1.cpp + check: test_sha1 ./test_sha1 clean: - $(RM) demo_sha1 test_sha1 + $(RM) demo_sha1 test_sha1 test_clang diff --git a/demo_sha1.cpp b/demo_sha1.cpp index cce28ca..314994c 100644 --- a/demo_sha1.cpp +++ b/demo_sha1.cpp @@ -1,12 +1,12 @@ /* demo_sha1.cpp - demo program of - + ============ SHA-1 in C++ ============ - + 100% Public Domain. - + Original C Code -- Steve Reid Small changes to fit into bglibs @@ -16,21 +16,20 @@ */ #include "sha1.hpp" -#include #include -using std::string; +#include using std::cout; using std::endl; +using std::string; -int main(int /* argc */, const char ** /* argv */) -{ - const string input = "abc"; +int main(int /* argc */, char const ** /* argv */) { + string const input = "abc"; - SHA1 checksum; - checksum.update(input); - const string hash = checksum.final(); + SHA1 checksum; + checksum.update(input); + string const hash = checksum.final(); - cout << "The SHA-1 of \"" << input << "\" is: " << hash << endl; + cout << "The SHA-1 of \"" << input << "\" is: " << hash << endl; - return 0; + return 0; } diff --git a/sha1.cpp b/sha1.cpp index ccae7c3..574b797 100644 --- a/sha1.cpp +++ b/sha1.cpp @@ -1,5 +1,5 @@ /* - sha1.cpp - This file is intenionally left blank. + sha1.cpp - This file is intentionally left blank. This file is no longer needed. It is only kept for backard compatibility. Simply: #include "sha1.hpp" */ diff --git a/sha1.hpp b/sha1.hpp index 67a6b52..bdeb4e3 100644 --- a/sha1.hpp +++ b/sha1.hpp @@ -1,12 +1,9 @@ /* sha1.hpp - source code of - ============ SHA-1 in C++ ============ - 100% Public Domain. - Original C Code -- Steve Reid Small changes to fit into bglibs @@ -17,12 +14,13 @@ -- Eugene Hopkinson Header-only library -- Zlatko Michailov + Strict conversion fixes + -- Jan Philipp Hafer */ #ifndef SHA1_HPP #define SHA1_HPP - #include #include #include @@ -30,305 +28,264 @@ #include #include - -class SHA1 -{ +class SHA1 { public: - SHA1(); - void update(const std::string &s); - void update(std::istream &is); - std::string final(); - static std::string from_file(const std::string &filename); + SHA1(); + void update(std::string const &s); + void update(std::istream &is); + std::string final(); + static std::string from_file(std::string const &filename); private: - uint32_t digest[5]; - std::string buffer; - uint64_t transforms; + uint32_t digest[5]; + [[maybe_unused]] char _digest_pad[4]; + std::string buffer; + uint64_t transforms; }; +static size_t const BLOCK_INTS = 16; /* number of 32bit integers per SHA1 block */ +static size_t const BLOCK_BYTES = BLOCK_INTS * 4; -static const size_t BLOCK_INTS = 16; /* number of 32bit integers per SHA1 block */ -static const size_t BLOCK_BYTES = BLOCK_INTS * 4; +inline static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms) { + /* SHA1 initialization constants */ + digest[0] = 0x67452301; + digest[1] = 0xefcdab89; + digest[2] = 0x98badcfe; + digest[3] = 0x10325476; + digest[4] = 0xc3d2e1f0; - -inline static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms) -{ - /* SHA1 initialization constants */ - digest[0] = 0x67452301; - digest[1] = 0xefcdab89; - digest[2] = 0x98badcfe; - digest[3] = 0x10325476; - digest[4] = 0xc3d2e1f0; - - /* Reset counters */ - buffer = ""; - transforms = 0; + /* Reset counters */ + buffer = ""; + transforms = 0; } +inline static uint32_t rol(uint32_t const value, size_t const bits) { return (value << bits) | (value >> (32 - bits)); } -inline static uint32_t rol(const uint32_t value, const size_t bits) -{ - return (value << bits) | (value >> (32 - bits)); +inline static uint32_t blk(uint32_t const block[BLOCK_INTS], size_t const i) { + return rol(block[(i + 13) & 15] ^ block[(i + 8) & 15] ^ block[(i + 2) & 15] ^ block[i], 1); } - -inline static uint32_t blk(const uint32_t block[BLOCK_INTS], const size_t i) -{ - return rol(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i], 1); -} - - /* * (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ -inline static void R0(const uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) -{ - z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5); - w = rol(w, 30); +inline static void R0(uint32_t const block[BLOCK_INTS], uint32_t const v, uint32_t &w, uint32_t const x, + uint32_t const y, uint32_t &z, size_t const i) { + z += ((w & (x ^ y)) ^ y) + block[i] + 0x5a827999 + rol(v, 5); + w = rol(w, 30); } - -inline static void R1(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) -{ - block[i] = blk(block, i); - z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5); - w = rol(w, 30); +inline static void R1(uint32_t block[BLOCK_INTS], uint32_t const v, uint32_t &w, uint32_t const x, uint32_t const y, + uint32_t &z, size_t const i) { + block[i] = blk(block, i); + z += ((w & (x ^ y)) ^ y) + block[i] + 0x5a827999 + rol(v, 5); + w = rol(w, 30); } - -inline static void R2(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) -{ - block[i] = blk(block, i); - z += (w^x^y) + block[i] + 0x6ed9eba1 + rol(v, 5); - w = rol(w, 30); +inline static void R2(uint32_t block[BLOCK_INTS], uint32_t const v, uint32_t &w, uint32_t const x, uint32_t const y, + uint32_t &z, size_t const i) { + block[i] = blk(block, i); + z += (w ^ x ^ y) + block[i] + 0x6ed9eba1 + rol(v, 5); + w = rol(w, 30); } - -inline static void R3(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) -{ - block[i] = blk(block, i); - z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + rol(v, 5); - w = rol(w, 30); +inline static void R3(uint32_t block[BLOCK_INTS], uint32_t const v, uint32_t &w, uint32_t const x, uint32_t const y, + uint32_t &z, size_t const i) { + block[i] = blk(block, i); + z += (((w | x) & y) | (w & x)) + block[i] + 0x8f1bbcdc + rol(v, 5); + w = rol(w, 30); } - -inline static void R4(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) -{ - block[i] = blk(block, i); - z += (w^x^y) + block[i] + 0xca62c1d6 + rol(v, 5); - w = rol(w, 30); +inline static void R4(uint32_t block[BLOCK_INTS], uint32_t const v, uint32_t &w, uint32_t const x, uint32_t const y, + uint32_t &z, size_t const i) { + block[i] = blk(block, i); + z += (w ^ x ^ y) + block[i] + 0xca62c1d6 + rol(v, 5); + w = rol(w, 30); } - /* * Hash a single 512-bit block. This is the core of the algorithm. */ -inline static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms) -{ - /* Copy digest[] to working vars */ - uint32_t a = digest[0]; - uint32_t b = digest[1]; - uint32_t c = digest[2]; - uint32_t d = digest[3]; - uint32_t e = digest[4]; - - /* 4 rounds of 20 operations each. Loop unrolled. */ - R0(block, a, b, c, d, e, 0); - R0(block, e, a, b, c, d, 1); - R0(block, d, e, a, b, c, 2); - R0(block, c, d, e, a, b, 3); - R0(block, b, c, d, e, a, 4); - R0(block, a, b, c, d, e, 5); - R0(block, e, a, b, c, d, 6); - R0(block, d, e, a, b, c, 7); - R0(block, c, d, e, a, b, 8); - R0(block, b, c, d, e, a, 9); - R0(block, a, b, c, d, e, 10); - R0(block, e, a, b, c, d, 11); - R0(block, d, e, a, b, c, 12); - R0(block, c, d, e, a, b, 13); - R0(block, b, c, d, e, a, 14); - R0(block, a, b, c, d, e, 15); - R1(block, e, a, b, c, d, 0); - R1(block, d, e, a, b, c, 1); - R1(block, c, d, e, a, b, 2); - R1(block, b, c, d, e, a, 3); - R2(block, a, b, c, d, e, 4); - R2(block, e, a, b, c, d, 5); - R2(block, d, e, a, b, c, 6); - R2(block, c, d, e, a, b, 7); - R2(block, b, c, d, e, a, 8); - R2(block, a, b, c, d, e, 9); - R2(block, e, a, b, c, d, 10); - R2(block, d, e, a, b, c, 11); - R2(block, c, d, e, a, b, 12); - R2(block, b, c, d, e, a, 13); - R2(block, a, b, c, d, e, 14); - R2(block, e, a, b, c, d, 15); - R2(block, d, e, a, b, c, 0); - R2(block, c, d, e, a, b, 1); - R2(block, b, c, d, e, a, 2); - R2(block, a, b, c, d, e, 3); - R2(block, e, a, b, c, d, 4); - R2(block, d, e, a, b, c, 5); - R2(block, c, d, e, a, b, 6); - R2(block, b, c, d, e, a, 7); - R3(block, a, b, c, d, e, 8); - R3(block, e, a, b, c, d, 9); - R3(block, d, e, a, b, c, 10); - R3(block, c, d, e, a, b, 11); - R3(block, b, c, d, e, a, 12); - R3(block, a, b, c, d, e, 13); - R3(block, e, a, b, c, d, 14); - R3(block, d, e, a, b, c, 15); - R3(block, c, d, e, a, b, 0); - R3(block, b, c, d, e, a, 1); - R3(block, a, b, c, d, e, 2); - R3(block, e, a, b, c, d, 3); - R3(block, d, e, a, b, c, 4); - R3(block, c, d, e, a, b, 5); - R3(block, b, c, d, e, a, 6); - R3(block, a, b, c, d, e, 7); - R3(block, e, a, b, c, d, 8); - R3(block, d, e, a, b, c, 9); - R3(block, c, d, e, a, b, 10); - R3(block, b, c, d, e, a, 11); - R4(block, a, b, c, d, e, 12); - R4(block, e, a, b, c, d, 13); - R4(block, d, e, a, b, c, 14); - R4(block, c, d, e, a, b, 15); - R4(block, b, c, d, e, a, 0); - R4(block, a, b, c, d, e, 1); - R4(block, e, a, b, c, d, 2); - R4(block, d, e, a, b, c, 3); - R4(block, c, d, e, a, b, 4); - R4(block, b, c, d, e, a, 5); - R4(block, a, b, c, d, e, 6); - R4(block, e, a, b, c, d, 7); - R4(block, d, e, a, b, c, 8); - R4(block, c, d, e, a, b, 9); - R4(block, b, c, d, e, a, 10); - R4(block, a, b, c, d, e, 11); - R4(block, e, a, b, c, d, 12); - R4(block, d, e, a, b, c, 13); - R4(block, c, d, e, a, b, 14); - R4(block, b, c, d, e, a, 15); - - /* Add the working vars back into digest[] */ - digest[0] += a; - digest[1] += b; - digest[2] += c; - digest[3] += d; - digest[4] += e; - - /* Count the number of transformations */ - transforms++; +inline static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms) { + /* Copy digest[] to working vars */ + uint32_t a = digest[0]; + uint32_t b = digest[1]; + uint32_t c = digest[2]; + uint32_t d = digest[3]; + uint32_t e = digest[4]; + + /* 4 rounds of 20 operations each. Loop unrolled. */ + R0(block, a, b, c, d, e, 0); + R0(block, e, a, b, c, d, 1); + R0(block, d, e, a, b, c, 2); + R0(block, c, d, e, a, b, 3); + R0(block, b, c, d, e, a, 4); + R0(block, a, b, c, d, e, 5); + R0(block, e, a, b, c, d, 6); + R0(block, d, e, a, b, c, 7); + R0(block, c, d, e, a, b, 8); + R0(block, b, c, d, e, a, 9); + R0(block, a, b, c, d, e, 10); + R0(block, e, a, b, c, d, 11); + R0(block, d, e, a, b, c, 12); + R0(block, c, d, e, a, b, 13); + R0(block, b, c, d, e, a, 14); + R0(block, a, b, c, d, e, 15); + R1(block, e, a, b, c, d, 0); + R1(block, d, e, a, b, c, 1); + R1(block, c, d, e, a, b, 2); + R1(block, b, c, d, e, a, 3); + R2(block, a, b, c, d, e, 4); + R2(block, e, a, b, c, d, 5); + R2(block, d, e, a, b, c, 6); + R2(block, c, d, e, a, b, 7); + R2(block, b, c, d, e, a, 8); + R2(block, a, b, c, d, e, 9); + R2(block, e, a, b, c, d, 10); + R2(block, d, e, a, b, c, 11); + R2(block, c, d, e, a, b, 12); + R2(block, b, c, d, e, a, 13); + R2(block, a, b, c, d, e, 14); + R2(block, e, a, b, c, d, 15); + R2(block, d, e, a, b, c, 0); + R2(block, c, d, e, a, b, 1); + R2(block, b, c, d, e, a, 2); + R2(block, a, b, c, d, e, 3); + R2(block, e, a, b, c, d, 4); + R2(block, d, e, a, b, c, 5); + R2(block, c, d, e, a, b, 6); + R2(block, b, c, d, e, a, 7); + R3(block, a, b, c, d, e, 8); + R3(block, e, a, b, c, d, 9); + R3(block, d, e, a, b, c, 10); + R3(block, c, d, e, a, b, 11); + R3(block, b, c, d, e, a, 12); + R3(block, a, b, c, d, e, 13); + R3(block, e, a, b, c, d, 14); + R3(block, d, e, a, b, c, 15); + R3(block, c, d, e, a, b, 0); + R3(block, b, c, d, e, a, 1); + R3(block, a, b, c, d, e, 2); + R3(block, e, a, b, c, d, 3); + R3(block, d, e, a, b, c, 4); + R3(block, c, d, e, a, b, 5); + R3(block, b, c, d, e, a, 6); + R3(block, a, b, c, d, e, 7); + R3(block, e, a, b, c, d, 8); + R3(block, d, e, a, b, c, 9); + R3(block, c, d, e, a, b, 10); + R3(block, b, c, d, e, a, 11); + R4(block, a, b, c, d, e, 12); + R4(block, e, a, b, c, d, 13); + R4(block, d, e, a, b, c, 14); + R4(block, c, d, e, a, b, 15); + R4(block, b, c, d, e, a, 0); + R4(block, a, b, c, d, e, 1); + R4(block, e, a, b, c, d, 2); + R4(block, d, e, a, b, c, 3); + R4(block, c, d, e, a, b, 4); + R4(block, b, c, d, e, a, 5); + R4(block, a, b, c, d, e, 6); + R4(block, e, a, b, c, d, 7); + R4(block, d, e, a, b, c, 8); + R4(block, c, d, e, a, b, 9); + R4(block, b, c, d, e, a, 10); + R4(block, a, b, c, d, e, 11); + R4(block, e, a, b, c, d, 12); + R4(block, d, e, a, b, c, 13); + R4(block, c, d, e, a, b, 14); + R4(block, b, c, d, e, a, 15); + + /* Add the working vars back into digest[] */ + digest[0] += a; + digest[1] += b; + digest[2] += c; + digest[3] += d; + digest[4] += e; + + /* Count the number of transformations */ + transforms++; } - -inline static void buffer_to_block(const std::string &buffer, uint32_t block[BLOCK_INTS]) -{ - /* Convert the std::string (byte buffer) to a uint32_t array (MSB) */ - for (size_t i = 0; i < BLOCK_INTS; i++) - { - block[i] = (buffer[4*i+3] & 0xff) - | (buffer[4*i+2] & 0xff)<<8 - | (buffer[4*i+1] & 0xff)<<16 - | (buffer[4*i+0] & 0xff)<<24; - } -} - - -inline SHA1::SHA1() -{ - reset(digest, buffer, transforms); +inline static void buffer_to_block(std::string const &buffer, uint32_t block[BLOCK_INTS]) { + /* Convert the std::string (byte buffer) to a uint32_t array (MSB) */ + for (size_t i = 0; i < BLOCK_INTS; i++) { + block[i] = static_cast((buffer[4 * i + 3] & 0xff) | (buffer[4 * i + 2] & 0xff) << 8 | + (buffer[4 * i + 1] & 0xff) << 16 | (buffer[4 * i + 0] & 0xff) << 24); + } } +inline SHA1::SHA1() { reset(digest, buffer, transforms); } -inline void SHA1::update(const std::string &s) -{ - std::istringstream is(s); - update(is); +inline void SHA1::update(std::string const &s) { + std::istringstream is(s); + update(is); } - -inline void SHA1::update(std::istream &is) -{ - while (true) - { - char sbuf[BLOCK_BYTES]; - is.read(sbuf, BLOCK_BYTES - buffer.size()); - buffer.append(sbuf, (std::size_t)is.gcount()); - if (buffer.size() != BLOCK_BYTES) - { - return; - } - uint32_t block[BLOCK_INTS]; - buffer_to_block(buffer, block); - transform(digest, block, transforms); - buffer.clear(); +inline void SHA1::update(std::istream &is) { + while (true) { + char sbuf[BLOCK_BYTES]; + is.read(sbuf, static_cast(BLOCK_BYTES - buffer.size())); + buffer.append(sbuf, static_cast(is.gcount())); + if (buffer.size() != BLOCK_BYTES) { + return; } + uint32_t block[BLOCK_INTS]; + buffer_to_block(buffer, block); + transform(digest, block, transforms); + buffer.clear(); + } } - /* * Add padding and return the message digest. */ -inline std::string SHA1::final() -{ - /* Total number of hashed bits */ - uint64_t total_bits = (transforms*BLOCK_BYTES + buffer.size()) * 8; - - /* Padding */ - buffer += (char)0x80; - size_t orig_size = buffer.size(); - while (buffer.size() < BLOCK_BYTES) - { - buffer += (char)0x00; - } +inline std::string SHA1::final() { + /* Total number of hashed bits */ + uint64_t total_bits = (transforms * BLOCK_BYTES + buffer.size()) * 8; - uint32_t block[BLOCK_INTS]; - buffer_to_block(buffer, block); + /* Padding */ + buffer += static_cast(0x80); + size_t orig_size = buffer.size(); + while (buffer.size() < BLOCK_BYTES) { + buffer += static_cast(0x00); + } - if (orig_size > BLOCK_BYTES - 8) - { - transform(digest, block, transforms); - for (size_t i = 0; i < BLOCK_INTS - 2; i++) - { - block[i] = 0; - } - } + uint32_t block[BLOCK_INTS]; + buffer_to_block(buffer, block); - /* Append total_bits, split this uint64_t into two uint32_t */ - block[BLOCK_INTS - 1] = (uint32_t)total_bits; - block[BLOCK_INTS - 2] = (uint32_t)(total_bits >> 32); + if (orig_size > BLOCK_BYTES - 8) { transform(digest, block, transforms); - - /* Hex std::string */ - std::ostringstream result; - for (size_t i = 0; i < sizeof(digest) / sizeof(digest[0]); i++) - { - result << std::hex << std::setfill('0') << std::setw(8); - result << digest[i]; + for (size_t i = 0; i < BLOCK_INTS - 2; i++) { + block[i] = 0; } + } - /* Reset for next run */ - reset(digest, buffer, transforms); + /* Append total_bits, split this uint64_t into two uint32_t */ + block[BLOCK_INTS - 1] = static_cast(total_bits); + block[BLOCK_INTS - 2] = static_cast(total_bits >> 32); + transform(digest, block, transforms); - return result.str(); -} + /* Hex std::string */ + std::ostringstream result; + for (size_t i = 0; i < sizeof(digest) / sizeof(digest[0]); i++) { + result << std::hex << std::setfill('0') << std::setw(8); + result << digest[i]; + } + /* Reset for next run */ + reset(digest, buffer, transforms); -inline std::string SHA1::from_file(const std::string &filename) -{ - std::ifstream stream(filename.c_str(), std::ios::binary); - SHA1 checksum; - checksum.update(stream); - return checksum.final(); + return result.str(); } +inline std::string SHA1::from_file(std::string const &filename) { + std::ifstream stream(filename.c_str(), std::ios::binary); + SHA1 checksum; + checksum.update(stream); + return checksum.final(); +} #endif /* SHA1_HPP */ diff --git a/test_sha1.cpp b/test_sha1.cpp index 56fda85..0950cd7 100644 --- a/test_sha1.cpp +++ b/test_sha1.cpp @@ -1,12 +1,12 @@ /* test_sha1.cpp - test program of - + ============ SHA-1 in C++ ============ - + 100% Public Domain. - + Original C Code -- Steve Reid Small changes to fit into bglibs @@ -18,155 +18,155 @@ */ #include "sha1.hpp" -#include "sha1.hpp" // Intentionally included twice for testing purposes +#include "sha1.hpp" // Intentionally included twice for testing purposes #include #include -using std::string; using std::cout; using std::endl; - +using std::string; /* * This method is defined in test_sha1_file.cpp. * The purpose of the split is to test linking of multiple files that include sha1.hpp. */ -void test_file(const string &filename); - +void test_file(string const &filename); -void compare(const string &result, const string &expected) -{ - const string &state = (result == expected) ? "OK" : "Failure"; - cout << "Result: " << result << endl; - cout << "Expected: " << expected << " (" << state << ")" << endl; +static void compare(string const &result, string const &expected) { + string const &state = (result == expected) ? "OK" : "Failure"; + cout << "Result: " << result << endl; + cout << "Expected: " << expected << " (" << state << ")" << endl; } - /* * The 3 test vectors from FIPS PUB 180-1 */ -void test_standard() -{ - // https://www.di-mgt.com.au/sha_testvectors.html - // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA1.pdf - - SHA1 checksum; - - cout << endl; - cout << "Test: abc" << endl; - checksum.update("abc"); - compare(checksum.final(), "a9993e364706816aba3e25717850c26c9cd0d89d"); - - cout << endl; - cout << "Test: abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" << endl; - checksum.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); - compare(checksum.final(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1"); - - - cout << endl; - cout << "Test: abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" << endl; - checksum.update("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"); - compare(checksum.final(), "a49b2446a02c645bf419f995b67091253a04a259"); - - cout << endl; - cout << "Test: A million repetitions of 'a'" << endl; - for (int i = 0; i < 1000000/200; ++i) - { - checksum.update("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - ); - } - compare(checksum.final(), "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); - - // https://en.wikipedia.org/wiki/SHA-1 - cout << endl; - cout << "Test: The quick brown fox jumps over the lazy dog" << endl; - checksum.update("The quick brown fox jumps over the lazy dog"); - compare(checksum.final(), "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"); - - cout << endl; - cout << "Test: The quick brown fox jumps over the lazy cog" << endl; - checksum.update("The quick brown fox jumps over the lazy cog"); - compare(checksum.final(), "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3"); +static void test_standard() { + // https://www.di-mgt.com.au/sha_testvectors.html + // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA1.pdf + + SHA1 checksum; + + cout << endl; + cout << "Test: abc" << endl; + checksum.update("abc"); + compare(checksum.final(), "a9993e364706816aba3e25717850c26c9cd0d89d"); + + cout << endl; + cout << "Test: abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" << endl; + checksum.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); + compare(checksum.final(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1"); + + cout << endl; + cout << "Test: " + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopq" + "rstu" + << endl; + checksum.update("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnop" + "qrstnopqrstu"); + compare(checksum.final(), "a49b2446a02c645bf419f995b67091253a04a259"); + + cout << endl; + cout << "Test: A million repetitions of 'a'" << endl; + for (int i = 0; i < 1000000 / 200; ++i) { + checksum.update("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + } + compare(checksum.final(), "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); + + // https://en.wikipedia.org/wiki/SHA-1 + cout << endl; + cout << "Test: The quick brown fox jumps over the lazy dog" << endl; + checksum.update("The quick brown fox jumps over the lazy dog"); + compare(checksum.final(), "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"); + + cout << endl; + cout << "Test: The quick brown fox jumps over the lazy cog" << endl; + checksum.update("The quick brown fox jumps over the lazy cog"); + compare(checksum.final(), "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3"); } -void test_slow() -{ - // https://www.di-mgt.com.au/sha_testvectors.html +static void test_slow() { + // https://www.di-mgt.com.au/sha_testvectors.html - SHA1 checksum; + SHA1 checksum; - cout << endl; - cout << "Test: 16,777,216 repititions of abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno" << endl; - for (int i = 0; i < 16777216; ++i) - { - checksum.update("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"); - } - compare(checksum.final(), "7789f0c9ef7bfc40d93311143dfbe69e2017f592"); + cout << endl; + cout << "Test: 16,777,216 repititions of abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno" + << endl; + for (int i = 0; i < 16777216; ++i) { + checksum.update("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"); + } + compare(checksum.final(), "7789f0c9ef7bfc40d93311143dfbe69e2017f592"); } /* * Other tests */ -void test_other() -{ - SHA1 checksum; - - cout << endl; - cout << "Test: No string" << endl; - compare(checksum.final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - cout << endl; - checksum.update(""); - cout << "Test: Empty string" << endl; - compare(checksum.final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); - - cout << endl; - cout << "Test: abcde" << endl; - checksum.update("abcde"); - compare(checksum.final(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); - - cout << endl; - cout << "Test: Two concurrent checksum calculations" << endl; - SHA1 checksum1, checksum2; - checksum1.update("abc"); - compare(checksum2.final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); /* "" */ - compare(checksum1.final(), "a9993e364706816aba3e25717850c26c9cd0d89d"); /* "abc" */ - - cout << endl; - cout << "Test: a [00] b [7F] c [80] d [FF] e [C3] [F0] f" << endl; - checksum.update(std::string("a" "\x00" "b" "\x7f" "c" "\x80" "d" "\xff" "e" "\xc3\xf0" "f", 12)); - compare(checksum.final(), "cd0dd10814c0d4f9c6a2a0a4be2304d2371468d3"); +static void test_other() { + SHA1 checksum; + + cout << endl; + cout << "Test: No string" << endl; + compare(checksum.final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); + + cout << endl; + checksum.update(""); + cout << "Test: Empty string" << endl; + compare(checksum.final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); + + cout << endl; + cout << "Test: abcde" << endl; + checksum.update("abcde"); + compare(checksum.final(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334"); + + cout << endl; + cout << "Test: Two concurrent checksum calculations" << endl; + SHA1 checksum1, checksum2; + checksum1.update("abc"); + compare(checksum2.final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); /* "" */ + compare(checksum1.final(), "a9993e364706816aba3e25717850c26c9cd0d89d"); /* "abc" */ + + cout << endl; + cout << "Test: a [00] b [7F] c [80] d [FF] e [C3] [F0] f" << endl; + checksum.update(std::string("a" + "\x00" + "b" + "\x7f" + "c" + "\x80" + "d" + "\xff" + "e" + "\xc3\xf0" + "f", + 12)); + compare(checksum.final(), "cd0dd10814c0d4f9c6a2a0a4be2304d2371468d3"); } /* * main */ -int main(int argc, const char *argv[]) -{ - const bool slow = (argc == 2 && std::string("--slow") == argv[1]); +int main(int argc, char const *argv[]) { + bool const slow = (argc == 2 && std::string("--slow") == argv[1]); - if (argc > 1 && !slow) - { - for (int i = 1; i < argc; i++) - { - test_file(argv[i]); - } + if (argc > 1 && !slow) { + for (int i = 1; i < argc; i++) { + test_file(argv[i]); } - else - { - test_standard(); - test_other(); - if (slow) { - test_slow(); - } - cout << endl; - cout << endl; + } else { + test_standard(); + test_other(); + if (slow) { + test_slow(); } + cout << endl; + cout << endl; + } - return 0; + return 0; } diff --git a/test_sha1_file.cpp b/test_sha1_file.cpp index ce88ebb..a8fdc77 100644 --- a/test_sha1_file.cpp +++ b/test_sha1_file.cpp @@ -1,13 +1,13 @@ /* test_sha1_file.cpp - tests are intentionally split in two files. The purpose of the split is to test linking of multiple files that include sha1.hpp. - + ============ SHA-1 in C++ ============ - + 100% Public Domain. - + Original C Code -- Steve Reid Small changes to fit into bglibs @@ -19,19 +19,18 @@ */ #include "sha1.hpp" -#include "sha1.hpp" // Intentionally included twice for testing purposes +#include "sha1.hpp" // Intentionally included twice for testing purposes #include #include -using std::string; using std::cout; using std::endl; - +using std::string; /* * Produce same output as "sha1sum -b" */ - -void test_file(const string &filename) -{ - cout << SHA1::from_file(filename) << " *" << filename << endl; -} +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-function" +#pragma clang diagnostic ignored "-Wmissing-prototypes" +void test_file(string const &filename) { cout << SHA1::from_file(filename) << " *" << filename << endl; } +#pragma clang diagnostic pop