We are working with signed long (made this explicit) so use fromLong instead of fromInt.
[UnsignedByte.git] / src / Resource / sha2.h
blobd862bad45ab6e01b759d697457c44bda867be249
1 /**
2 * @file sha2.h
3 * This file contains methods required to calculate SHA-256 hashes.
4 */
5 #ifndef _SHA2_H
6 #define _SHA2_H
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
12 /**
13 * \brief SHA-256 context structure
15 typedef struct
17 unsigned long total[2]; /*!< number of bytes processed */
18 unsigned long state[8]; /*!< intermediate digest state */
19 unsigned char buffer[64]; /*!< data block being processed */
20 unsigned char ipad[64]; /*!< HMAC: inner padding */
21 unsigned char opad[64]; /*!< HMAC: outer padding */
22 int is224; /*!< 0 if SHA-256, 1 if SHA-224 */
24 sha2_context;
26 /**
27 * \brief SHA-256 context setup
29 * \param ctx context to be initialized
30 * \param is224 0 = use SHA256, 1 = use SHA224
32 void sha2_starts( sha2_context *ctx, int is224 );
34 /**
35 * \brief SHA-256 process buffer
37 * \param ctx SHA-256 context
38 * \param input buffer holding the data
39 * \param ilen length of the input data
41 void sha2_update( sha2_context *ctx, unsigned char *input, int ilen );
43 /**
44 * \brief SHA-256 final digest
46 * \param ctx SHA-256 context
47 * \param output SHA-224/256 checksum result
49 void sha2_finish( sha2_context *ctx, unsigned char *output );
51 /**
52 * \brief Output = SHA-256( input buffer )
54 * \param input buffer holding the data
55 * \param ilen length of the input data
56 * \param output SHA-224/256 checksum result
57 * \param is224 0 = use SHA256, 1 = use SHA224
59 void sha2( unsigned char *input, int ilen,
60 unsigned char *output, int is224 );
62 /**
63 * \brief Output = SHA-256( file contents )
65 * \param path input file name
66 * \param output SHA-224/256 checksum result
67 * \param is224 0 = use SHA256, 1 = use SHA224
69 * \return 0 if successful, 1 if fopen failed,
70 * or 2 if fread failed
72 int sha2_file( char *path, unsigned char *output, int is224 );
74 /**
75 * \brief SHA-256 HMAC context setup
77 * \param ctx HMAC context to be initialized
78 * \param is224 0 = use SHA256, 1 = use SHA224
79 * \param key HMAC secret key
80 * \param keylen length of the HMAC key
82 void sha2_hmac_starts( sha2_context *ctx, int is224,
83 unsigned char *key, int keylen );
85 /**
86 * \brief SHA-256 HMAC process buffer
88 * \param ctx HMAC context
89 * \param input buffer holding the data
90 * \param ilen length of the input data
92 void sha2_hmac_update( sha2_context *ctx,
93 unsigned char *input, int ilen );
95 /**
96 * \brief SHA-256 HMAC final digest
98 * \param ctx HMAC context
99 * \param output SHA-224/256 HMAC checksum result
101 void sha2_hmac_finish( sha2_context *ctx, unsigned char *output );
104 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
106 * \param key HMAC secret key
107 * \param keylen length of the HMAC key
108 * \param input buffer holding the data
109 * \param ilen length of the input data
110 * \param output HMAC-SHA-224/256 result
111 * \param is224 0 = use SHA256, 1 = use SHA224
113 void sha2_hmac( unsigned char *key, int keylen,
114 unsigned char *input, int ilen,
115 unsigned char *output, int is224 );
118 * \brief Checkup routine
120 * \return 0 if successful, or 1 if the test failed
122 int sha2_self_test( int verbose );
124 #ifdef __cplusplus
126 #endif
128 #endif /* sha2.h */