Overview
Features
Download
Documentation
Community
Add-Ons & Services

how to use pbkdf2 and hmac_sha1 using Poco's crypto lib

Please post support and help requests here.

how to use pbkdf2 and hmac_sha1 using Poco's crypto lib

Postby HemDutt » 26 Jun 2014, 12:53

I have built crypto library on Mac OS 10.8 with POCO.

I want to generate pbkdf2 of a string and then using that hmac_sha1 hash. I did not found any documentation or sample code for this.
I tried PKCS5_PBKDF2_HMAC_SHA1 function in openssl/evp.h but it gave error that definition not found.
HemDutt
 
Posts: 11
Joined: 24 Jun 2014, 14:21

Re: how to use pbkdf2 and hmac_sha1 using Poco's crypto lib

Postby alex » 28 Jun 2014, 01:38

alex
 
Posts: 1102
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: how to use pbkdf2 and hmac_sha1 using Poco's crypto lib

Postby HemDutt » 30 Jun 2014, 11:13

Thanks Alex for the reply.

But I did not found PBKDF2Engine.h in Poco folder, although HMACEngine.h and SHA1Engine.h were present. Any idea why that could be?
HemDutt
 
Posts: 11
Joined: 24 Jun 2014, 14:21

Re: how to use pbkdf2 and hmac_sha1 using Poco's crypto lib

Postby alex » 30 Jun 2014, 13:03

It will be in 1.5.3 release
alex
 
Posts: 1102
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: how to use pbkdf2 and hmac_sha1 using Poco's crypto lib

Postby HemDutt » 03 Jul 2014, 13:08

Thanks Alex,

I did this to accomplish the task.
Added libcrypto and libssl in the XCode project

Code: Select all
#include "openssl/evp.h"
#include <openssl/sha.h>
#include <openssl/hmac.h>

#define KEY_LEN  len

char pbkdf2String[2*KEY_LEN];
char mdString[40];

char* pbkdf2KeyGenerator(const char* password , const unsigned char* salt, int iterations)
{
    size_t i;
    unsigned char *out;
   
    out = (unsigned char *) malloc(sizeof(unsigned char) * KEY_LEN);
       
    if( PKCS5_PBKDF2_HMAC_SHA1(password, strlen(password), salt, strlen((char*)salt), iterations, KEY_LEN, out) != 0 )
    {
        for(i=0;i<KEY_LEN;i++)
        {
            sprintf(&pbkdf2String[i*2], "%02x", (unsigned int)out[i]);
        }
    }
    else
    {
        fprintf(stderr, "PKCS5_PBKDF2_HMAC_SHA1 failed\n");
    }
   
    free(out);
   
    return pbkdf2String;
}

char* calculateHmacSha1Hash(const char* password, const char* salt, const char* data, int iterations)
{
    // The key to hash
    std::string saltStr(salt);
    const unsigned char* unsignedSalt = (unsigned char*)saltStr.c_str();
   
    char* key = pbkdf2KeyGenerator(password,unsignedSalt,iterations);
   
    // The data that we're going to hash using HMAC
    std::string dataVal(data);
    const unsigned char* unsignedData = (unsigned char*)dataVal.c_str();
   
    unsigned char* digest;
   
    // Using sha1 hash engine here.
    digest = HMAC(EVP_sha1(), key, strlen(key), unsignedData, strlen((char *)unsignedData), NULL, NULL);
   
    // Be careful of the length of string with the choosen hash engine. SHA1 produces a 20-byte hash value which rendered as 40 characters.
    // Change the length accordingly with choosen hash engine
   
    for(int i = 0; i < 20; i++)
    {
        sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
    }
   
    return mdString;
}
HemDutt
 
Posts: 11
Joined: 24 Jun 2014, 14:21


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest