cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * IPRT - SHA256 digest creation
ed6697871be690c5826c252d598ea9965c2816b4vboxsync * @todo Replace this with generic RTCrDigest based implementation. Too much
ed6697871be690c5826c252d598ea9965c2816b4vboxsync * stupid code duplication.
ed6697871be690c5826c252d598ea9965c2816b4vboxsync * Copyright (C) 2009-2014 Oracle Corporation
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * available from http://www.virtualbox.org. This file is free software;
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * you can redistribute it and/or modify it under the terms of the GNU
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * General Public License (GPL) as published by the Free Software
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * The contents of this file may alternatively be used under the terms
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * of the Common Development and Distribution License Version 1.0
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * VirtualBox OSE distribution, in which case the provisions of the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * CDDL are applicable instead of those of the GPL.
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * You may elect to license modified versions of this file under the
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync * terms and conditions of either the GPL or the CDDL or both.
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync/*******************************************************************************
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync* Header Files *
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync*******************************************************************************/
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsyncRTR3DECL(int) RTSha256Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Validate input */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
ed6697871be690c5826c252d598ea9965c2816b4vboxsync /* Initialize the hash context. */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Buffer size for progress callback */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Working buffer */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Process the memory in blocks */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Call the progress callback if one is defined */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync break; /* canceled */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Finished? */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Finally calculate & format the SHA256 sum */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = RTStrAllocEx(&pszDigest, RTSHA256_DIGEST_LEN + 1);
ed6697871be690c5826c252d598ea9965c2816b4vboxsync rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsyncRTR3DECL(int) RTSha256DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Validate input */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
ed6697871be690c5826c252d598ea9965c2816b4vboxsync /* Initialize the hash context. */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Open the file to calculate a SHA256 sum of */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Fetch the file size. Only needed if there is a progress callback. */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Allocate a reasonably large buffer, fall back on a tiny one. */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Read that file in blocks */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Call the progress callback if one is defined */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync break; /* canceled */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync /* Finally calculate & format the SHA256 sum */
cbf12315646559bf0cb5f12957f6096cb73e30f4vboxsync rc = RTStrAllocEx(&pszDigest, RTSHA256_DIGEST_LEN + 1);