Checking comments

This commit is contained in:
manzerbredes 2015-03-12 20:59:12 +04:00
parent 6fcf2131d3
commit 80f1e1ce67
2 changed files with 46 additions and 33 deletions

View file

@ -8,13 +8,12 @@
* *
*/ */
//----- class ----- //----- class -----
#include "HASHCrypt.hpp" #include "HASHCrypt.hpp"
//Constructor //Constructor
HASHCrypt::HASHCrypt(){ HASHCrypt::HASHCrypt(){
} }
@ -25,6 +24,7 @@ HASHCrypt::~HASHCrypt(){
//Contruct MD5 over 128 bits and put it into digest
void HASHCrypt::getMD5_128(std::string chain, byte* digest, int size){ void HASHCrypt::getMD5_128(std::string chain, byte* digest, int size){
//Digest size controller //Digest size controller
@ -37,6 +37,8 @@ void HASHCrypt::getMD5_128(std::string chain, byte* digest, int size){
} }
//Contruct SHA-256 and put it into digest
void HASHCrypt::getSHA_256(std::string chain, byte* digest, int size){ void HASHCrypt::getSHA_256(std::string chain, byte* digest, int size){
//Digest size controller //Digest size controller
@ -50,27 +52,6 @@ void HASHCrypt::getSHA_256(std::string chain, byte* digest, int size){
} }
//Check the size of the digest
void HASHCrypt::checkDigestSize(int sizeRequired, int size){
try{
if(size !=sizeRequired){
throw this->getInvalidDigestSizeError(sizeRequired, size);
}
}
catch(std::string erreur){
std::cerr << erreur <<std::endl;
std::exit(EXIT_FAILURE);
}
}
//Make the error
std::string HASHCrypt::getInvalidDigestSizeError(int sizeRequired, int size){
std::ostringstream erreurStream;
erreurStream << "Invalid digest size ! ("<< sizeRequired <<" bytes required and "<< size <<" given)";
return erreurStream.str();
}
//Compare 2 digest (same size) //Compare 2 digest (same size)
bool HASHCrypt::compareDigest(byte* digest1, byte* digest2, int size){ bool HASHCrypt::compareDigest(byte* digest1, byte* digest2, int size){
@ -96,6 +77,7 @@ bool HASHCrypt::compareDigest(byte* digest1, byte* digest2, int size){
} }
//Convert digest to string //Convert digest to string
std::string HASHCrypt::digestToString(byte* digest, int size){ std::string HASHCrypt::digestToString(byte* digest, int size){
@ -107,3 +89,28 @@ std::string HASHCrypt::digestToString(byte* digest, int size){
return output; return output;
} }
//Check the size of the digest
void HASHCrypt::checkDigestSize(int sizeRequired, int size){
try{
if(size !=sizeRequired){
throw this->getInvalidDigestSizeError(sizeRequired, size);
}
}
catch(std::string erreur){
std::cerr << erreur <<std::endl;
std::exit(EXIT_FAILURE);
}
}
//Make the error
std::string HASHCrypt::getInvalidDigestSizeError(int sizeRequired, int size){
std::ostringstream erreurStream;
erreurStream << "Invalid digest size ! ("<< sizeRequired <<" bytes required and "<< size <<" given)";
return erreurStream.str();
}

View file

@ -45,27 +45,31 @@ class HASHCrypt{
*/ */
~HASHCrypt(); ~HASHCrypt();
/** /**
* @brief Create an MD5 over 128 bits on a digest array of bytes. * @brief Create an MD5 over 128 bits on a digest array of bytes.
* *
* @param chain : Chain to hash * @param chain : Chain to hash
* @param digest : An array of bytes (8 bits) * @param digest : An array of bytes (8 bits)
* @param size : Length of the array digest * @param size : Length of digest
* *
* **Warning** digest will be modified. * **Warning** digest will be modified.
* Digest must be an array of byte with 16 entries * Digest must be an array of byte with 16 entries
* Invalid size can cause "Segmentation Fault"
*/ */
void getMD5_128(std::string chain, byte* digest, int size); void getMD5_128(std::string chain, byte* digest, int size);
/** /**
* @brief Create an SHA over 256 bits on a digest array of bytes. * @brief Create an SHA over 256 bits on a digest array of bytes.
* *
* @param chain : Chain to hash * @param chain : Chain to hash
* @param digest : An array of bytes (8 bits) * @param digest : An array of bytes (8 bits)
* @param size : Length of the array digest * @param size : Length of digest
* *
* **Warning** digest will be modified. * **Warning** digest will be modified.
* Digest must be an array of byte with 32 entries * Digest must be an array of byte with 32 entries
* Invalid size can cause "Segmentation Fault"
*/ */
void getSHA_256(std::string chain, byte* digest, int size); //Return SHA_256 void getSHA_256(std::string chain, byte* digest, int size); //Return SHA_256
@ -73,14 +77,15 @@ class HASHCrypt{
/** /**
* @brief Convert digest to a string of HEX characters * @brief Convert digest to a string of HEX characters
* *
* @param digest : An array of bytes (8 bits) * @param digest : an array of bytes (8 bits)
* @param size : Length of the array digest * @param size : length of digest
* *
* @return a string of hex digest equivalent * @return return a string of hex digest equivalent
* *
* Digest must be an array of byte with 16 entries * Digest must be an array of byte.
*/ */
std::string digestToString(byte* digest, int size); //Return a string of a digest std::string digestToString(byte* digest, int size); //Return a string
/** /**
* @brief Compare 2 digest * @brief Compare 2 digest
@ -89,10 +94,11 @@ class HASHCrypt{
* @param digest2 : An array of bytes (8 bits) * @param digest2 : An array of bytes (8 bits)
* @param size : Length of the array digest1 or digest2 * @param size : Length of the array digest1 or digest2
* *
* @return a boolean if digest1 equals to digest2 * @return return a boolean (true if digest1 equals to digest2 and false else)
* *
* **Warning** if sizeof(digest1) != sizeof(digest 2) : segmentation fault ! * **Warning** if sizeof(digest1) != sizeof(digest 2) : segmentation fault !
* Compare the two digest. * Compare the two digest.
*
*/ */
bool compareDigest(byte* digest1, byte* digest2, int size); bool compareDigest(byte* digest1, byte* digest2, int size);
@ -105,7 +111,7 @@ class HASHCrypt{
* @param sizeRequired : Digest size expected * @param sizeRequired : Digest size expected
* @param size : Given digest size * @param size : Given digest size
* *
* Throw an exception, and stop the programm if * Throw an exception, and stop the program if
* sizeRequired != size * sizeRequired != size
* Use getInvalidDigestSizeError method. * Use getInvalidDigestSizeError method.
*/ */
@ -113,7 +119,7 @@ class HASHCrypt{
/** /**
* @brief Make and error message. * @brief Make "invalid digest size" error message.
* *
* @param sizeRequired : Digest size expected * @param sizeRequired : Digest size expected
* @param size : Given digest size * @param size : Given digest size