Monday, August 23, 2010

UUID Generator

Sometime when coding for ATL, i need to create manually Interface derived from IUnknown. Declaration of Interface derived from IUnknown like below:


[
object,
uuid(9465EAB0-AE53-11DF-94E2-0800200C9A66),
dual,
nonextensible,
helpstring("IInternal Interface"),
pointer_default(unique)
]
interface IInternal : IUnknown{
};


Look at the uuid field. It is a unique (hope) id for this interrface. Forming the uuid refers to RFC 4122. Fortunately, many people has offered a tool to generate it. You can use site below to generate it.


1. http://www.famkruithof.net/uuid/uuidgen
2. http://www.guidgenerator.com/
3. http://www.somacon.com/p113.php

Ok, hope it help!


Read More……

Tuesday, March 11, 2008

DoEvents in VC++

DoEvents just like on VB, sometime is a need for our code. You know, we use DoEvents in VB to let other control/process to do their update when we are in a loop code.


Read More……

Monday, December 17, 2007

Google Knol, Alternative knowledge sharring

Knol, a new inspiratif product from google to share a knowledge between us. this will add alternative media to share knowledge. Before we've used wikipedia, blog, and others to share our knowledge.

Google says that knol equipped with a number of tools to make the Author share the knowledge in an easy way. Google PageRank will be a weapon to know the value of the information shared.

So with this product, we can use for sharing our knowledge, create a FAQ for our System or others. Just be patient, because this tools still in testing and inviting mode only.


Read More……

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.


Read More……

Creating X509 Certificate using OpenSSL Library

OpenSSL Library is a very powerfull Library to Create many other things. From X509 Certificate, Message Signing, Message Encryption and Decryption, and others. And don't forget that OpenSSL library is distributed under Apache Style License. Detail License can be found here.

Below step by step to create Self Sign X509-Certificate:
  1. Initialize X509 Structure, sample:
    X509 *X509Cert = X509_new();

  2. Set X509 Certificate Version:
    X509_set_version(X509Cert, 0x2); // Set to V3

  3. Set Serial Number:
    ASN1_INTEGER_set(X509_get_serialNumber(X509Cert), 1);

  4. Set Issuer Certificate:
    X509_NAME_add_entry_by_txt(X509_get_issuer_name(X509Cert), "C", MBSTRING_ASC, (unsigned char *)"CC", -1, -1, 0);
    X509_NAME_add_entry_by_txt(X509_get_issuer_name(X509Cert), "O", MBSTRING_ASC, (unsigned char *)"Org", -1, -1, 0);
    X509_NAME_add_entry_by_txt(X509_get_issuer_name(X509Cert), "OU", MBSTRING_ASC, (unsigned char *)"OrgUnit", -1, -1, 0);
    X509_NAME_add_entry_by_txt(X509_get_issuer_name(X509Cert), "CN", MBSTRING_ASC, (unsigned char *)"CommonName", -1, -1, 0);

  5. Set Subject Certificate:
    X509_NAME_add_entry_by_txt(X509_get_subject_name(X509Cert), "C", MBSTRING_ASC, (unsigned char *)"CC", -1, -1, 0);
    X509_NAME_add_entry_by_txt(X509_get_subject_name(X509Cert), "O", MBSTRING_ASC, (unsigned char *)"Org", -1, -1, 0);
    X509_NAME_add_entry_by_txt(X509_get_subject_name(X509Cert), "OU", MBSTRING_ASC, (unsigned char *)"OrgUnit", -1, -1, 0);
    X509_NAME_add_entry_by_txt(X509_get_subject_name(X509Cert), "CN", MBSTRING_ASC, (unsigned char *)"CommonName", -1, -1, 0);

  6. Set the Validity of Certificate:
    X509_gmtime_adj(X509_get_notBefore(X509Cert), (long)60*60*24*Days); //Valid From, (start from now)
    X509_gmtime_adj(X509_get_notAfter(X509Cert), (long)60*60*24*Days); //Valid Until (start from now)

  7. Self Sign the Certificate:
    1. Generate RSA Key-pairs:
      EVP_PKEY *pkey = EVP_PKEY_new();
      RSA *rsa = RSA_generate_key(512, 65535, NULL, NULL); //RSA key: 512 bits
      EVP_PKEY_set1_RSA (pkey, rsa);
      X509_set_pubkey(X509Cert, pkey);

    2. Instantiate Digest Algorithm Object (SHA1/ SHA224/ SHA256/ SHA384/ SHA512):
      const EVP_MD *dgAlg = EVP_get_digestbyname("SHA1");

    3. Sign the Certificate:
      X509_sign(X509Cert, pkey, dgAlg);


  8. Save Certificate to File (DER Encoded : .cer):
    BIO *out = BIO_new_file("SampleCert.cer", "w");
    i2d_X509_bio(out, X509Cert);
    BIO_free(out);

  9. Save Corresponding Private Key-pairs to File (DER Encoded : .key):
    BIO *out = BIO_new_file("SampleCert.key", "w");
    i2d_PrivateKey_bio(out, pkey);
    BIO_free(out);

  10. Free up Certificate Variable:
    RSA_free(rsa);
    EVP_PKEY_free(pkey);
    X509_free(X509Cert);

All the code without verifying return value from each function to check whether thu function run successfully or not.


Read More……