VirtualBox

Changeset 23501 in vbox


Ignore:
Timestamp:
Oct 2, 2009 10:59:42 AM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
53140
Message:

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

Location:
trunk
Files:
3 added
3 edited
2 moved

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/include/iprt/sha.h

    r23489 r23501  
    2828 */
    2929
    30 #ifndef ___iprt_sha1_h
    31 #define ___iprt_sha1_h
    32 
    33 #include <iprt/cdefs.h>
     30#ifndef ___iprt_sha_h
     31#define ___iprt_sha_h
     32
     33#include <iprt/types.h>
    3434
    3535RT_C_DECLS_BEGIN
    3636
    37 /** @defgroup grp_rt_sha1digest   RTSha1Digest - SHA1 digest creation
     37/** @defgroup grp_rt_sha   RTSha - SHA Family of Hash Functions
    3838 * @ingroup grp_rt
    3939 * @{
    4040 */
    4141
     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
    4294/**
    4395 * Creates a SHA1 digest for the given file.
     
    50102RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest);
    51103
     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
    52208/** @} */
    53209
  • TabularUnified trunk/src/VBox/Runtime/Makefile.kmk

    r23452 r23501  
    848848        common/misc/s3.cpp \
    849849        r3/xml.cpp \
     850        common/checksum/RTSha1Digest.cpp \
     851        common/checksum/manifest.cpp \
    850852        common/checksum/sha1.cpp \
    851         common/checksum/manifest.cpp
     853        common/checksum/sha256.cpp \
     854        common/checksum/sha512.cpp
    852855VBoxRT_SOURCES.$(KBUILD_TARGET) = $(RuntimeR3_SOURCES.$(KBUILD_TARGET))
    853856VBoxRT_SOURCES.$(KBUILD_TARGET).$(KBUILD_TARGET_ARCH) = $(RuntimeR3_SOURCES.$(KBUILD_TARGET).$(KBUILD_TARGET_ARCH))
  • TabularUnified trunk/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp

    r23489 r23501  
    2929 */
    3030
    31 #include <iprt/sha1.h>
     31
     32/*******************************************************************************
     33*   Header Files                                                               *
     34*******************************************************************************/
     35#include "internal/iprt.h"
     36#include <iprt/sha.h>
     37
     38#include <iprt/assert.h>
     39#include <iprt/err.h>
    3240#include <iprt/stream.h>
    3341#include <iprt/string.h>
    34 #include <iprt/assert.h>
    35 #include <iprt/err.h>
    3642
    3743#include <openssl/sha.h>
    3844
    39 /*******************************************************************************
    40 *   Public RTSha1Digest interface                                              *
    41 *******************************************************************************/
     45
    4246
    4347RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest)
    4448{
    4549    /* Validate input */
    46     if (!pszFile || !ppszDigest)
    47     {
    48         AssertMsgFailed(("Must supply pszFile and ppszDigest!\n"));
    49         return VERR_INVALID_PARAMETER;
    50     }
     50    AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
     51    AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
    5152
    5253    *ppszDigest = NULL;
     
    5657    if (!SHA1_Init(&ctx))
    5758        return VERR_INTERNAL_ERROR;
     59
     60    /** @todo r=bird: Using a stream here doesn't really serve much purpose as
     61     *        few stream implementations uses a buffer much larger than 4KB. (The
     62     *        only I'm aware of is libc on OS/2, which uses 8KB.) */
    5863
    5964    /* Open the file to calculate a SHA1 sum of */
     
    7782            break;
    7883        }
    79     }
    80     while (cbRead > 0);
     84    } while (cbRead > 0);
    8185    RTStrmClose(pStream);
    8286
     
    8589
    8690    /* Finally calculate & format the SHA1 sum */
    87     unsigned char pucDig[20];
    88     if (!SHA1_Final(pucDig, &ctx))
     91    unsigned char auchDig[20];
     92    if (!SHA1_Final(auchDig, &ctx))
    8993        return VERR_INTERNAL_ERROR;
    9094
    91     int cbRet = RTStrAPrintf(ppszDigest, "%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x",
    92                              pucDig[0] , pucDig[1] , pucDig[2] , pucDig[3] , pucDig[4],
    93                              pucDig[5] , pucDig[6] , pucDig[7] , pucDig[8] , pucDig[9],
    94                              pucDig[10], pucDig[11], pucDig[12], pucDig[13], pucDig[14],
    95                              pucDig[15], pucDig[16], pucDig[17], pucDig[18], pucDig[19]);
    96     if (RT_UNLIKELY(cbRet == -1))
     95    int cch = RTStrAPrintf(ppszDigest, "%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x",
     96                           auchDig[0] , auchDig[1] , auchDig[2] , auchDig[3] , auchDig[4],
     97                           auchDig[5] , auchDig[6] , auchDig[7] , auchDig[8] , auchDig[9],
     98                           auchDig[10], auchDig[11], auchDig[12], auchDig[13], auchDig[14],
     99                           auchDig[15], auchDig[16], auchDig[17], auchDig[18], auchDig[19]);
     100    if (RT_UNLIKELY(cch == -1))
    97101        rc = VERR_INTERNAL_ERROR;
    98102
  • TabularUnified trunk/src/VBox/Runtime/common/checksum/manifest.cpp

    r23499 r23501  
    4040#include <iprt/mem.h>
    4141#include <iprt/path.h>
    42 #include <iprt/sha1.h>
     42#include <iprt/sha.h>
    4343#include <iprt/stream.h>
    4444#include <iprt/string.h>
  • TabularUnified trunk/src/VBox/Runtime/testcase/Makefile.kmk

    r23452 r23501  
    5555        tstRTCritSect \
    5656        tstDeadlock \
     57        tstRTDigest \
    5758        tstDir \
    5859        tstDir-2 \
     
    167168tstDeadlock_SOURCES = tstDeadlock.cpp
    168169
     170tstRTDigest_SOURCES = tstRTDigest.cpp
     171
    169172tstDir_SOURCES = tstDir.cpp
    170173
Note: See TracChangeset for help on using the changeset viewer.

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