HLRichardson-Git / Gestalt

A user-friendly cryptography library
https://gestaltcrypto.github.io
MIT License
2 stars 1 forks source link

Implement the SHA2 Algorithms #19

Closed HLRichardson-Git closed 2 months ago

HLRichardson-Git commented 2 months ago

Implementation

Follow the FIPS 180-4 standard to implement SHA2 and try to use the same naming convention of functions and variables as in 180-4. Also please keep similar documentation as is in the standard.

It shall be discussed how users should access the SHA2 algorithms since it is not just one algorithms of course. I.e. should there be gestalt/sha224.h, gestalt/sha256.h, gestalt/sha384.h, etc or just gestlat/sha2.h. I think both have merit.

Testing

Don't forget to write a new tests/sha2Tests.cpp file to test your implementation.

Starting

If you are unsure where to start I believe it would be best to get separate classes for SHA256 and SHA512 working, then the truncated version. Then try to template a single class for all 6 SHA2 algorithms. But if you have another idea this is not the only way to do it, just how I would.

Let me know if you have any questions

HLRichardson-Git commented 2 months ago

I made an initial commit for the SHA256 algorithm. I'm following my initial idea of implementing the algorithms separately to understand better what to template so we don't repeat a stupid amount of code.

I also did the thing where I made the public header file and separate "Core" files that have the implementations. This is so the user only gets a wrapped version of the implementation and doesn't have access to the internal implementation. But I think I can make this a lot cleaner and only use the public header and matching source file using "#define" statements. But this will be an idea for later. Currently, a user can use the hashSHA2(string) function as so:

#include <gestalt/sha2.h>

std::string example= hashSHA2("ABC"); // I know it would be more correct to be hashSHA256("ABC");

But I didn't realize before I used that the name of it doesn't make sense. I will change that as well lol. Surprisingly this implementation is already working, I reused the SHA1 functions/ structure a bit so that probably helped. Hopefully, this is a trend.

HLRichardson-Git commented 2 months ago

Newest commit just added SHA (224, 384, 512, 512/224 and 512/256). This commit also implemented what I said in my last comment meaning I got rid of the Core files and put all the definitions in sha2.cpp while still only allowing users to use the SHA2 hashing functions.

Note that I essentially copied and pasted SHA256 for all the other algorithms and made the respective changes. This is the process I am taking for now to get it all working and plan to template it all in further commits. Additionally more unit tests need to be added. Currently they are only being tested with a message "abc" but more variable sizes should be covered like an empty string and a string larger than the buffer size.

Here is how the code is being used for now:

#include <gestalt/sha2.h>
std::string example = "";

example= hashSHA224("abc");
example= hashSHA256("abc");
example= hashSHA384("abc");
example= hashSHA512("abc");
example= hashSHA512_224("abc");
example= hashSHA512_256("abc");

For now there are no plans to change the function names/ how the user uses the SHA2 algorithms for Gestalt.

HLRichardson-Git commented 2 months ago

This issue was completed in Pull Request #23