RTSha256Digest.cpp revision cbf12315646559bf0cb5f12957f6096cb73e30f4
af062818b47340eef15700d2f0211576ba3506eevboxsync/** @file
af062818b47340eef15700d2f0211576ba3506eevboxsync * IPRT - SHA256 digest creation
af062818b47340eef15700d2f0211576ba3506eevboxsync */
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync/*
af062818b47340eef15700d2f0211576ba3506eevboxsync * Copyright (C) 2009-2013 Oracle Corporation
af062818b47340eef15700d2f0211576ba3506eevboxsync *
af062818b47340eef15700d2f0211576ba3506eevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
af062818b47340eef15700d2f0211576ba3506eevboxsync * available from http://www.virtualbox.org. This file is free software;
af062818b47340eef15700d2f0211576ba3506eevboxsync * you can redistribute it and/or modify it under the terms of the GNU
af062818b47340eef15700d2f0211576ba3506eevboxsync * General Public License (GPL) as published by the Free Software
af062818b47340eef15700d2f0211576ba3506eevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
af062818b47340eef15700d2f0211576ba3506eevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
af062818b47340eef15700d2f0211576ba3506eevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
af062818b47340eef15700d2f0211576ba3506eevboxsync *
af062818b47340eef15700d2f0211576ba3506eevboxsync * The contents of this file may alternatively be used under the terms
af062818b47340eef15700d2f0211576ba3506eevboxsync * of the Common Development and Distribution License Version 1.0
af062818b47340eef15700d2f0211576ba3506eevboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
af062818b47340eef15700d2f0211576ba3506eevboxsync * VirtualBox OSE distribution, in which case the provisions of the
af062818b47340eef15700d2f0211576ba3506eevboxsync * CDDL are applicable instead of those of the GPL.
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync *
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync * You may elect to license modified versions of this file under the
4b9d6701570cb98fd36e209314239d104ec584d3vboxsync * terms and conditions of either the GPL or the CDDL or both.
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync */
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync/*******************************************************************************
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync* Header Files *
b955672b950093ff7416d1269dd4d3b69983bd8fvboxsync*******************************************************************************/
af062818b47340eef15700d2f0211576ba3506eevboxsync#include "internal/iprt.h"
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/sha.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/alloca.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/assert.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/mem.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/string.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <iprt/file.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync#include <openssl/sha.h>
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsyncRTR3DECL(int) RTSha256Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
af062818b47340eef15700d2f0211576ba3506eevboxsync{
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Validate input */
af062818b47340eef15700d2f0211576ba3506eevboxsync AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
af062818b47340eef15700d2f0211576ba3506eevboxsync AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
af062818b47340eef15700d2f0211576ba3506eevboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync int rc = VINF_SUCCESS;
af062818b47340eef15700d2f0211576ba3506eevboxsync *ppszDigest = NULL;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Initialize OpenSSL. */
af062818b47340eef15700d2f0211576ba3506eevboxsync SHA256_CTX ctx;
af062818b47340eef15700d2f0211576ba3506eevboxsync if (!SHA256_Init(&ctx))
af062818b47340eef15700d2f0211576ba3506eevboxsync return VERR_INTERNAL_ERROR;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Buffer size for progress callback */
af062818b47340eef15700d2f0211576ba3506eevboxsync double rdMulti = 100.0 / cbBuf;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Working buffer */
af062818b47340eef15700d2f0211576ba3506eevboxsync char *pvTmp = (char*)pvBuf;
af062818b47340eef15700d2f0211576ba3506eevboxsync
589fd26cedb2b4ebbed14f2964cad03cc8ebbca2vboxsync /* Process the memory in blocks */
af062818b47340eef15700d2f0211576ba3506eevboxsync size_t cbRead;
af062818b47340eef15700d2f0211576ba3506eevboxsync size_t cbReadTotal = 0;
af062818b47340eef15700d2f0211576ba3506eevboxsync for (;;)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync cbRead = RT_MIN(cbBuf - cbReadTotal, _1M);
af062818b47340eef15700d2f0211576ba3506eevboxsync if(!SHA256_Update(&ctx, pvTmp, cbRead))
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync rc = VERR_INTERNAL_ERROR;
af062818b47340eef15700d2f0211576ba3506eevboxsync break;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync cbReadTotal += cbRead;
af062818b47340eef15700d2f0211576ba3506eevboxsync pvTmp += cbRead;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Call the progress callback if one is defined */
af062818b47340eef15700d2f0211576ba3506eevboxsync if (pfnProgressCallback)
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
af062818b47340eef15700d2f0211576ba3506eevboxsync if (RT_FAILURE(rc))
af062818b47340eef15700d2f0211576ba3506eevboxsync break; /* canceled */
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Finished? */
af062818b47340eef15700d2f0211576ba3506eevboxsync if (cbReadTotal == cbBuf)
af062818b47340eef15700d2f0211576ba3506eevboxsync break;
af062818b47340eef15700d2f0211576ba3506eevboxsync }
af062818b47340eef15700d2f0211576ba3506eevboxsync if (RT_SUCCESS(rc))
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync /* Finally calculate & format the SHA256 sum */
af062818b47340eef15700d2f0211576ba3506eevboxsync unsigned char auchDig[RTSHA256_HASH_SIZE];
af062818b47340eef15700d2f0211576ba3506eevboxsync if (!SHA256_Final(auchDig, &ctx))
af062818b47340eef15700d2f0211576ba3506eevboxsync return VERR_INTERNAL_ERROR;
af062818b47340eef15700d2f0211576ba3506eevboxsync
af062818b47340eef15700d2f0211576ba3506eevboxsync char *pszDigest;
af062818b47340eef15700d2f0211576ba3506eevboxsync rc = RTStrAllocEx(&pszDigest, RTSHA256_DIGEST_LEN + 1);
af062818b47340eef15700d2f0211576ba3506eevboxsync if (RT_SUCCESS(rc))
af062818b47340eef15700d2f0211576ba3506eevboxsync {
af062818b47340eef15700d2f0211576ba3506eevboxsync rc = RTSha256ToString(auchDig, pszDigest, RTSHA256_DIGEST_LEN + 1);
af062818b47340eef15700d2f0211576ba3506eevboxsync if (RT_SUCCESS(rc))
af062818b47340eef15700d2f0211576ba3506eevboxsync *ppszDigest = pszDigest;
else
RTStrFree(pszDigest);
}
}
return rc;
}
RTR3DECL(int) RTSha256DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
{
/* Validate input */
AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
*ppszDigest = NULL;
/* Initialize OpenSSL. */
SHA256_CTX ctx;
if (!SHA256_Init(&ctx))
return VERR_INTERNAL_ERROR;
/* Open the file to calculate a SHA256 sum of */
RTFILE hFile;
int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
if (RT_FAILURE(rc))
return rc;
/* Fetch the file size. Only needed if there is a progress callback. */
double rdMulti = 0;
if (pfnProgressCallback)
{
uint64_t cbFile;
rc = RTFileGetSize(hFile, &cbFile);
if (RT_FAILURE(rc))
{
RTFileClose(hFile);
return rc;
}
rdMulti = 100.0 / cbFile;
}
/* Allocate a reasonably large buffer, fall back on a tiny one. */
void *pvBufFree;
size_t cbBuf = _1M;
void *pvBuf = pvBufFree = RTMemTmpAlloc(cbBuf);
if (!pvBuf)
{
cbBuf = 0x1000;
pvBuf = alloca(cbBuf);
}
/* Read that file in blocks */
size_t cbRead;
size_t cbReadTotal = 0;
for (;;)
{
rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead);
if (RT_FAILURE(rc) || !cbRead)
break;
if(!SHA256_Update(&ctx, pvBuf, cbRead))
{
rc = VERR_INTERNAL_ERROR;
break;
}
cbReadTotal += cbRead;
/* Call the progress callback if one is defined */
if (pfnProgressCallback)
{
rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
if (RT_FAILURE(rc))
break; /* canceled */
}
}
RTMemTmpFree(pvBufFree);
RTFileClose(hFile);
if (RT_FAILURE(rc))
return rc;
/* Finally calculate & format the SHA256 sum */
unsigned char auchDig[RTSHA256_HASH_SIZE];
if (!SHA256_Final(auchDig, &ctx))
return VERR_INTERNAL_ERROR;
char *pszDigest;
rc = RTStrAllocEx(&pszDigest, RTSHA256_DIGEST_LEN + 1);
if (RT_SUCCESS(rc))
{
rc = RTSha256ToString(auchDig, pszDigest, RTSHA256_DIGEST_LEN + 1);
if (RT_SUCCESS(rc))
*ppszDigest = pszDigest;
else
RTStrFree(pszDigest);
}
return rc;
}