Results 1 to 4 of 4

Thread: Need Help Getting SHA1 value of File

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    2

    Need Help Getting SHA1 value of File

    I'm trying to write a routine to get the SAH1 hash value of graphics and video files. Can find lots of docs on how to hash a password but none on how to
    get the hash of a file. It appears that GetHashFromFile is what I need to use
    but I could use some example code. Am using VB.Net 2003
    Thanks,
    Dave

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Need Help Getting SHA1 value of File

    I have a code in C

    #include <stdio.h>
    #include <windows.h>
    #include <wincrypt.h>

    #define BUFSIZE 1024
    #define MD5LEN 16

    DWORD main(int argc, char *argv[])
    {
    DWORD dwStatus = 0;
    BOOL bResult = FALSE;
    HCRYPTPROV hProv = 0;
    HCRYPTHASH hHash = 0;
    HANDLE hFile = NULL;
    BYTE rgbFile[BUFSIZE];
    DWORD cbRead = 0;
    BYTE rgbHash[MD5LEN];
    DWORD cbHash = 0;
    CHAR rgbDigits[] = "0123456789abcdef";

    //Logic to check usage goes here.

    hFile = CreateFile(argv[1],
    GENERIC_READ,
    FILE_SHARE_READ,
    NULL,
    OPEN_EXISTING,
    FILE_FLAG_SEQUENTIAL_SCAN,
    NULL);

    if (INVALID_HANDLE_VALUE == hFile)
    {
    dwStatus = GetLastError();
    printf("Error opening file %s\nError: %d\n", argv[1], dwStatus);
    return dwStatus;
    }

    //Get handle to the crypto provider
    if (!CryptAcquireContext(&hProv,
    NULL,
    NULL,
    PROV_RSA_FULL,
    CRYPT_VERIFYCONTEXT))
    {
    dwStatus = GetLastError();
    printf("CryptAcquireContext failed: %d\n", dwStatus);
    CloseHandle(hFile);
    return dwStatus;
    }

    if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
    {
    dwStatus = GetLastError();
    printf("CryptAcquireContext failed: %d\n", dwStatus);
    CloseHandle(hFile);
    CryptReleaseContext(hProv, 0);
    return dwStatus;
    }

    while (bResult = ReadFile(hFile, rgbFile, BUFSIZE, &cbRead, NULL))
    {
    if (0 == cbRead)
    {
    break;
    }

    if (!CryptHashData(hHash, rgbFile, cbRead, 0))
    {
    dwStatus = GetLastError();
    printf("CryptHashData failed: %d\n", dwStatus);
    CryptReleaseContext(hProv, 0);
    CryptDestroyHash(hHash);
    CloseHandle(hFile);
    return dwStatus;
    }
    }

    if (!bResult)
    {
    dwStatus = GetLastError();
    printf("ReadFile failed: %d\n", dwStatus);
    CryptReleaseContext(hProv, 0);
    CryptDestroyHash(hHash);
    CloseHandle(hFile);
    return dwStatus;
    }

    cbHash = MD5LEN;
    if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
    {
    printf("MD5 hash of file %s is: ", argv[1]);
    for (DWORD i = 0; i < cbHash; i++)
    {
    printf("%c%c", rgbDigits[rgbHash[i] >> 4],
    rgbDigits[rgbHash[i] & 0xf]);
    }
    printf("\n");
    }
    else
    {
    dwStatus = GetLastError();
    printf("CryptGetHashParam failed: %d\n", dwStatus);
    }

    CryptDestroyHash(hHash);
    CryptReleaseContext(hProv, 0);
    CloseHandle(hFile);

    return dwStatus;
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    2

    Re: Need Help Getting SHA1 value of File

    C code doesn't help me, don't understand it
    I was hoping that getting the hash value of a file would be more common
    nowadays!
    Thanks though,
    Dave

  4. #4
    Junior Member
    Join Date
    Aug 2005
    Posts
    27

    Re: Need Help Getting SHA1 value of File

    Here you go. Takes a file and generates a SHA1 hash for it.
    VB Code:
    1. Dim f As New System.IO.FileStream([b]FILENAME[/b], IO.FileMode.Open)
    2. Dim sha As New System.Security.Cryptography.SHA1CryptoServiceProvider
    3. Dim hash As Array
    4. Dim shaHash as String
    5. Dim sb As New System.Text.StringBuilder
    6.  
    7. sha.ComputeHash(f)
    8. hash = sha.Hash
    9. For Each hashByte As Byte In hash
    10.     sb.Append(String.Format("{0:X1}", hashByte))
    11. Next
    12. shaHash = sb.ToString
    13. f.Close()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width