1 | /** @file
|
---|
2 | * IPRT - SHA1 digest creation
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2009-2010 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 |
|
---|
31 | RT_C_DECLS_BEGIN
|
---|
32 |
|
---|
33 | /** @defgroup grp_rt_sha RTSha - SHA Family of Hash Functions
|
---|
34 | * @ingroup grp_rt
|
---|
35 | * @{
|
---|
36 | */
|
---|
37 |
|
---|
38 | /** The size of a SHA-1 hash. */
|
---|
39 | #define RTSHA1_HASH_SIZE 20
|
---|
40 | /** The length of a SHA-1 digest string. The terminator is not included. */
|
---|
41 | #define RTSHA1_DIGEST_LEN 40
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * SHA-1 context.
|
---|
45 | */
|
---|
46 | typedef union RTSHA1CONTEXT
|
---|
47 | {
|
---|
48 | uint8_t abPadding[ARCH_BITS == 32 ? 96 : 128];
|
---|
49 | #ifdef RT_SHA1_PRIVATE_CONTEXT
|
---|
50 | SHA_CTX Private;
|
---|
51 | #endif
|
---|
52 | } RTSHA1CONTEXT;
|
---|
53 | /** Pointer to an SHA-1 context. */
|
---|
54 | typedef RTSHA1CONTEXT *PRTSHA1CONTEXT;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Compute the SHA-1 hash of the data.
|
---|
58 | *
|
---|
59 | * @param pvBuf Pointer to the data.
|
---|
60 | * @param cbBuf The amount of data (in bytes).
|
---|
61 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
62 | * the caller's buffer.)
|
---|
63 | */
|
---|
64 | RTDECL(void) RTSha1(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Initializes the SHA-1 context.
|
---|
68 | *
|
---|
69 | * @param pCtx Pointer to the SHA-1 context.
|
---|
70 | */
|
---|
71 | RTDECL(void) RTSha1Init(PRTSHA1CONTEXT pCtx);
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Feed data into the SHA-1 computation.
|
---|
75 | *
|
---|
76 | * @param pCtx Pointer to the SHA-1 context.
|
---|
77 | * @param pvBuf Pointer to the data.
|
---|
78 | * @param cbBuf The length of the data (in bytes).
|
---|
79 | */
|
---|
80 | RTDECL(void) RTSha1Update(PRTSHA1CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Compute the SHA-1 hash of the data.
|
---|
84 | *
|
---|
85 | * @param pCtx Pointer to the SHA-1 context.
|
---|
86 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
87 | * the caller's buffer.)
|
---|
88 | */
|
---|
89 | RTDECL(void) RTSha1Final(PRTSHA1CONTEXT pCtx, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Converts a SHA-1 hash to a digest string.
|
---|
93 | *
|
---|
94 | * @returns IPRT status code.
|
---|
95 | *
|
---|
96 | * @param pabDigest The binary digest returned by RTSha1Final or RTSha1.
|
---|
97 | * @param pszDigest Where to return the stringified digest.
|
---|
98 | * @param cchDigest The size of the output buffer. Should be at least
|
---|
99 | * RTSHA1_DIGEST_LEN + 1 bytes.
|
---|
100 | */
|
---|
101 | RTDECL(int) RTSha1ToString(uint8_t const pabDigest[RTSHA1_HASH_SIZE], char *pszDigest, size_t cchDigest);
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Converts a SHA-1 hash to a digest string.
|
---|
105 | *
|
---|
106 | * @returns IPRT status code.
|
---|
107 | *
|
---|
108 | * @param pszDigest The stringified digest. Leading and trailing spaces are
|
---|
109 | * ignored.
|
---|
110 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
111 | * the caller's buffer.)
|
---|
112 | */
|
---|
113 | RTDECL(int) RTSha1FromString(char const *pszDigest, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Creates a SHA1 digest for the given memory buffer.
|
---|
117 | *
|
---|
118 | * @returns iprt status code.
|
---|
119 | *
|
---|
120 | * @param pvBuf Memory buffer to create a SHA1 digest for.
|
---|
121 | * @param cbBuf The amount of data (in bytes).
|
---|
122 | * @param ppszDigest On success the SHA1 digest.
|
---|
123 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
124 | * @param pvUser user defined pointer for the callback
|
---|
125 | */
|
---|
126 | RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Creates a SHA1 digest for the given file.
|
---|
130 | *
|
---|
131 | * @returns iprt status code.
|
---|
132 | *
|
---|
133 | * @param pszFile Filename to create a SHA1 digest for.
|
---|
134 | * @param ppszDigest On success the SHA1 digest.
|
---|
135 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
136 | * @param pvUser user defined pointer for the callback
|
---|
137 | */
|
---|
138 | RTR3DECL(int) RTSha1DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
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 | */
|
---|
149 | typedef 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. */
|
---|
157 | typedef 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 | */
|
---|
167 | RTDECL(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 | */
|
---|
174 | RTDECL(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 | */
|
---|
183 | RTDECL(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 | */
|
---|
192 | RTDECL(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_DIGEST_LEN + 1 bytes.
|
---|
203 | */
|
---|
204 | RTDECL(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 stringified 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 | */
|
---|
216 | RTDECL(int) RTSha256FromString(char const *pszDigest, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Creates a SHA256 digest for the given memory buffer.
|
---|
220 | *
|
---|
221 | * @returns iprt status code.
|
---|
222 | *
|
---|
223 | * @param pvBuf Memory buffer to create a
|
---|
224 | * SHA256 digest for.
|
---|
225 | * @param cbBuf The amount of data (in bytes).
|
---|
226 | * @param ppszDigest On success the SHA256 digest.
|
---|
227 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
228 | * @param pvUser user defined pointer for the callback
|
---|
229 | */
|
---|
230 | RTR3DECL(int) RTSha256Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Creates a SHA256 digest for the given file.
|
---|
234 | *
|
---|
235 | * @returns iprt status code.
|
---|
236 | *
|
---|
237 | * @param pszFile Filename to create a SHA256
|
---|
238 | * digest for.
|
---|
239 | * @param ppszDigest On success the SHA256 digest.
|
---|
240 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
241 | * @param pvUser user defined pointer for the callback
|
---|
242 | */
|
---|
243 | RTR3DECL(int) RTSha256DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
244 |
|
---|
245 | /** The size of a SHA-512 hash. */
|
---|
246 | #define RTSHA512_HASH_SIZE 64
|
---|
247 | /** The length of a SHA-512 digest string. The terminator is not included. */
|
---|
248 | #define RTSHA512_DIGEST_LEN 128
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * SHA-512 context.
|
---|
252 | */
|
---|
253 | typedef union RTSHA512CONTEXT
|
---|
254 | {
|
---|
255 | uint8_t abPadding[ARCH_BITS == 32 ? 216 : 256];
|
---|
256 | #ifdef RT_SHA512_PRIVATE_CONTEXT
|
---|
257 | SHA512_CTX Private;
|
---|
258 | #endif
|
---|
259 | } RTSHA512CONTEXT;
|
---|
260 | /** Pointer to an SHA-512 context. */
|
---|
261 | typedef RTSHA512CONTEXT *PRTSHA512CONTEXT;
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Compute the SHA-512 hash of the data.
|
---|
265 | *
|
---|
266 | * @param pvBuf Pointer to the data.
|
---|
267 | * @param cbBuf The amount of data (in bytes).
|
---|
268 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
269 | * the caller's buffer.)
|
---|
270 | */
|
---|
271 | RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Initializes the SHA-512 context.
|
---|
275 | *
|
---|
276 | * @param pCtx Pointer to the SHA-512 context.
|
---|
277 | */
|
---|
278 | RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx);
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Feed data into the SHA-512 computation.
|
---|
282 | *
|
---|
283 | * @param pCtx Pointer to the SHA-512 context.
|
---|
284 | * @param pvBuf Pointer to the data.
|
---|
285 | * @param cbBuf The length of the data (in bytes).
|
---|
286 | */
|
---|
287 | RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Compute the SHA-512 hash of the data.
|
---|
291 | *
|
---|
292 | * @param pCtx Pointer to the SHA-512 context.
|
---|
293 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
294 | * the caller's buffer.)
|
---|
295 | */
|
---|
296 | RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Converts a SHA-512 hash to a digest string.
|
---|
300 | *
|
---|
301 | * @returns IPRT status code.
|
---|
302 | *
|
---|
303 | * @param pabDigest The binary digest returned by RTSha512Final or RTSha512.
|
---|
304 | * @param pszDigest Where to return the stringified digest.
|
---|
305 | * @param cchDigest The size of the output buffer. Should be at least
|
---|
306 | * RTSHA512_DIGEST_LEN + 1 bytes.
|
---|
307 | */
|
---|
308 | RTDECL(int) RTSha512ToString(uint8_t const pabDigest[RTSHA512_HASH_SIZE], char *pszDigest, size_t cchDigest);
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Converts a SHA-512 hash to a digest string.
|
---|
312 | *
|
---|
313 | * @returns IPRT status code.
|
---|
314 | *
|
---|
315 | * @param pszDigest The stringified digest. Leading and trailing spaces are
|
---|
316 | * ignored.
|
---|
317 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
318 | * the caller's buffer.)
|
---|
319 | */
|
---|
320 | RTDECL(int) RTSha512FromString(char const *pszDigest, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
|
---|
321 |
|
---|
322 | /** @} */
|
---|
323 |
|
---|
324 | RT_C_DECLS_END
|
---|
325 |
|
---|
326 | #endif /* ___iprt_sha1_h */
|
---|
327 |
|
---|