VirtualBox

source: vbox/trunk/include/iprt/sha.h@ 29901

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

IPRT: added progress callback support to SHA1 digest calculation and manifest verification

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1/** @file
2 * IPRT - SHA1 digest creation
3 */
4
5/*
6 * Copyright (C) 2009 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_sha_h
27#define ___iprt_sha_h
28
29#include <iprt/types.h>
30
31RT_C_DECLS_BEGIN
32
33/** @defgroup grp_rt_sha RTSha - SHA Family of Hash Functions
34 * @ingroup grp_rt
35 * @{
36 */
37
38/**
39 * SHA progress callback.
40 *
41 * @returns IPRT status code.
42 *
43 * @param uPercent The progress completion percentage.
44 * @param pvUser The user defined parameter.
45 */
46typedef DECLCALLBACK(int) FNRTSHAPROGRESS(unsigned uPercent, void *pvUser);
47/** Pointer to a SHA progress callback. */
48typedef FNRTSHAPROGRESS *PFNRTSHAPROGRESS;
49
50/** The size of a SHA-1 hash. */
51#define RTSHA1_HASH_SIZE 20
52/** The length of a SHA-1 digest string. The terminator is not included. */
53#define RTSHA1_DIGEST_LEN (40)
54
55/**
56 * SHA-1 context.
57 */
58typedef union RTSHA1CONTEXT
59{
60 uint8_t abPadding[ARCH_BITS == 32 ? 96 : 128];
61#ifdef RT_SHA1_PRIVATE_CONTEXT
62 SHA_CTX Private;
63#endif
64} RTSHA1CONTEXT;
65/** Pointer to an SHA-1 context. */
66typedef RTSHA1CONTEXT *PRTSHA1CONTEXT;
67
68/**
69 * Compute the SHA-1 hash of the data.
70 *
71 * @param pvBuf Pointer to the data.
72 * @param cbBuf The amount of data (in bytes).
73 * @param pabDigest Where to store the hash. (What is passed is a pointer to
74 * the caller's buffer.)
75 */
76RTDECL(void) RTSha1(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
77
78/**
79 * Initializes the SHA-1 context.
80 *
81 * @param pCtx Pointer to the SHA-1 context.
82 */
83RTDECL(void) RTSha1Init(PRTSHA1CONTEXT pCtx);
84
85/**
86 * Feed data into the SHA-1 computation.
87 *
88 * @param pCtx Pointer to the SHA-1 context.
89 * @param pvBuf Pointer to the data.
90 * @param cbBuf The length of the data (in bytes).
91 */
92RTDECL(void) RTSha1Update(PRTSHA1CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
93
94/**
95 * Compute the SHA-1 hash of the data.
96 *
97 * @param pCtx Pointer to the SHA-1 context.
98 * @param pabDigest Where to store the hash. (What is passed is a pointer to
99 * the caller's buffer.)
100 */
101RTDECL(void) RTSha1Final(PRTSHA1CONTEXT pCtx, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
102
103/**
104 * Converts a SHA-1 hash to a digest string.
105 *
106 * @returns IPRT status code.
107 *
108 * @param pabDigest The binary digest returned by RTSha1Final or RTSha1.
109 * @param pszDigest Where to return the stringified digest.
110 * @param cchDigest The size of the output buffer. Should be at least
111 * RTSHA1_STRING_LEN + 1 bytes.
112 */
113RTDECL(int) RTSha1ToString(uint8_t const pabDigest[RTSHA1_HASH_SIZE], char *pszDigest, size_t cchDigest);
114
115/**
116 * Converts a SHA-1 hash to a digest string.
117 *
118 * @returns IPRT status code.
119 *
120 * @param pszDigest The strigified digest. Leading and trailing spaces are
121 * ignored.
122 * @param pabDigest Where to store the hash. (What is passed is a pointer to
123 * the caller's buffer.)
124 */
125RTDECL(int) RTSha1FromString(char const *pszDigest, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
126
127/**
128 * Creates a SHA1 digest for the given file.
129 *
130 * @returns iprt status code.
131 *
132 * @param pszFile Filename to create a SHA1 digest for.
133 * @param ppszDigest On success the SHA1 digest.
134 * @param pfnProgressCallback optional callback for the progress indication
135 * @param pvUser user defined pointer for the callback
136 */
137RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, PFNRTSHAPROGRESS pfnProgressCallback, void *pvUser);
138
139
140
141/** The size of a SHA-256 hash. */
142#define RTSHA256_HASH_SIZE 32
143/** The length of a SHA-256 digest string. The terminator is not included. */
144#define RTSHA256_DIGEST_LEN 64
145
146/**
147 * SHA-256 context.
148 */
149typedef union RTSHA256CONTEXT
150{
151 uint8_t abPadding[ARCH_BITS == 32 ? 112 : 160];
152#ifdef RT_SHA256_PRIVATE_CONTEXT
153 SHA256_CTX Private;
154#endif
155} RTSHA256CONTEXT;
156/** Pointer to an SHA-256 context. */
157typedef RTSHA256CONTEXT *PRTSHA256CONTEXT;
158
159/**
160 * Compute the SHA-256 hash of the data.
161 *
162 * @param pvBuf Pointer to the data.
163 * @param cbBuf The amount of data (in bytes).
164 * @param pabDigest Where to store the hash. (What is passed is a pointer to
165 * the caller's buffer.)
166 */
167RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
168
169/**
170 * Initializes the SHA-256 context.
171 *
172 * @param pCtx Pointer to the SHA-256 context.
173 */
174RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx);
175
176/**
177 * Feed data into the SHA-256 computation.
178 *
179 * @param pCtx Pointer to the SHA-256 context.
180 * @param pvBuf Pointer to the data.
181 * @param cbBuf The length of the data (in bytes).
182 */
183RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
184
185/**
186 * Compute the SHA-256 hash of the data.
187 *
188 * @param pCtx Pointer to the SHA-256 context.
189 * @param pabDigest Where to store the hash. (What is passed is a pointer to
190 * the caller's buffer.)
191 */
192RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
193
194/**
195 * Converts a SHA-256 hash to a digest string.
196 *
197 * @returns IPRT status code.
198 *
199 * @param pabDigest The binary digest returned by RTSha256Final or RTSha256.
200 * @param pszDigest Where to return the stringified digest.
201 * @param cchDigest The size of the output buffer. Should be at least
202 * RTSHA256_STRING_LEN + 1 bytes.
203 */
204RTDECL(int) RTSha256ToString(uint8_t const pabDigest[RTSHA256_HASH_SIZE], char *pszDigest, size_t cchDigest);
205
206/**
207 * Converts a SHA-256 hash to a digest string.
208 *
209 * @returns IPRT status code.
210 *
211 * @param pszDigest The strigified digest. Leading and trailing spaces are
212 * ignored.
213 * @param pabDigest Where to store the hash. (What is passed is a pointer to
214 * the caller's buffer.)
215 */
216RTDECL(int) RTSha256FromString(char const *pszDigest, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
217
218
219
220/** The size of a SHA-512 hash. */
221#define RTSHA512_HASH_SIZE 64
222/** The length of a SHA-512 digest string. The terminator is not included. */
223#define RTSHA512_STRING_LEN 128
224
225/**
226 * SHA-512 context.
227 */
228typedef union RTSHA512CONTEXT
229{
230 uint8_t abPadding[ARCH_BITS == 32 ? 216 : 256];
231#ifdef RT_SHA512_PRIVATE_CONTEXT
232 SHA512_CTX Private;
233#endif
234} RTSHA512CONTEXT;
235/** Pointer to an SHA-512 context. */
236typedef RTSHA512CONTEXT *PRTSHA512CONTEXT;
237
238/**
239 * Compute the SHA-512 hash of the data.
240 *
241 * @param pvBuf Pointer to the data.
242 * @param cbBuf The amount of data (in bytes).
243 * @param pabDigest Where to store the hash. (What is passed is a pointer to
244 * the caller's buffer.)
245 */
246RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
247
248/**
249 * Initializes the SHA-512 context.
250 *
251 * @param pCtx Pointer to the SHA-512 context.
252 */
253RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx);
254
255/**
256 * Feed data into the SHA-512 computation.
257 *
258 * @param pCtx Pointer to the SHA-512 context.
259 * @param pvBuf Pointer to the data.
260 * @param cbBuf The length of the data (in bytes).
261 */
262RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
263
264/**
265 * Compute the SHA-512 hash of the data.
266 *
267 * @param pCtx Pointer to the SHA-512 context.
268 * @param pabDigest Where to store the hash. (What is passed is a pointer to
269 * the caller's buffer.)
270 */
271RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
272
273/**
274 * Converts a SHA-512 hash to a digest string.
275 *
276 * @returns IPRT status code.
277 *
278 * @param pabDigest The binary digest returned by RTSha512Final or RTSha512.
279 * @param pszDigest Where to return the stringified digest.
280 * @param cchDigest The size of the output buffer. Should be at least
281 * RTSHA512_STRING_LEN + 1 bytes.
282 */
283RTDECL(int) RTSha512ToString(uint8_t const pabDigest[RTSHA512_HASH_SIZE], char *pszDigest, size_t cchDigest);
284
285/**
286 * Converts a SHA-512 hash to a digest string.
287 *
288 * @returns IPRT status code.
289 *
290 * @param pszDigest The strigified digest. Leading and trailing spaces are
291 * ignored.
292 * @param pabDigest Where to store the hash. (What is passed is a pointer to
293 * the caller's buffer.)
294 */
295RTDECL(int) RTSha512FromString(char const *pszDigest, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
296
297/** @} */
298
299RT_C_DECLS_END
300
301#endif /* ___iprt_sha1_h */
302
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