Audacity 3.2.0
Public Member Functions | Static Public Attributes | Private Attributes | List of all members
crypto::SHA256 Class Referencefinal

#include <SHA256.h>

Public Member Functions

 SHA256 ()
 
 SHA256 (const SHA256 &)=delete
 
 SHA256 (SHA256 &&)=delete
 
SHA256operator= (const SHA256 &)=delete
 
SHA256operator= (SHA256 &&)=delete
 
void Update (const void *data, std::size_t size)
 
void Update (const char *zString)
 
template<typename T >
void Update (const T &data)
 
std::string Finalize ()
 
void Reset ()
 

Static Public Attributes

static constexpr std::size_t HASH_SIZE = 32
 
static constexpr std::size_t BLOCK_SIZE = 64
 

Private Attributes

uint64_t mBitLength
 
uint32_t mState [8]
 
uint8_t mBuffer [BLOCK_SIZE]
 
uint32_t mBufferLength
 

Detailed Description

Definition at line 16 of file SHA256.h.

Constructor & Destructor Documentation

◆ SHA256() [1/3]

crypto::SHA256::SHA256 ( )

Definition at line 93 of file SHA256.cpp.

94{
95 Reset();
96}
void Reset()
Definition: SHA256.cpp:190

References Reset().

Here is the call graph for this function:

◆ SHA256() [2/3]

crypto::SHA256::SHA256 ( const SHA256 )
delete

◆ SHA256() [3/3]

crypto::SHA256::SHA256 ( SHA256 &&  )
delete

Member Function Documentation

◆ Finalize()

std::string crypto::SHA256::Finalize ( )

Definition at line 127 of file SHA256.cpp.

128{
129 uint8_t pad[SHA256::BLOCK_SIZE];
130 std::size_t padLength;
131
132 // `mBufferLength` is always less than SHA256::BLOCK_SIZE. See `Update`
133 // method.
135
137
138 if (mBufferLength < 56)
139 {
140 mBuffer[mBufferLength++] = 0x80;
141 std::memset(mBuffer + mBufferLength, 0, 56 - mBufferLength);
142 }
143 else
144 {
145 mBuffer[mBufferLength++] = 0x80;
146 std::memset(
149 std::memset(mBuffer, 0, 56);
150 }
151
152
153 mBuffer[56] = (mBitLength >> 56) & 0xff;
154 mBuffer[57] = (mBitLength >> 48) & 0xff;
155 mBuffer[58] = (mBitLength >> 40) & 0xff;
156 mBuffer[59] = (mBitLength >> 32) & 0xff;
157 mBuffer[60] = (mBitLength >> 24) & 0xff;
158 mBuffer[61] = (mBitLength >> 16) & 0xff;
159 mBuffer[62] = (mBitLength >> 8) & 0xff;
160 mBuffer[63] = (mBitLength >> 0) & 0xff;
161
163
164 uint8_t result[SHA256::HASH_SIZE];
165
166 for (int i = 0; i < 8; ++i)
167 {
168 result[i * 4 + 0] = (mState[i] >> 24) & 0xff;
169 result[i * 4 + 1] = (mState[i] >> 16) & 0xff;
170 result[i * 4 + 2] = (mState[i] >> 8) & 0xff;
171 result[i * 4 + 3] = (mState[i] >> 0) & 0xff;
172 }
173
174 Reset();
175
176 // Convert to hex string
177 constexpr char hexChars[] = "0123456789ABCDEF";
178 std::string resultStr;
179 resultStr.resize(HASH_SIZE * 2);
180
181 for (int i = 0; i < SHA256::HASH_SIZE; ++i)
182 {
183 resultStr[i * 2 + 0] = hexChars[(result[i] >> 4) & 0xf];
184 resultStr[i * 2 + 1] = hexChars[result[i] & 0xf];
185 }
186
187 return resultStr;
188}
uint8_t mBuffer[BLOCK_SIZE]
Definition: SHA256.h:45
static constexpr std::size_t HASH_SIZE
Definition: SHA256.h:19
uint32_t mState[8]
Definition: SHA256.h:44
static constexpr std::size_t BLOCK_SIZE
Definition: SHA256.h:20
uint64_t mBitLength
Definition: SHA256.h:43
uint32_t mBufferLength
Definition: SHA256.h:46
void sha256_transform(uint32_t state[8], const uint8_t data[64])
Definition: SHA256.cpp:43

References BLOCK_SIZE, HASH_SIZE, mBitLength, mBuffer, mBufferLength, mState, Reset(), and crypto::anonymous_namespace{SHA256.cpp}::sha256_transform().

Referenced by crypto::sha256().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=() [1/2]

SHA256 & crypto::SHA256::operator= ( const SHA256 )
delete

◆ operator=() [2/2]

SHA256 & crypto::SHA256::operator= ( SHA256 &&  )
delete

◆ Reset()

void crypto::SHA256::Reset ( )

Definition at line 190 of file SHA256.cpp.

191{
192 mBitLength = 0;
193
194 mState[0] = 0x6a09e667;
195 mState[1] = 0xbb67ae85;
196 mState[2] = 0x3c6ef372;
197 mState[3] = 0xa54ff53a;
198 mState[4] = 0x510e527f;
199 mState[5] = 0x9b05688c;
200 mState[6] = 0x1f83d9ab;
201 mState[7] = 0x5be0cd19;
202
203 std::memset(mBuffer, 0, sizeof(mBuffer));
204 mBufferLength = 0;
205}

References mBitLength, mBuffer, mBufferLength, and mState.

Referenced by Finalize(), and SHA256().

Here is the caller graph for this function:

◆ Update() [1/3]

void crypto::SHA256::Update ( const char *  zString)

Definition at line 122 of file SHA256.cpp.

123{
124 Update(zString, std::strlen(zString));
125}
void Update(const void *data, std::size_t size)
Definition: SHA256.cpp:98

References Update().

Here is the call graph for this function:

◆ Update() [2/3]

template<typename T >
void crypto::SHA256::Update ( const T &  data)
inline

Definition at line 33 of file SHA256.h.

34 {
35 Update(data.data(), data.size());
36 }

◆ Update() [3/3]

void crypto::SHA256::Update ( const void *  data,
std::size_t  size 
)

Definition at line 98 of file SHA256.cpp.

99{
100 const uint8_t* dataPtr = static_cast<const uint8_t*>(data);
101
102 while (size > 0)
103 {
104 std::size_t blockSize =
105 std::min<size_t>(size, SHA256::BLOCK_SIZE - mBufferLength);
106
107 std::memcpy(mBuffer + mBufferLength, dataPtr, blockSize);
108
109 mBufferLength += blockSize;
110 dataPtr += blockSize;
111 size -= blockSize;
112
114 {
116 mBitLength += 512;
117 mBufferLength = 0;
118 }
119 }
120}

References BLOCK_SIZE, mBitLength, mBuffer, mBufferLength, mState, crypto::anonymous_namespace{SHA256.cpp}::sha256_transform(), and size.

Referenced by crypto::sha256(), and Update().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ BLOCK_SIZE

constexpr std::size_t crypto::SHA256::BLOCK_SIZE = 64
staticconstexpr

◆ HASH_SIZE

constexpr std::size_t crypto::SHA256::HASH_SIZE = 32
staticconstexpr

Definition at line 19 of file SHA256.h.

Referenced by Finalize().

◆ mBitLength

uint64_t crypto::SHA256::mBitLength
private

Definition at line 43 of file SHA256.h.

Referenced by Finalize(), Reset(), and Update().

◆ mBuffer

uint8_t crypto::SHA256::mBuffer[BLOCK_SIZE]
private

Definition at line 45 of file SHA256.h.

Referenced by Finalize(), Reset(), and Update().

◆ mBufferLength

uint32_t crypto::SHA256::mBufferLength
private

Definition at line 46 of file SHA256.h.

Referenced by Finalize(), Reset(), and Update().

◆ mState

uint32_t crypto::SHA256::mState[8]
private

Definition at line 44 of file SHA256.h.

Referenced by Finalize(), Reset(), and Update().


The documentation for this class was generated from the following files: