Tuesday, November 13, 2007

Hash (ing) with OpenSSL


We can compute hash with OpenSSL Library. Hash method supported by OpenSSL are SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, MD5, MD4 and others.


Below is Simple example for utilize this library:
  1. Declare Used Variable:
    EVP_MD_CTX mdctx;
    const EVP_MD *md;

  2. Initialize Variable:
    md = EVP_get_digestbyname("sha1");
    EVP_MD_CTX_init(&mdctx);

  3. Initialize Hash Process:
    EVP_DigestInit_ex(&mdctx, md, NULL);

  4. Adding data to be hashed:
    EVP_DigestUpdate(&mdctx, (void*)"data-1", strlen("data-1"));

  5. Step above can be repeated so all data has been added

  6. Finish Hash and get the Hash Result:
    unsigned char *HashResult;
    int HashLen = EVP_MD_size(md);
    HashResult = new unsigned char[HashLen];
    EVP_DigestFinal_ex(&mdctx, HashResult , &HashLen);

Hash Result saved in HashResult Variable.

1 comments:

Anonymous said...

Amiable post and this fill someone in on helped me alot in my college assignement. Gratefulness you seeking your information.