Click here to Skip to main content
11,802,442 members (72,862 online)
Click here to Skip to main content

Using hashlib++ for easily creating cryptographic checksums such as SHA1 and MD5

, 22 Sep 2007 CPOL 23K 474 13
Rate this:
Please Sign up or sign in to vote.
This short article explains how to create a cryptographic checksum (a hash) with the help of the hashlib++ library.


This short article explains how to create a cryptographic checksum (a hash) with the help of the hashlib++ library. hashlib++ is a simple and very easy to use library to create a cryptographic checksum called "hash". The library is written in plain C++, and should work with every compiler and platform. hashlib++ is released under the BSD-license and is therefore free software.

Using the Code

hashlib++ provides the so called "wrappers" for each supported hash function which simplifies the creation of the relevant hash. Instead of implementing the full algorithm for the hash function, you only have to instantiate a desired wrapper and call a member function like getHashFromString() or getHashFromFile().

After downloading the small library from the project's website (, you have to include the base class "hashwrapper.h" and the header file of the wrapper you want to use:

#include "hashwrapper.h"
#include "sha1wrapper.h"
#include "md5wrapper.h"

After that, you can create wrapper objects:

hashwrapper *md5 = new md5wrapper();
hashwrapper *sha1 = new sha1wrapper();

Once a wrapper has been instantiated, you can basically call the member functions getHashFromFile() and getHashFromString() to create a hash from a file or string.

std::string mytexthash = md5->getHashFromString("Hello World");
std::string myfilehash = md5->getHashFromFile("README.TXT");

And that's all!

delete md5;
delete sha1;

Now you can add the corresponding *.cpp files (for MD5, for example: md5.cpp and md5wrapper.cpp) to your project and start compiling.

I have attached libtest.cpp, which is a full example of how to use hashlib++.

Have fun!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralProblem same as BluePain Pin
Masoud Gh8-Nov-10 20:00
memberMasoud Gh8-Nov-10 20:00 
GeneralRe: Problem same as BluePain Pin
gripmaster8-Nov-10 20:18
membergripmaster8-Nov-10 20:18 
Generalwords with accents Pin
ale_nehurotykos19-Feb-09 8:14
memberale_nehurotykos19-Feb-09 8:14 
QuestionWhats wrong? Pin
BluePain14-Nov-07 8:04
memberBluePain14-Nov-07 8:04 
Whats wrong? I get this error when compling in Visual Studio 2005
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall sha1wrapper::sha1wrapper(void)" (??0sha1wrapper@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall md5wrapper::md5wrapper(void)" (??0md5wrapper@@QAE@XZ) referenced in function _main
1>C:\Documents and Settings\Administrator\Mine dokumenter\Visual Studio 2005\Projects\bummer\Debug\bummer.exe : fatal error LNK1120: 2 unresolved externals

and this when I do it in DEV-C++:
[Linker error] undefined reference to `md5wrapper::md5wrapper()'
[Linker error] undefined reference to `sha1wrapper::sha1wrapper()'
ld returned 1 exit status
C:\Dev-Cpp\Prog\md5_\ [Build Error] [md5_.exe] Error 1
AnswerRe: Whats wrong? Pin
gripmaster18-Nov-07 22:54
membergripmaster18-Nov-07 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 22 Sep 2007
Article Copyright 2007 by gripmaster
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid