📄 sha2_h.old
字号:
/*
---------------------------------------------------------------------------
Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
LICENSE TERMS
The free distribution and use of this software in both source and binary
form is allowed (with or without changes) provided that:
1. distributions of this source code include the above copyright
notice, this list of conditions and the following disclaimer;
2. distributions in binary form include the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other associated materials;
3. the copyright holder's name is not used to endorse products
built using this software without specific written permission.
ALTERNATIVELY, provided that this notice is retained in full, this product
may be distributed under the terms of the GNU General Public License (GPL),
in which case the provisions of the GPL apply INSTEAD OF those given above.
DISCLAIMER
This software is provided 'as is' with no explicit or implied warranties
in respect of its properties, including, but not limited to, correctness
and/or fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 16/01/2004
*/
#ifndef _SHA2_H
#define _SHA2_H
#include <limits.h>
/* Many systems don't support 64-bit ints yet, in which case only SHA-256 can
be used */
/* #define SHA_224 */
#define SHA_256
/* #define SHA_384 */
/* #define SHA_512 */
#if defined( SHA_384 ) || defined( SHA_512 )
#define USE_64BIT_INT
#endif /* SHA-384 || SHA-512 */
/* define an unsigned 32-bit type */
#if defined(_MSC_VER)
typedef unsigned long sha2_32t;
#elif defined(ULONG_MAX) && ULONG_MAX == 4294967295ul
typedef unsigned long sha2_32t;
#elif defined(UINT_MAX) && UINT_MAX == 4294967295
typedef unsigned int sha2_32t;
#else
# error Please define sha2_32t as an unsigned 32 bit type in sha2.h
#endif
/* define an unsigned 64-bit type (thanks go to Mark Shelor */
/* and Olaf Pors for their help in developing and testing */
/* this). Note that li_64(h) is needed to substitute 'ui64' */
/* for 'ull' as the suffix for defining 64-bit literals in */
/* Microsoft VC++ versions below 7.1 (with _MSCVER < 1310) */
#ifdef USE_64BIT_INT
#define li_64(h) 0x##h##ull
#if defined( _MSC_VER )
# if _MSC_VER < 1310
typedef unsigned __int64 sha2_64t;
# undef li_64
# define li_64(h) 0x##h##ui64
# else
typedef unsigned long long sha2_64t;
# endif
#elif defined( __sun ) && defined(ULONG_MAX) && ULONG_MAX == 0xfffffffful
typedef unsigned long long sha2_64t; /* hack for Sun 32-bit case */
#elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX == 0xffffffffffffffffull
typedef unsigned long long sha2_64t;
#elif defined( ULLONG_MAX ) && ULLONG_MAX == 0xffffffffffffffffull
typedef unsigned long long sha2_64t;
#elif defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffffffffffful
typedef unsigned long sha2_64t;
#elif defined( UINT_MAX ) && UINT_MAX == 0xffffffffffffffff
typedef unsigned int sha2_64t;
#else
# error Please define sha2_64t as an unsigned 64 bit type in sha2.h
#endif
#endif /* USE_64BIT_INT */
#if defined(__cplusplus)
extern "C"
{
#endif
#define SHA224_DIGEST_SIZE 28
#define SHA256_DIGEST_SIZE 32
#define SHA384_DIGEST_SIZE 48
#define SHA512_DIGEST_SIZE 64
#define SHA224_BLOCK_SIZE 64
#define SHA256_BLOCK_SIZE 64
#define SHA384_BLOCK_SIZE 128
#define SHA512_BLOCK_SIZE 128
#define SHA2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
#define SHA2_GOOD 0
#define SHA2_BAD 1
/* type to hold the SHA256 (and SHA224) context */
typedef struct
{ sha2_32t count[2];
sha2_32t hash[8];
sha2_32t wbuf[16];
} sha256_ctx;
typedef sha256_ctx sha224_ctx;
/* type to hold the SHA384 (and SHA512) context */
#ifdef USE_64BIT_INT
typedef struct
{ sha2_64t count[2];
sha2_64t hash[8];
sha2_64t wbuf[16];
} sha512_ctx;
typedef sha512_ctx sha384_ctx;
#endif /* USE_64BIT_INT */
/* type to hold a SHA2 context (256/384/512) */
typedef struct
{ union
{ sha256_ctx ctx256[1];
#ifdef USE_64BIT_INT
sha512_ctx ctx512[1];
#endif /* USE_64BIT_INT */
} uu[1];
sha2_32t sha2_len;
} sha2_ctx;
#ifndef SHA2_DLL /* implement normal or DLL functions */
#define sha2_void void
#define sha2_int int
#else
#define sha2_void void __declspec(dllexport) _stdcall
#define sha2_int int __declspec(dllexport) _stdcall
#endif
/* Note that these prototypes are the same for both bit and */
/* byte oriented implementations. However the length fields */
/* are in bytes or bits as appropriate for the version used */
/* and bit sequences are input as arrays of bytes in which */
/* bit sequence indexes increase from the most to the least */
/* significant end of each byte */
sha2_void sha256_compile(sha256_ctx ctx[1]);
#ifdef USE_64BIT_INT
sha2_void sha512_compile(sha512_ctx ctx[1]);
#endif /* USE_64BIT_INT */
sha2_void sha224_begin(sha224_ctx ctx[1]);
#define sha224_hash sha256_hash
sha2_void sha224_end(unsigned char hval[], sha224_ctx ctx[1]);
sha2_void sha224(unsigned char hval[], const unsigned char data[], unsigned long len);
sha2_void sha256_begin(sha256_ctx ctx[1]);
sha2_void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
sha2_void sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
sha2_void sha256(unsigned char hval[], const unsigned char data[], unsigned long len);
#ifdef USE_64BIT_INT
sha2_void sha384_begin(sha384_ctx ctx[1]);
#define sha384_hash sha512_hash
sha2_void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
sha2_void sha384(unsigned char hval[], const unsigned char data[], unsigned long len);
sha2_void sha512_begin(sha512_ctx ctx[1]);
sha2_void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
sha2_void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
sha2_void sha512(unsigned char hval[], const unsigned char data[], unsigned long len);
#endif /* USE_64BIT_INT */
sha2_int sha2_begin(unsigned long size, sha2_ctx ctx[1]);
sha2_void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
sha2_void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
sha2_int sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);
#if defined(__cplusplus)
}
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -