VirtualBox

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

Last change on this file since 87203 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/* $Id: RTSha1Digest.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - SHA1 digest creation
4 *
5 * @todo Replace this with generic RTCrDigest based implementation. Too much
6 * stupid code duplication.
7 */
8
9/*
10 * Copyright (C) 2009-2020 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 *
20 * The contents of this file may alternatively be used under the terms
21 * of the Common Development and Distribution License Version 1.0
22 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
23 * VirtualBox OSE distribution, in which case the provisions of the
24 * CDDL are applicable instead of those of the GPL.
25 *
26 * You may elect to license modified versions of this file under the
27 * terms and conditions of either the GPL or the CDDL or both.
28 */
29
30
31/*********************************************************************************************************************************
32* Header Files *
33*********************************************************************************************************************************/
34#include "internal/iprt.h"
35#include <iprt/sha.h>
36
37#include <iprt/alloca.h>
38#include <iprt/assert.h>
39#include <iprt/errcore.h>
40#include <iprt/mem.h>
41#include <iprt/string.h>
42#include <iprt/file.h>
43
44
45RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
46{
47 /* Validate input */
48 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
49 AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
50 AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
51
52 int rc = VINF_SUCCESS;
53 *ppszDigest = NULL;
54
55 /* Initialize the hash context. */
56 RTSHA1CONTEXT Ctx;
57 RTSha1Init(&Ctx);
58
59 /* Buffer size for progress callback */
60 double rdMulti = 100.0 / (cbBuf ? cbBuf : 1);
61
62 /* Working buffer */
63 char *pvTmp = (char*)pvBuf;
64
65 /* Process the memory in blocks */
66 size_t cbReadTotal = 0;
67 for (;;)
68 {
69 size_t cbRead = RT_MIN(cbBuf - cbReadTotal, _1M);
70 RTSha1Update(&Ctx, pvTmp, cbRead);
71 cbReadTotal += cbRead;
72 pvTmp += cbRead;
73
74 /* Call the progress callback if one is defined */
75 if (pfnProgressCallback)
76 {
77 rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
78 if (RT_FAILURE(rc))
79 break; /* canceled */
80 }
81 /* Finished? */
82 if (cbReadTotal == cbBuf)
83 break;
84 }
85 if (RT_SUCCESS(rc))
86 {
87 /* Finally calculate & format the SHA1 sum */
88 uint8_t abHash[RTSHA1_HASH_SIZE];
89 RTSha1Final(&Ctx, abHash);
90
91 char *pszDigest;
92 rc = RTStrAllocEx(&pszDigest, RTSHA1_DIGEST_LEN + 1);
93 if (RT_SUCCESS(rc))
94 {
95 rc = RTSha1ToString(abHash, pszDigest, RTSHA1_DIGEST_LEN + 1);
96 if (RT_SUCCESS(rc))
97 *ppszDigest = pszDigest;
98 else
99 RTStrFree(pszDigest);
100 }
101 }
102
103 return rc;
104}
105
106RTR3DECL(int) RTSha1DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
107{
108 /* Validate input */
109 AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
110 AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
111 AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
112
113 *ppszDigest = NULL;
114
115 /* Open the file to calculate a SHA1 sum of */
116 RTFILE hFile;
117 int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
118 if (RT_FAILURE(rc))
119 return rc;
120
121 /* Fetch the file size. Only needed if there is a progress callback. */
122 double rdMulti = 0;
123 if (pfnProgressCallback)
124 {
125 uint64_t cbFile;
126 rc = RTFileQuerySize(hFile, &cbFile);
127 if (RT_FAILURE(rc))
128 {
129 RTFileClose(hFile);
130 return rc;
131 }
132 rdMulti = 100.0 / (cbFile ? cbFile : 1);
133 }
134
135 /* Allocate a reasonably large buffer, fall back on a tiny one. */
136 void *pvBufFree;
137 size_t cbBuf = _1M;
138 void *pvBuf = pvBufFree = RTMemTmpAlloc(cbBuf);
139 if (!pvBuf)
140 {
141 cbBuf = 0x1000;
142 pvBuf = alloca(cbBuf);
143 }
144
145 /* Initialize the hash context. */
146 RTSHA1CONTEXT Ctx;
147 RTSha1Init(&Ctx);
148
149 /* Read that file in blocks */
150 size_t cbReadTotal = 0;
151 for (;;)
152 {
153 size_t cbRead;
154 rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead);
155 if (RT_FAILURE(rc) || !cbRead)
156 break;
157 RTSha1Update(&Ctx, pvBuf, cbRead);
158 cbReadTotal += cbRead;
159
160 /* Call the progress callback if one is defined */
161 if (pfnProgressCallback)
162 {
163 rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
164 if (RT_FAILURE(rc))
165 break; /* canceled */
166 }
167 }
168 RTMemTmpFree(pvBufFree);
169 RTFileClose(hFile);
170
171 if (RT_FAILURE(rc))
172 return rc;
173
174 /* Finally calculate & format the SHA1 sum */
175 uint8_t abHash[RTSHA1_HASH_SIZE];
176 RTSha1Final(&Ctx, abHash);
177
178 char *pszDigest;
179 rc = RTStrAllocEx(&pszDigest, RTSHA1_DIGEST_LEN + 1);
180 if (RT_SUCCESS(rc))
181 {
182 rc = RTSha1ToString(abHash, pszDigest, RTSHA1_DIGEST_LEN + 1);
183 if (RT_SUCCESS(rc))
184 *ppszDigest = pszDigest;
185 else
186 RTStrFree(pszDigest);
187 }
188
189 return rc;
190}
191
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