RINASim  October 2016
Documentation of framework for OMNeT++
SHA256.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2014-2016 Brno University of Technology, PRISTINE project
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 
23 #ifndef SHA256_H_
24 #define SHA256_H_
25 
26 #include <cstring>
27 #include <string>
28 #include <cstdio>
29 
30 class SHA256 {
31  public:
32  SHA256();
33  virtual ~SHA256();
34 
35  protected:
36  typedef unsigned char uint8;
37  typedef unsigned int uint32;
38  typedef unsigned long long uint64;
39 
40  const static uint32 sha256_k[];
41  static const unsigned int SHA224_256_BLOCK_SIZE = (512/8);
42  public:
43  void init();
44  void update(const unsigned char *message, unsigned int len);
45  void final(unsigned char *digest);
46  static const unsigned int DIGEST_SIZE = ( 256 / 8);
47 
48  protected:
49  void transform(const unsigned char *message, unsigned int block_nb);
50  unsigned int m_tot_len;
51  unsigned int m_len;
52  unsigned char m_block[2*SHA224_256_BLOCK_SIZE];
53  uint32 m_h[8];
54 
55 };
56 
57 
58 std::string sha256(std::string input);
59 
60 #define SHA2_SHFR(x, n) (x >> n)
61 #define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
62 #define SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
63 #define SHA2_CH(x, y, z) ((x & y) ^ (~x & z))
64 #define SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
65 #define SHA256_F1(x) (SHA2_ROTR(x, 2) ^ SHA2_ROTR(x, 13) ^ SHA2_ROTR(x, 22))
66 #define SHA256_F2(x) (SHA2_ROTR(x, 6) ^ SHA2_ROTR(x, 11) ^ SHA2_ROTR(x, 25))
67 #define SHA256_F3(x) (SHA2_ROTR(x, 7) ^ SHA2_ROTR(x, 18) ^ SHA2_SHFR(x, 3))
68 #define SHA256_F4(x) (SHA2_ROTR(x, 17) ^ SHA2_ROTR(x, 19) ^ SHA2_SHFR(x, 10))
69 #define SHA2_UNPACK32(x, str) \
70 { \
71  *((str) + 3) = (uint8) ((x) ); \
72  *((str) + 2) = (uint8) ((x) >> 8); \
73  *((str) + 1) = (uint8) ((x) >> 16); \
74  *((str) + 0) = (uint8) ((x) >> 24); \
75 }
76 #define SHA2_PACK32(str, x) \
77 { \
78  *(x) = ((uint32) *((str) + 3) ) \
79  | ((uint32) *((str) + 2) << 8) \
80  | ((uint32) *((str) + 1) << 16) \
81  | ((uint32) *((str) + 0) << 24); \
82 }
83 
84 #endif /* SHA256_H_ */
Definition: SHA256.h:30
static const unsigned int DIGEST_SIZE
Definition: SHA256.h:46
void init()
Definition: SHA256.cc:90
unsigned char m_block[2 *SHA224_256_BLOCK_SIZE]
Definition: SHA256.h:52
std::string sha256(std::string input)
Definition: SHA256.cc:146
void transform(const unsigned char *message, unsigned int block_nb)
Definition: SHA256.cc:52
static const unsigned int SHA224_256_BLOCK_SIZE
Definition: SHA256.h:41
uint32 m_h[8]
Definition: SHA256.h:53
static const uint32 sha256_k[]
Definition: SHA256.h:40
unsigned int m_tot_len
Definition: SHA256.h:50
unsigned int m_len
Definition: SHA256.h:51
unsigned int uint32
Definition: SHA256.h:37
SHA256()
Definition: SHA256.cc:25
unsigned char uint8
Definition: SHA256.h:36
void update(const unsigned char *message, unsigned int len)
Definition: SHA256.cc:104
virtual ~SHA256()
Definition: SHA256.cc:30
unsigned long long uint64
Definition: SHA256.h:38