Advertisement

04.04.2008 at 03:14AM PDT, ID: 23295501
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Rewrite function to work with AnsiString

Tags: Borland C++ Builder 6
Hi,

Found a snippet which allows you to convert char* into a MD5 hash.

Since I am coding in Borland C++ with all its noobish objects like AnsiString.
And since I'm newbish at the rewriting-the-code-to-work-like-you-want-it thing I am asking for help here.

I need this function to return the hash in AnsiString format. Also the parameters of the function will only be AnsiString

For example:
AnsiString Str = "This is my string";
AnsiString Hash = MD5(Str);
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
#include <wincrypto.h>
 
bool md5(unsigned char hash[16], unsigned char *buf, int len)
{
    HCRYPTPROV hCryptProv;
    HCRYPTHASH hHash;
    BYTE bHash[0x7f];
    DWORD dwHashLen = 16;
    DWORD cbContent = len;
    BYTE* pbContent = (BYTE*)buf;
 
    if(CryptAcquireContext(&hCryptProv,
        NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
    {
 
        if(CryptCreateHash(hCryptProv,
            CALG_MD5,    
            0, 0, &hHash))
        {
            if(CryptHashData(hHash, pbContent, cbContent, 0))
            {
                if(CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLen, 0))
                {
                    memcpy(hash, bHash, 16);
                }
                else return false;
            }
            else return false;
        }
        else return false;
    }
    else return false;
 
    CryptDestroyHash(hHash);
    CryptReleaseContext(hCryptProv, 0);
 
    return true;
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: Jammer1224
Solution Provided By: Kdo
Participating Experts: 1
Solution Grade: A
Views: 66
Translate:
Loading Advertisement...
04.04.2008 at 05:10AM PDT, ID: 21280779

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.04.2008 at 05:28AM PDT, ID: 21280896

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.04.2008 at 05:35AM PDT, ID: 21280957

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.04.2008 at 06:00AM PDT, ID: 21281155

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.04.2008 at 06:20AM PDT, ID: 21281339

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.04.2008 at 07:37AM PDT, ID: 21282186

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.04.2008 at 10:20AM PDT, ID: 21283791

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.04.2008 at 01:58PM PDT, ID: 21285606

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
04.05.2008 at 06:50PM PDT, ID: 21290370

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
04.04.2008 at 05:10AM PDT, ID: 21280779

Rank: Master

Hi Jammer1224,

It will probably make the most sense if you change the actual code as little as possible.  That's pretty easy to accommodate by changing the header and moving necessary items to stack variables.  

I've not tested it, but something like I've attached below should be a good start.

C++ Builder handles conventional strings just fine (char Array[]).  Why do you need to return an AnsiString?


Good Luck,
Kent
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
AnsiString md5 (AnsiString Source)
{ 
  HCRYPTPROV hCryptProv;
  HCRYPTHASH hHash;
  BYTE bHash[0x7f];
  DWORD dwHashLen = 16;
  DWORD cbContent = len;
  BYTE* pbContent = (BYTE*)buf;  
//  New variables -- originally on the function's parameter list 
  unsigned char hash [16+1];// The buffer where the md5 hash will be built
  unsigned char *buf;       // Originally this was the buffer to hash
  int      len;             // Original buffer length; 
  len = Source.Length ();
  buf = (unsigned char*)Source.c_ptr (); 
  if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
  {
    if (CryptCreateHash (hCryptProv, CALG_MD5, 0, 0, &hHash))
    {
      if (CryptHashData (hHash, pbContent, cbContent, 0))
      {
        if (CryptGetHashParam (hHash, HP_HASHVAL, bHash, &dwHashLen, 0))
        {
          memcpy(hash, bHash, 16);
        }
        else return "";
      }
      else return "";
    }
    else return "";
  }
  else return "";
 
  CryptDestroyHash(hHash);
  CryptReleaseContext(hCryptProv, 0);
 
  hash[16] = 0;   // Append a stringterminator to the hash 
  return (AnsiString) (hash);
}
Open in New Window
 
04.04.2008 at 05:28AM PDT, ID: 21280896
Because I'm not good at converting variables from one type to another.
It took me 2 days to figure out how to convert a number stored in a string to a int :)
First of all, to correct something that you did wrong, old variables must be declared below the new ones.
Also the typing mistake you made:
buf = (unsigned char*)Source.c_ptr ();
It should be:
buf = (unsigned char*)Source.c_str ();

Stating these mistakes in case if someone more novice then me looks for solution like this.

Secondly, I get a compiler error:
[C++ Error] Unit1.cpp(208): E2285 Could not find a match for 'AnsiString::AnsiString(unsigned char *)'
[C++ Error] Unit1.cpp(208): E2031 Cannot cast from 'unsigned char *' to 'AnsiString'

@ return (AnsiString) (hash);


 
04.04.2008 at 05:35AM PDT, ID: 21280957

Rank: Master

Hi Jammer1224,

c_ptr / c_str ...  I've been using C++Builder since it first came out and I still do that in my own code about 1/2 the time.   :)


The error must be on the last line.  We'll just recast twice.

    return (AnsiString)(char *)(hash);


Note that you can also call the original function, and preform just this double recast on the returned buffer hash[].  Though you'll have to take the same caution to ensure that it is null terminated.



Good Luck,
Kent
 
04.04.2008 at 06:00AM PDT, ID: 21281155
It returns me an:

[C++ Error] Unit1.cpp(183): E2316 'c_ptr' is not a member of 'AnsiString'

when using ptr

When I use c_str the hash looks awfull.
the bHash looks even worse... (see picture attached)


Maby thats because I use c_str not c_ptr?

If not, maby you see the problem of this snippet?
I really need to add MD5 somehow to the code I am writing.

I do not know how to use DLL's (but I have one which has a MD5 convertion function in it).

Also I have got some cc files which could be used to add a MD5 function to the, but again, I have no knowledge how to use them.

The cc files can be found:
http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html
Or direct: http://userpages.umbc.edu/~mabzug1/cs/md5/md5-cc-102.tar.gz
 
Values of the hashes.
Values of the hashes.
 
 
04.04.2008 at 06:20AM PDT, ID: 21281339

Rank: Master

Hi Jammer1224,

An md5 hash is really a 16*8 bit  (128) bit value.  You can not directly display the AnsiString value where you've put it.  Instead, you'll need to convert each 4-bit value to hex, just as if it were in a char[16].

I think that you'll be better off if we start back at the ground level and rework this.  An md5 value in an AnsiString may be "portable", but it's not much good for anything else.

A lightweight wrapper class around the md5 algorithm(s) might be the right way to go.



Good Luck,
Kent
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
class mymd5
{
  public:
    bool md5 (AnsiString);
    char DisplayValue [17]; 
  private:
    unsigned char hash[16];
    bool     Success;
}; 
bool mymd5::md5 (AnsiString Source)
{
  Success = md5 (hash, (unsigned char*)Source.c_str (), Source.Length);
  if (Success)
  {
    for (int idx = 0; unsigned char *ptr = hash; ++idx, ++ptr)
    {
      DisplayValue[idx*2] = (char*)"0123456789ABCDEF"[(*ptr)>>4];
      DisplayValue[idx*2+1] = (char*)"0123456789ABCDEF"[(*ptr)& 0xF)];
    }
    DisplayValue[16] = 0;
  }
  else
    DispalyValue[0] = 0;
  return Success;
}
Open in New Window
Accepted Solution
 
