Click here to Skip to main content
Licence CPOL
First Posted 22 Sep 2007
Views 16,940
Downloads 178
Bookmarked 11 times

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

By | 22 Sep 2007 | Article
This short article explains how to create a cryptographic checksum (a hash) with the help of the hashlib++ library.

Introduction

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 (http://hashlib2plus.sourceforge.net), 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!

License

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

About the Author

gripmaster



Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralProblem same as BluePain PinmemberMasoud Gh20:00 8 Nov '10  
GeneralRe: Problem same as BluePain Pinmembergripmaster20:18 8 Nov '10  
Generalwords with accents Pinmemberale_nehurotykos8:14 19 Feb '09  
QuestionWhats wrong? PinmemberBluePain8:04 14 Nov '07  
AnswerRe: Whats wrong? Pinmembergripmaster22:54 18 Nov '07  

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.

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