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 |
|
---|
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_STRING_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 strigified 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 file.
|
---|
117 | *
|
---|
118 | * @returns iprt status code.
|
---|
119 | *
|
---|
120 | * @param pszFile Filename to create a SHA1 digest for.
|
---|
121 | * @param ppszDigest On success the SHA1 digest.
|
---|
122 | * @param pfnProgressCallback optional callback for the progress indication
|
---|
123 | * @param pvUser user defined pointer for the callback
|
---|
124 | */
|
---|
125 | RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, FNRTPROGRESS pfnProgressCallback, void *pvUser);
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 | /** The size of a SHA-256 hash. */
|
---|
130 | #define RTSHA256_HASH_SIZE 32
|
---|
131 | /** The length of a SHA-256 digest string. The terminator is not included. */
|
---|
132 | #define RTSHA256_DIGEST_LEN 64
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * SHA-256 context.
|
---|
136 | */
|
---|
137 | typedef union RTSHA256CONTEXT
|
---|
138 | {
|
---|
139 | uint8_t abPadding[ARCH_BITS == 32 ? 112 : 160];
|
---|
140 | #ifdef RT_SHA256_PRIVATE_CONTEXT
|
---|
141 | SHA256_CTX Private;
|
---|
142 | #endif
|
---|
143 | } RTSHA256CONTEXT;
|
---|
144 | /** Pointer to an SHA-256 context. */
|
---|
145 | typedef RTSHA256CONTEXT *PRTSHA256CONTEXT;
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Compute the SHA-256 hash of the data.
|
---|
149 | *
|
---|
150 | * @param pvBuf Pointer to the data.
|
---|
151 | * @param cbBuf The amount of data (in bytes).
|
---|
152 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
153 | * the caller's buffer.)
|
---|
154 | */
|
---|
155 | RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Initializes the SHA-256 context.
|
---|
159 | *
|
---|
160 | * @param pCtx Pointer to the SHA-256 context.
|
---|
161 | */
|
---|
162 | RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx);
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Feed data into the SHA-256 computation.
|
---|
166 | *
|
---|
167 | * @param pCtx Pointer to the SHA-256 context.
|
---|
168 | * @param pvBuf Pointer to the data.
|
---|
169 | * @param cbBuf The length of the data (in bytes).
|
---|
170 | */
|
---|
171 | RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Compute the SHA-256 hash of the data.
|
---|
175 | *
|
---|
176 | * @param pCtx Pointer to the SHA-256 context.
|
---|
177 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
178 | * the caller's buffer.)
|
---|
179 | */
|
---|
180 | RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Converts a SHA-256 hash to a digest string.
|
---|
184 | *
|
---|
185 | * @returns IPRT status code.
|
---|
186 | *
|
---|
187 | * @param pabDigest The binary digest returned by RTSha256Final or RTSha256.
|
---|
188 | * @param pszDigest Where to return the stringified digest.
|
---|
189 | * @param cchDigest The size of the output buffer. Should be at least
|
---|
190 | * RTSHA256_STRING_LEN + 1 bytes.
|
---|
191 | */
|
---|
192 | RTDECL(int) RTSha256ToString(uint8_t const pabDigest[RTSHA256_HASH_SIZE], char *pszDigest, size_t cchDigest);
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Converts a SHA-256 hash to a digest string.
|
---|
196 | *
|
---|
197 | * @returns IPRT status code.
|
---|
198 | *
|
---|
199 | * @param pszDigest The strigified digest. Leading and trailing spaces are
|
---|
200 | * ignored.
|
---|
201 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
202 | * the caller's buffer.)
|
---|
203 | */
|
---|
204 | RTDECL(int) RTSha256FromString(char const *pszDigest, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
|
---|
205 |
|
---|
206 |
|
---|
207 |
|
---|
208 | /** The size of a SHA-512 hash. */
|
---|
209 | #define RTSHA512_HASH_SIZE 64
|
---|
210 | /** The length of a SHA-512 digest string. The terminator is not included. */
|
---|
211 | #define RTSHA512_STRING_LEN 128
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * SHA-512 context.
|
---|
215 | */
|
---|
216 | typedef union RTSHA512CONTEXT
|
---|
217 | {
|
---|
218 | uint8_t abPadding[ARCH_BITS == 32 ? 216 : 256];
|
---|
219 | #ifdef RT_SHA512_PRIVATE_CONTEXT
|
---|
220 | SHA512_CTX Private;
|
---|
221 | #endif
|
---|
222 | } RTSHA512CONTEXT;
|
---|
223 | /** Pointer to an SHA-512 context. */
|
---|
224 | typedef RTSHA512CONTEXT *PRTSHA512CONTEXT;
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Compute the SHA-512 hash of the data.
|
---|
228 | *
|
---|
229 | * @param pvBuf Pointer to the data.
|
---|
230 | * @param cbBuf The amount of data (in bytes).
|
---|
231 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
232 | * the caller's buffer.)
|
---|
233 | */
|
---|
234 | RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Initializes the SHA-512 context.
|
---|
238 | *
|
---|
239 | * @param pCtx Pointer to the SHA-512 context.
|
---|
240 | */
|
---|
241 | RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx);
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Feed data into the SHA-512 computation.
|
---|
245 | *
|
---|
246 | * @param pCtx Pointer to the SHA-512 context.
|
---|
247 | * @param pvBuf Pointer to the data.
|
---|
248 | * @param cbBuf The length of the data (in bytes).
|
---|
249 | */
|
---|
250 | RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Compute the SHA-512 hash of the data.
|
---|
254 | *
|
---|
255 | * @param pCtx Pointer to the SHA-512 context.
|
---|
256 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
257 | * the caller's buffer.)
|
---|
258 | */
|
---|
259 | RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Converts a SHA-512 hash to a digest string.
|
---|
263 | *
|
---|
264 | * @returns IPRT status code.
|
---|
265 | *
|
---|
266 | * @param pabDigest The binary digest returned by RTSha512Final or RTSha512.
|
---|
267 | * @param pszDigest Where to return the stringified digest.
|
---|
268 | * @param cchDigest The size of the output buffer. Should be at least
|
---|
269 | * RTSHA512_STRING_LEN + 1 bytes.
|
---|
270 | */
|
---|
271 | RTDECL(int) RTSha512ToString(uint8_t const pabDigest[RTSHA512_HASH_SIZE], char *pszDigest, size_t cchDigest);
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Converts a SHA-512 hash to a digest string.
|
---|
275 | *
|
---|
276 | * @returns IPRT status code.
|
---|
277 | *
|
---|
278 | * @param pszDigest The strigified digest. Leading and trailing spaces are
|
---|
279 | * ignored.
|
---|
280 | * @param pabDigest Where to store the hash. (What is passed is a pointer to
|
---|
281 | * the caller's buffer.)
|
---|
282 | */
|
---|
283 | RTDECL(int) RTSha512FromString(char const *pszDigest, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
|
---|
284 |
|
---|
285 | /** @} */
|
---|
286 |
|
---|
287 | RT_C_DECLS_END
|
---|
288 |
|
---|
289 | #endif /* ___iprt_sha1_h */
|
---|
290 |
|
---|