Sample code for 30+ languages & platforms
C

Hash File: SHA-1, HAVAL, MD2, MD5, SHA-256, SHA-384, SHA-512

See more Encryption Examples

Computing the hash for a file of any size.

Chilkat C Downloads

C
#include <C_CkCrypt2.h>

void ChilkatSample(void)
    {
    HCkCrypt2 crypt;
    const char *filename;
    const char *hash;

    //  This example assumes the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    crypt = CkCrypt2_Create();

    //  Any type of file may be hashed.
    //  There is  no size limitation because the file is consumed
    //  in streaming mode internally.

    filename = "something.zip";

    CkCrypt2_putHashAlgorithm(crypt,"sha1");
    CkCrypt2_putEncodingMode(crypt,"hex");

    //  Other possible EncodingMode settings are: 
    //  "quoted-printable", "base64", "base32", and "url";

    hash = CkCrypt2_hashFileENC(crypt,filename);
    printf("SHA1:\n");
    printf("%s\n",hash);

    //  Hash using MD2
    CkCrypt2_putHashAlgorithm(crypt,"md2");
    hash = CkCrypt2_hashFileENC(crypt,filename);
    printf("MD2:\n");
    printf("%s\n",hash);

    //  Hash using MD5
    CkCrypt2_putHashAlgorithm(crypt,"md5");
    hash = CkCrypt2_hashFileENC(crypt,filename);
    printf("MD5:\n");
    printf("%s\n",hash);

    //  Hash using SHA-256
    CkCrypt2_putHashAlgorithm(crypt,"sha256");
    hash = CkCrypt2_hashFileENC(crypt,filename);
    printf("SHA256:\n");
    printf("%s\n",hash);

    //  Hash using SHA-384
    CkCrypt2_putHashAlgorithm(crypt,"sha384");
    hash = CkCrypt2_hashFileENC(crypt,filename);
    printf("SHA384:\n");
    printf("%s\n",hash);

    //  Hash using SHA-512
    CkCrypt2_putHashAlgorithm(crypt,"sha512");
    hash = CkCrypt2_hashFileENC(crypt,filename);
    printf("SHA512:\n");
    printf("%s\n",hash);

    //  Hash using HAVAL
    //  There are two additional properties relevant to HAVAL:
    //  HavalRounds, and KeyLength.
    //  HavalRounds can have values of 3, 4, or 5.
    //  KeyLength can have values of 128, 160, 192, 224, or 256
    CkCrypt2_putHashAlgorithm(crypt,"haval");
    CkCrypt2_putHavalRounds(crypt,5);
    CkCrypt2_putKeyLength(crypt,256);
    hash = CkCrypt2_hashFileENC(crypt,filename);
    printf("Haval:\n");
    printf("%s\n",hash);


    CkCrypt2_Dispose(crypt);

    }