VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp@ 30079

Last change on this file since 30079 was 30079, checked in by vboxsync, 15 years ago

IPRT: Use PFNRTPROGRESS.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1/* $Id: RTSha1Digest.cpp 30079 2010-06-07 14:57:44Z vboxsync $ */
2/** @file
3 * IPRT - SHA1 digest creation
4 */
5
6/*
7 * Copyright (C) 2009-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/sha.h>
33
34#include <iprt/alloca.h>
35#include <iprt/assert.h>
36#include <iprt/mem.h>
37#include <iprt/string.h>
38#include <iprt/file.h>
39
40#include <openssl/sha.h>
41
42
43RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser)
44{
45 /* Validate input */
46 AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
47 AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
48 AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
49
50 *ppszDigest = NULL;
51
52 /* Initialize OpenSSL. */
53 SHA_CTX ctx;
54 if (!SHA1_Init(&ctx))
55 return VERR_INTERNAL_ERROR;
56
57 /* Open the file to calculate a SHA1 sum of */
58 RTFILE hFile;
59 int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
60 if (RT_FAILURE(rc))
61 return rc;
62
63 /* Fetch the file size. Only needed if there is a progress callback. */
64 double rdMulti = 0;
65 if (pfnProgressCallback)
66 {
67 uint64_t cbFile;
68 rc = RTFileGetSize(hFile, &cbFile);
69 if (RT_FAILURE(rc))
70 {
71 RTFileClose(hFile);
72 return rc;
73 }
74 rdMulti = 100.0 / cbFile;
75 }
76
77 /* Allocate a reasonably large buffer, fall back on a tiny one. */
78 void *pvBufFree;
79 size_t cbBuf = _1M;
80 void *pvBuf = pvBufFree = RTMemTmpAlloc(cbBuf);
81 if (!pvBuf)
82 {
83 cbBuf = 0x1000;
84 pvBuf = alloca(cbBuf);
85 }
86
87 /* Read that file in blocks */
88 size_t cbRead;
89 size_t cbReadTotal = 0;
90 for (;;)
91 {
92 rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead);
93 if (RT_FAILURE(rc) || !cbRead)
94 break;
95 if(!SHA1_Update(&ctx, pvBuf, cbRead))
96 {
97 rc = VERR_INTERNAL_ERROR;
98 break;
99 }
100 cbReadTotal += cbRead;
101
102 /* Call the progress callback if one is defined */
103 if (pfnProgressCallback)
104 {
105 rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
106 if (RT_FAILURE(rc))
107 break; /* canceled */
108 }
109 }
110 RTMemTmpFree(pvBufFree);
111 RTFileClose(hFile);
112
113 if (RT_FAILURE(rc))
114 return rc;
115
116 /* Finally calculate & format the SHA1 sum */
117 unsigned char auchDig[RTSHA1_HASH_SIZE];
118 if (!SHA1_Final(auchDig, &ctx))
119 return VERR_INTERNAL_ERROR;
120
121 char *pszDigest;
122 rc = RTStrAllocEx(&pszDigest, RTSHA1_DIGEST_LEN + 1);
123 if (RT_SUCCESS(rc))
124 {
125 rc = RTSha1ToString(auchDig, pszDigest, RTSHA1_DIGEST_LEN + 1);
126 if (RT_SUCCESS(rc))
127 *ppszDigest = pszDigest;
128 else
129 RTStrFree(pszDigest);
130 }
131
132 return rc;
133}
134
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette