]> FriiDump Source - friidump.git/blob - libmultihash/sha2.h
FriiDump 0.5.3.16 candidate: add native reports and Linux Xbox support
[friidump.git] / libmultihash / sha2.h
1 #ifndef __SHA2_H__
2 #define __SHA2_H__
3 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4 //  WjCryptLib_Sha256
5 //
6 //  Implementation of SHA256 hash function.
7 //  Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
8 //  Modified by WaterJuice retaining Public Domain license.
9 //
10 //  This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
11 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
12
13 #pragma once
14
15 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 //  IMPORTS
17 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18
19 #include <stdint.h>
20 #include <stdio.h>
21
22 typedef struct
23 {
24     uint64_t    length;
25     uint32_t    state[8];
26     uint32_t    curlen;
27     uint8_t     buf[64];
28 } Sha256Context;
29
30 #define SHA256_HASH_SIZE           ( 256 / 8 )
31
32 typedef struct
33 {
34     uint8_t      bytes [SHA256_HASH_SIZE];
35 } SHA256_HASH;
36
37 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38 //  PUBLIC FUNCTIONS
39 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
40
41 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
42 //  Sha256Initialise
43 //
44 //  Initialises a SHA256 Context. Use this to initialise/reset a context.
45 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
46 void
47     Sha256Initialise
48     (
49         Sha256Context*      Context         // [out]
50     );
51
52 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53 //  Sha256Update
54 //
55 //  Adds data to the SHA256 context. This will process the data and update the internal state of the context. Keep on
56 //  calling this function until all the data has been added. Then call Sha256Finalise to calculate the hash.
57 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
58 void
59     Sha256Update
60     (
61         Sha256Context*      Context,        // [in out]
62         void const*         Buffer,         // [in]
63         uint32_t            BufferSize      // [in]
64     );
65
66 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67 //  Sha256Finalise
68 //
69 //  Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash). After
70 //  calling this, Sha256Initialised must be used to reuse the context.
71 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72 void
73     Sha256Finalise
74     (
75         Sha256Context*      Context,        // [in out]
76         SHA256_HASH*        Digest          // [out]
77     );
78
79 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80 //  Sha256Calculate
81 //
82 //  Combines Sha256Initialise, Sha256Update, and Sha256Finalise into one function. Calculates the SHA256 hash of the
83 //  buffer.
84 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85 void
86     Sha256Calculate
87     (
88         void  const*        Buffer,         // [in]
89         uint32_t            BufferSize,     // [in]
90         SHA256_HASH*        Digest          // [in]
91     );
92 #endif /* __SHA2_H__ */