VirtualBox

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

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

Runtime: Use RTSha1ToString cause the old method used the wrong formater anyway.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1/* $Id: RTSha1Digest.cpp 29778 2010-05-25 11:56:36Z 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/err.h>
36#include <iprt/stream.h>
37#include <iprt/string.h>
38#include <iprt/mem.h>
39
40#include <openssl/sha.h>
41
42
43
44RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest)
45{
46 /* Validate input */
47 AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
48 AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
49
50 *ppszDigest = NULL;
51
52 /* Initialize OpenSSL */
53 SHA_CTX ctx;
54 if (!SHA1_Init(&ctx))
55 return VERR_INTERNAL_ERROR;
56
57 /** @todo r=bird: Using a stream here doesn't really serve much purpose as
58 * few stream implementations uses a buffer much larger than 4KB. (The
59 * only I'm aware of is libc on OS/2, which uses 8KB.) */
60
61 /* Open the file to calculate a SHA1 sum of */
62 PRTSTREAM pStream;
63 int rc = RTStrmOpen(pszFile, "rb", &pStream);
64 if (RT_FAILURE(rc))
65 return rc;
66
67 /* Read that file in blocks */
68 void *pvBuf[4096];
69 size_t cbRead;
70 do
71 {
72 cbRead = 0;
73 rc = RTStrmReadEx(pStream, pvBuf, 4096, &cbRead);
74 if (RT_FAILURE(rc))
75 break;
76 if(!SHA1_Update(&ctx, pvBuf, cbRead))
77 {
78 rc = VERR_INTERNAL_ERROR;
79 break;
80 }
81 } while (cbRead > 0);
82 RTStrmClose(pStream);
83
84 if (RT_FAILURE(rc))
85 return rc;
86
87 /* Finally calculate & format the SHA1 sum */
88 unsigned char auchDig[20];
89 if (!SHA1_Final(auchDig, &ctx))
90 return VERR_INTERNAL_ERROR;
91
92 *ppszDigest = (char*)RTMemAlloc(41);
93 rc = RTSha1ToString(auchDig, *ppszDigest, 41);
94 if (RT_FAILURE(rc))
95 RTStrFree(*ppszDigest);
96
97 return rc;
98}
99
Note: See TracBrowser for help on using the repository browser.

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