Changeset 51851 in vbox for trunk/src/VBox/Runtime/common/checksum/RTSha256Digest.cpp
- Timestamp:
- Jul 3, 2014 2:01:28 PM (11 years ago)
- svn:sync-xref-src-repo-rev:
- 94702
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/checksum/RTSha256Digest.cpp
r45234 r51851 1 1 /** @file 2 2 * IPRT - SHA256 digest creation 3 * 4 * @todo Replace this with generic RTCrDigest based implementation. Too much 5 * stupid code duplication. 3 6 */ 4 7 5 8 /* 6 * Copyright (C) 2009-201 3Oracle Corporation9 * Copyright (C) 2009-2014 Oracle Corporation 7 10 * 8 11 * This file is part of VirtualBox Open Source Edition (OSE), as … … 37 40 #include <iprt/file.h> 38 41 39 #include <openssl/sha.h>40 41 42 42 43 RTR3DECL(int) RTSha256Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser) … … 50 51 *ppszDigest = NULL; 51 52 52 /* Initialize OpenSSL. */ 53 SHA256_CTX ctx; 54 if (!SHA256_Init(&ctx)) 55 return VERR_INTERNAL_ERROR; 53 /* Initialize the hash context. */ 54 RTSHA256CONTEXT Ctx; 55 RTSha256Init(&Ctx); 56 56 57 57 /* Buffer size for progress callback */ 58 double rdMulti = 100.0 / cbBuf;58 double rdMulti = 100.0 / (cbBuf ? cbBuf : 1); 59 59 60 60 /* Working buffer */ … … 62 62 63 63 /* Process the memory in blocks */ 64 size_t cbRead;65 64 size_t cbReadTotal = 0; 66 65 for (;;) 67 66 { 68 cbRead = RT_MIN(cbBuf - cbReadTotal, _1M); 69 if(!SHA256_Update(&ctx, pvTmp, cbRead)) 70 { 71 rc = VERR_INTERNAL_ERROR; 72 break; 73 } 67 size_t cbRead = RT_MIN(cbBuf - cbReadTotal, _1M); 68 RTSha256Update(&Ctx, pvTmp, cbRead); 74 69 cbReadTotal += cbRead; 75 70 pvTmp += cbRead; … … 89 84 { 90 85 /* Finally calculate & format the SHA256 sum */ 91 unsigned char auchDig[RTSHA256_HASH_SIZE]; 92 if (!SHA256_Final(auchDig, &ctx)) 93 return VERR_INTERNAL_ERROR; 86 uint8_t abHash[RTSHA256_HASH_SIZE]; 87 RTSha256Final(&Ctx, abHash); 94 88 95 89 char *pszDigest; … … 97 91 if (RT_SUCCESS(rc)) 98 92 { 99 rc = RTSha256ToString(a uchDig, pszDigest, RTSHA256_DIGEST_LEN + 1);93 rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1); 100 94 if (RT_SUCCESS(rc)) 101 95 *ppszDigest = pszDigest; … … 117 111 *ppszDigest = NULL; 118 112 119 /* Initialize OpenSSL. */ 120 SHA256_CTX ctx; 121 if (!SHA256_Init(&ctx)) 122 return VERR_INTERNAL_ERROR; 113 /* Initialize the hash context. */ 114 RTSHA256CONTEXT Ctx; 115 RTSha256Init(&Ctx); 123 116 124 117 /* Open the file to calculate a SHA256 sum of */ … … 139 132 return rc; 140 133 } 141 rdMulti = 100.0 / cbFile;134 rdMulti = 100.0 / (cbFile ? cbFile : 1); 142 135 } 143 136 … … 153 146 154 147 /* Read that file in blocks */ 155 size_t cbRead;156 148 size_t cbReadTotal = 0; 157 149 for (;;) 158 150 { 151 size_t cbRead; 159 152 rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead); 160 153 if (RT_FAILURE(rc) || !cbRead) 161 154 break; 162 if(!SHA256_Update(&ctx, pvBuf, cbRead)) 163 { 164 rc = VERR_INTERNAL_ERROR; 165 break; 166 } 155 RTSha256Update(&Ctx, pvBuf, cbRead); 167 156 cbReadTotal += cbRead; 168 157 … … 182 171 183 172 /* Finally calculate & format the SHA256 sum */ 184 unsigned char auchDig[RTSHA256_HASH_SIZE]; 185 if (!SHA256_Final(auchDig, &ctx)) 186 return VERR_INTERNAL_ERROR; 173 uint8_t abHash[RTSHA256_HASH_SIZE]; 174 RTSha256Final(&Ctx, abHash); 187 175 188 176 char *pszDigest; … … 190 178 if (RT_SUCCESS(rc)) 191 179 { 192 rc = RTSha256ToString(a uchDig, pszDigest, RTSHA256_DIGEST_LEN + 1);180 rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1); 193 181 if (RT_SUCCESS(rc)) 194 182 *ppszDigest = pszDigest;
Note:
See TracChangeset
for help on using the changeset viewer.