1 | /* $Id: RTSha1Digest.cpp 29901 2010-05-31 12:53:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - SHA1 digest creation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 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/assert.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/file.h>
|
---|
38 |
|
---|
39 | #include <openssl/sha.h>
|
---|
40 |
|
---|
41 | RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, PFNRTSHAPROGRESS pfnProgressCallback, void *pvUser)
|
---|
42 | {
|
---|
43 | /* Validate input */
|
---|
44 | AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
|
---|
45 | AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
|
---|
46 | AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
|
---|
47 |
|
---|
48 | *ppszDigest = NULL;
|
---|
49 | int rc = VINF_SUCCESS;
|
---|
50 |
|
---|
51 | /* Initialize OpenSSL */
|
---|
52 | SHA_CTX ctx;
|
---|
53 | if (!SHA1_Init(&ctx))
|
---|
54 | return VERR_INTERNAL_ERROR;
|
---|
55 |
|
---|
56 | /* Fetch the file size. Only needed if there is a progress callback. */
|
---|
57 | float multi = 0;
|
---|
58 | if (pfnProgressCallback)
|
---|
59 | {
|
---|
60 | uint64_t cbFile;
|
---|
61 | rc = RTFileQuerySize(pszFile, &cbFile);
|
---|
62 | if (RT_FAILURE(rc))
|
---|
63 | return rc;
|
---|
64 | multi = 100.0 / cbFile;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /* Open the file to calculate a SHA1 sum of */
|
---|
68 | RTFILE file;
|
---|
69 | rc = RTFileOpen(&file, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
|
---|
70 | if (RT_FAILURE(rc))
|
---|
71 | return rc;
|
---|
72 |
|
---|
73 | /* Read that file in blocks */
|
---|
74 | void *pvBuf = RTMemTmpAlloc(_1M);
|
---|
75 | if (!pvBuf)
|
---|
76 | {
|
---|
77 | RTFileClose(file);
|
---|
78 | rc = VERR_NO_MEMORY;
|
---|
79 | }
|
---|
80 | size_t cbRead;
|
---|
81 | size_t cbReadFull = 0;
|
---|
82 | for (;;)
|
---|
83 | {
|
---|
84 | cbRead = 0;
|
---|
85 | rc = RTFileRead(file, pvBuf, _1M, &cbRead);
|
---|
86 | if (RT_FAILURE(rc) || !cbRead)
|
---|
87 | break;
|
---|
88 | if(!SHA1_Update(&ctx, pvBuf, cbRead))
|
---|
89 | {
|
---|
90 | rc = VERR_INTERNAL_ERROR;
|
---|
91 | break;
|
---|
92 | }
|
---|
93 | cbReadFull += cbRead;
|
---|
94 | /* Call progress callback if some is defined */
|
---|
95 | if ( pfnProgressCallback
|
---|
96 | && RT_FAILURE(pfnProgressCallback((unsigned)(cbReadFull * multi), pvUser)))
|
---|
97 | {
|
---|
98 | /* Cancel support */
|
---|
99 | rc = VERR_CANCELLED;
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | RTMemTmpFree(pvBuf);
|
---|
104 | RTFileClose(file);
|
---|
105 |
|
---|
106 | if (RT_FAILURE(rc))
|
---|
107 | return rc;
|
---|
108 |
|
---|
109 | /* Finally calculate & format the SHA1 sum */
|
---|
110 | unsigned char auchDig[RTSHA1_HASH_SIZE];
|
---|
111 | if (!SHA1_Final(auchDig, &ctx))
|
---|
112 | return VERR_INTERNAL_ERROR;
|
---|
113 |
|
---|
114 | char *pszDigest;
|
---|
115 | rc = RTStrAllocEx(&pszDigest, RTSHA1_DIGEST_LEN + 1);
|
---|
116 | if (RT_SUCCESS(rc))
|
---|
117 | {
|
---|
118 | rc = RTSha1ToString(auchDig, pszDigest, RTSHA1_DIGEST_LEN + 1);
|
---|
119 | if (RT_SUCCESS(rc))
|
---|
120 | *ppszDigest = pszDigest;
|
---|
121 | else
|
---|
122 | RTStrFree(pszDigest);
|
---|
123 | }
|
---|
124 |
|
---|
125 | return rc;
|
---|
126 | }
|
---|
127 |
|
---|