VirtualBox

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

最後變更 在這個檔案從23501是 23501,由 vboxsync 提交於 15 年 前

IPRT: Added SHA-1, SHA-256 and SHA-512 APIs. Added a simple digest program for testing these.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.0 KB
 
1/** @file
2 * IPRT - SHA1 digest creation
3 */
4
5/*
6 * Copyright (C) 2009 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.alldomusa.eu.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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_sha_h
31#define ___iprt_sha_h
32
33#include <iprt/types.h>
34
35RT_C_DECLS_BEGIN
36
37/** @defgroup grp_rt_sha RTSha - SHA Family of Hash Functions
38 * @ingroup grp_rt
39 * @{
40 */
41
42/** The size of a SHA-1 hash. */
43#define RTSHA1_HASH_SIZE 20
44
45/**
46 * SHA-1 context.
47 */
48typedef union RTSHA1CONTEXT
49{
50 uint8_t abPadding[ARCH_BITS == 32 ? 96 : 128];
51#ifdef RT_SHA1_PRIVATE_CONTEXT
52 SHA_CTX Private;
53#endif
54} RTSHA1CONTEXT;
55/** Pointer to an SHA-1 context. */
56typedef RTSHA1CONTEXT *PRTSHA1CONTEXT;
57
58/**
59 * Compute the SHA-1 hash of the data.
60 *
61 * @param pvBuf Pointer to the data.
62 * @param cbBuf The amount of data (in bytes).
63 * @param pabDigest Where to store the hash. (What is passed is a pointer to
64 * the caller's buffer.)
65 */
66RTDECL(void) RTSha1(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
67
68/**
69 * Initializes the SHA-1 context.
70 *
71 * @param pCtx Pointer to the SHA-1 context.
72 */
73RTDECL(void) RTSha1Init(PRTSHA1CONTEXT pCtx);
74
75/**
76 * Feed data into the SHA-1 computation.
77 *
78 * @param pCtx Pointer to the SHA-1 context.
79 * @param pvBuf Pointer to the data.
80 * @param cbBuf The length of the data (in bytes).
81 */
82RTDECL(void) RTSha1Update(PRTSHA1CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
83
84/**
85 * Compute the SHA-1 hash of the data.
86 *
87 * @param pCtx Pointer to the SHA-1 context.
88 * @param pabDigest Where to store the hash. (What is passed is a pointer to
89 * the caller's buffer.)
90 */
91RTDECL(void) RTSha1Final(PRTSHA1CONTEXT pCtx, uint8_t pabDigest[RTSHA1_HASH_SIZE]);
92
93
94/**
95 * Creates a SHA1 digest for the given file.
96 *
97 * @returns iprt status code.
98 *
99 * @param pszFile Filename to create a SHA1 digest for.
100 * @param ppszDigest On success the SHA1 digest.
101 */
102RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest);
103
104
105/** The size of a SHA-256 hash. */
106#define RTSHA256_HASH_SIZE 32
107
108/**
109 * SHA-256 context.
110 */
111typedef union RTSHA256CONTEXT
112{
113 uint8_t abPadding[ARCH_BITS == 32 ? 112 : 160];
114#ifdef RT_SHA256_PRIVATE_CONTEXT
115 SHA256_CTX Private;
116#endif
117} RTSHA256CONTEXT;
118/** Pointer to an SHA-256 context. */
119typedef RTSHA256CONTEXT *PRTSHA256CONTEXT;
120
121/**
122 * Compute the SHA-256 hash of the data.
123 *
124 * @param pvBuf Pointer to the data.
125 * @param cbBuf The amount of data (in bytes).
126 * @param pabDigest Where to store the hash. (What is passed is a pointer to
127 * the caller's buffer.)
128 */
129RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
130
131/**
132 * Initializes the SHA-256 context.
133 *
134 * @param pCtx Pointer to the SHA-256 context.
135 */
136RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx);
137
138/**
139 * Feed data into the SHA-256 computation.
140 *
141 * @param pCtx Pointer to the SHA-256 context.
142 * @param pvBuf Pointer to the data.
143 * @param cbBuf The length of the data (in bytes).
144 */
145RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
146
147/**
148 * Compute the SHA-256 hash of the data.
149 *
150 * @param pCtx Pointer to the SHA-256 context.
151 * @param pabDigest Where to store the hash. (What is passed is a pointer to
152 * the caller's buffer.)
153 */
154RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE]);
155
156
157/** The size of a SHA-512 hash. */
158#define RTSHA512_HASH_SIZE 64
159
160/**
161 * SHA-512 context.
162 */
163typedef union RTSHA512CONTEXT
164{
165 uint8_t abPadding[ARCH_BITS == 32 ? 216 : 256];
166#ifdef RT_SHA512_PRIVATE_CONTEXT
167 SHA512_CTX Private;
168#endif
169} RTSHA512CONTEXT;
170/** Pointer to an SHA-512 context. */
171typedef RTSHA512CONTEXT *PRTSHA512CONTEXT;
172
173/**
174 * Compute the SHA-512 hash of the data.
175 *
176 * @param pvBuf Pointer to the data.
177 * @param cbBuf The amount of data (in bytes).
178 * @param pabDigest Where to store the hash. (What is passed is a pointer to
179 * the caller's buffer.)
180 */
181RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
182
183/**
184 * Initializes the SHA-512 context.
185 *
186 * @param pCtx Pointer to the SHA-512 context.
187 */
188RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx);
189
190/**
191 * Feed data into the SHA-512 computation.
192 *
193 * @param pCtx Pointer to the SHA-512 context.
194 * @param pvBuf Pointer to the data.
195 * @param cbBuf The length of the data (in bytes).
196 */
197RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf);
198
199/**
200 * Compute the SHA-512 hash of the data.
201 *
202 * @param pCtx Pointer to the SHA-512 context.
203 * @param pabDigest Where to store the hash. (What is passed is a pointer to
204 * the caller's buffer.)
205 */
206RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE]);
207
208/** @} */
209
210RT_C_DECLS_END
211
212#endif /* ___iprt_sha1_h */
213
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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