04.04.2008 at 07:37AM PDT, ID: 21282186
There must be a mistake somewhere in :
    for (int idx = 0; unsigned char *ptr = hash; ++idx, ++ptr)
    {
      DisplayValue[idx*2] = (char *)"0123456789ABCDEF"[(*ptr)>>4];
      DisplayValue[idx*2+1] = (char *)"0123456789ABCDEF"[(*ptr)&0xF];
    }

I guess we need to use (char).
If using char i get an access error when building without debug.

When debugging seems that the DisplayValue gets A2A2A2.....A2A2A2A2.....
and the hash value gets:
hash = hash + DisplayValue...
Also the loop seems to be infinite.
 
04.04.2008 at 10:20AM PDT, ID: 21283791

Rank: Master

Hi Jammer1224,

Ok.  I'd used that before so I guess that I mistyped something.

Create an array with the hex digits.

  static const char HexDigit[16] = "0123456789ABCDEF";


Then reference it

      for (int idx = 0; unsigned char *ptr = hash; ++idx, ++ptr)
      {
        DisplayValue[idx*2] = HexDigit [(*ptr)>>4];
        DisplayValue[idx*2+1] = HexDigit [(*ptr)&0xF];
      }


Good Luck,
Kent
 
04.04.2008 at 01:58PM PDT, ID: 21285606
No still does not work.
Still an access point error.
I'm using it this way because I do not know how to use classes.
Both functions declared in the Form class.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
String TForm1::MD5(AnsiString Source)
{
        unsigned char hash[16];
        bool     Success;
        char DisplayValue [17];
  Success = MD5Hex(hash, (unsigned char*)Source.c_str (), Source.Length());
  if (Success)
  {
  static const char HexDigit[16] = "0123456789ABCDEF";
      for (int idx = 0; unsigned char *ptr = hash; ++idx, ++ptr)
      {
        DisplayValue[idx*2] = HexDigit [(*ptr)>>4];
        DisplayValue[idx*2+1] = HexDigit [(*ptr)&0xF];
      }
 
    DisplayValue[16] = 0;
    return (String)DisplayValue;
  }
  return "";
}
bool TForm1::MD5Hex(unsigned char hash[16], unsigned char *buf, int len)
{
    HCRYPTPROV hCryptProv;
    HCRYPTHASH hHash;
    BYTE bHash[0x7f];
    DWORD dwHashLen = 16;
    DWORD cbContent = len;
    BYTE* pbContent = (BYTE*)buf;
 
    if(CryptAcquireContext(&hCryptProv,
        NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
    {
 
        if(CryptCreateHash(hCryptProv,
            CALG_MD5,    //  wincrypt.h
            0, 0, &hHash))
        {
            if(CryptHashData(hHash, pbContent, cbContent, 0))
            {
                if(CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLen, 0))
                {
                    memcpy(hash, bHash, 16);
                }
                else return false;
            }
            else return false;
        }
        else return false;
    }
    else return false;
 
    CryptDestroyHash(hHash);
    CryptReleaseContext(hCryptProv, 0);
    return true;
}
Open in New Window
 
04.05.2008 at 06:50PM PDT, ID: 21290370
Edited your code a bit and it works. Thanks.
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628