md5wrapper.cpp
Go to the documentation of this file.
00001 /*
00002  *      This is part of my wrapper-class to create
00003  *      a MD5 Hash from a string and a file.
00004  *
00005  *      This code is completly free, you 
00006  *      can copy it, modify it, or do 
00007  *      what ever you want with it.
00008  *
00009  *      Feb. 2005
00010  *      Benjamin Gr¨ądelbach
00011  */
00012 
00013 //---------------------------------------------------------------------- 
00014 //basic includes
00015 #include <iostream>
00016 #include <sstream>
00017 using namespace std;
00018 
00019 //my includes
00020 #include "md5wrapper.h"
00021 #include "md5.h"
00022 #include <iomanip>
00023 
00024 
00025 //---------privates--------------------------
00026 
00027 /*
00028  * internal hash function, calling
00029  * the basic methods from md5.h
00030  */     
00031 std::string md5wrapper::hashit(std::string text)
00032 {
00033         MD5_CTX ctx;
00034         
00035         //init md5
00036         md5->MD5Init(&ctx);
00037         //update with our string
00038         md5->MD5Update(&ctx,
00039                  (unsigned char*)text.c_str(),
00040                  text.length());
00041         
00042         //create the hash
00043         unsigned char buff[16] = "";    
00044         md5->MD5Final((unsigned char*)buff,&ctx);
00045 
00046         //converte the hash to a string and return it
00047         return convToString(buff);      
00048 }
00049 
00050 /*
00051  * converts the numeric hash to
00052  * a valid std::string.
00053  * (based on Jim Howard's code;
00054  * http://www.codeproject.com/cpp/cmd5.asp)
00055  */
00056 std::string md5wrapper::convToString(unsigned char *bytes)
00057 {
00058         stringstream out;
00059 
00060         //out.setf ( ios::hex, ios::basefield );       // set hex as the basefield
00061         //out.width(2);
00062 
00063         for(int i=0; i<16; i++)
00064         {
00065                 out << setfill('0') << setw(2) << hex << bytes[i];
00066         }       
00067         
00068         return out.str();
00069 }
00070 
00071 //---------publics--------------------------
00072 
00073 //constructor
00074 md5wrapper::md5wrapper()
00075 {
00076         md5 = new MD5();
00077 }
00078 
00079 
00080 //destructor
00081 md5wrapper::~md5wrapper()
00082 {
00083         delete md5;
00084 }
00085 
00086 /*
00087  * creates a MD5 hash from
00088  * "text" and returns it as
00089  * string
00090  */     
00091 std::string md5wrapper::getHashFromString(std::string text)
00092 {
00093         return this->hashit(text); 
00094 }
00095 
00096 
00097 
00098 
00099 /*
00100  * EOF
00101  */


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29