• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/Windows/DcCrypto.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00003 This file is part of Beesnest.
00004 
00005 Beesnest is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 Beesnest is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with Beesnest; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 
00021 /**
00022  * DcCrypto.cpp: implementation of the CdcCrypto class.
00023  * for documentation see the header file.
00024  */
00025 #include "DcCrypto.h"
00026 
00027 #define NUM_OF_DIGITS   10
00028 #define NUM_OF_LETTERS  26
00029 
00030 HCRYPTPROV CdcCrypto::hProv;
00031 
00032 void CdcCrypto::Init ()
00033 {
00034     /* Get a handle to the default provider. */
00035     if (!CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL,
00036         CRYPT_VERIFYCONTEXT))
00037         throw CdcException ("Cannot get a handle to the default provider.");
00038 
00039 }
00040 
00041 
00042 void CdcCrypto::Destroy ()
00043 {
00044     CryptReleaseContext (hProv, 0);
00045 }
00046 
00047 
00048 CdcString CdcCrypto::GetRandomString (int len)
00049 {
00050     if (len >= BUFF_LEN) len = BUFF_LEN - 1;
00051     unsigned char temp [BUFF_LEN];
00052 
00053     /* Get a random buffer. */
00054     if (!CryptGenRandom (hProv, len, temp))
00055             throw CdcException ("Cannot generate random string.");
00056 
00057     /* Convert a to valid string. */
00058     for (int i = 0; i < len; ++i)
00059     {   /* I want upper case letters, lower case and digit. */
00060         temp[i] = temp[i] % ((NUM_OF_LETTERS * 2) + NUM_OF_DIGITS);
00061         if (temp[i] < NUM_OF_DIGITS) temp[i] += '0';
00062         else if (temp[i] < (NUM_OF_DIGITS + NUM_OF_LETTERS))
00063             temp[i] += 'A' - NUM_OF_DIGITS;
00064         else temp[i] += 'a' - (NUM_OF_DIGITS + NUM_OF_LETTERS);
00065     }
00066 
00067     temp[len] = '\0';
00068     return CdcString ((char*)temp);
00069 }
00070 
00071 CdcString  CdcCrypto::MakeHash (const CdcString& buff)
00072 {
00073     HCRYPTHASH hHash = 0;
00074 
00075     /* Create a hash object. */
00076     if (!CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash))
00077         throw CdcException ("Cannot create hash object.");
00078 
00079     /* Put the hash in a buffer. */
00080     if (!CryptHashData (hHash, (unsigned char*)buff.GetBuffer (),
00081         buff.GetLength (), 0))
00082         throw CdcException ("Cannot put the buffer into hash object.");
00083 
00084     /* Read the hash value. */
00085     unsigned char hash_buff [BUFF_LEN];
00086     DWORD len = BUFF_LEN;
00087     if (!CryptGetHashParam (hHash, HP_HASHVAL, hash_buff, &len, 0))
00088         throw CdcException ("Cannot read the hash value.");
00089 
00090     /* Destroy the hash object. */
00091     if (hHash) CryptDestroyHash(hHash);
00092 
00093     /* Convert hash to hex. */
00094     CdcString hex_hash;
00095     char temp[10];
00096     for (int i = 0; i < len; ++i)
00097     {
00098         sprintf (temp, "%2x", hash_buff[i]);
00099         if (temp[0] == ' ') temp[0] = '0';
00100         hex_hash += temp;
00101     }
00102     return hex_hash;
00103 }

Generated on Mon Oct 11 2010 16:23:25 for Beesnest by  doxygen 1.7.2