1 | /* $Id: RTSha256Digest.cpp 80585 2019-09-04 14:05:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - SHA256 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-2019 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 |
|
---|
45 | RTR3DECL(int) RTSha256Digest(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 | RTSHA256CONTEXT Ctx;
|
---|
57 | RTSha256Init(&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 | RTSha256Update(&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 SHA256 sum */
|
---|
88 | uint8_t abHash[RTSHA256_HASH_SIZE];
|
---|
89 | RTSha256Final(&Ctx, abHash);
|
---|
90 |
|
---|
91 | char *pszDigest;
|
---|
92 | rc = RTStrAllocEx(&pszDigest, RTSHA256_DIGEST_LEN + 1);
|
---|
93 | if (RT_SUCCESS(rc))
|
---|
94 | {
|
---|
95 | rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1);
|
---|
96 | if (RT_SUCCESS(rc))
|
---|
97 | *ppszDigest = pszDigest;
|
---|
98 | else
|
---|
99 | RTStrFree(pszDigest);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | return rc;
|
---|
104 | }
|
---|
105 |
|
---|
106 | RTR3DECL(int) RTSha256DigestFromFile(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 | /* Initialize the hash context. */
|
---|
116 | RTSHA256CONTEXT Ctx;
|
---|
117 | RTSha256Init(&Ctx);
|
---|
118 |
|
---|
119 | /* Open the file to calculate a SHA256 sum of */
|
---|
120 | RTFILE hFile;
|
---|
121 | int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
|
---|
122 | if (RT_FAILURE(rc))
|
---|
123 | return rc;
|
---|
124 |
|
---|
125 | /* Fetch the file size. Only needed if there is a progress callback. */
|
---|
126 | double rdMulti = 0;
|
---|
127 | if (pfnProgressCallback)
|
---|
128 | {
|
---|
129 | uint64_t cbFile;
|
---|
130 | rc = RTFileQuerySize(hFile, &cbFile);
|
---|
131 | if (RT_FAILURE(rc))
|
---|
132 | {
|
---|
133 | RTFileClose(hFile);
|
---|
134 | return rc;
|
---|
135 | }
|
---|
136 | rdMulti = 100.0 / (cbFile ? cbFile : 1);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* Allocate a reasonably large buffer, fall back on a tiny one. */
|
---|
140 | void *pvBufFree;
|
---|
141 | size_t cbBuf = _1M;
|
---|
142 | void *pvBuf = pvBufFree = RTMemTmpAlloc(cbBuf);
|
---|
143 | if (!pvBuf)
|
---|
144 | {
|
---|
145 | cbBuf = 0x1000;
|
---|
146 | pvBuf = alloca(cbBuf);
|
---|
147 | }
|
---|
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 | RTSha256Update(&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 SHA256 sum */
|
---|
175 | uint8_t abHash[RTSHA256_HASH_SIZE];
|
---|
176 | RTSha256Final(&Ctx, abHash);
|
---|
177 |
|
---|
178 | char *pszDigest;
|
---|
179 | rc = RTStrAllocEx(&pszDigest, RTSHA256_DIGEST_LEN + 1);
|
---|
180 | if (RT_SUCCESS(rc))
|
---|
181 | {
|
---|
182 | rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1);
|
---|
183 | if (RT_SUCCESS(rc))
|
---|
184 | *ppszDigest = pszDigest;
|
---|
185 | else
|
---|
186 | RTStrFree(pszDigest);
|
---|
187 | }
|
---|
188 |
|
---|
189 | return rc;
|
---|
190 | }
|
---|
191 |
|
---|