HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

My own SHA-256 code implementation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sha256owncodeimplementation

Problem

I'm worried about my code performance. The code I've written is an implementation of SHA-256 using pseudocode from Wikipedia. It works fine, but I want to speed it up. With a buffer of 4096 bytes, I only get 40 Mb/s, whereas commercial software gets 120Mb/s. I've degugged the code, and it seems that the problem is in the loop: it computes the SHA-256 slowly.

Can anybody help me or offer advice?

```
void sha256_file(HWND hDlg, TCHAR filename, unsigned int sha256)
{
unsigned long long lenBits, lenBytes, lenPadding, ctBuffer, index, j, i, nBlocks, chunk, pr, temp;
unsigned int w[64], h0, h1, h2, h3, h4, h5, h6, h7, ch, z, ct , memR;
unsigned int s0, s1, a, b, c, d, e, f, g, h, maj, t1, t2;
unsigned char *padding;

TCHAR szPr[20];
HWND hPb;
HANDLE hf, hp;
LARGE_INTEGER fSize;
DWORD bytesRead;
//Create the file
hf = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if(hf == NULL)
{
MessageBox(hDlg, TEXT("Can't create file!"), TEXT("SHA - 256 Calc"), MB_OK);
exit(1);
}
if(GetLastError() == ERROR_FILE_NOT_FOUND)
{
MessageBox(hDlg, TEXT("File not found!"), TEXT("SHA - 256 Calc"), MB_OK | MB_ICONEXCLAMATION);
exit(1);
}
hp = HeapCreate(HEAP_GENERATE_EXCEPTIONS, 0, 0);
if(hp == NULL)
{
MessageBox(hDlg, TEXT("Can't create heap!"), TEXT("SHA - 256 Calc"), MB_OK);
exit(1);
}
//Get Process bar handle
hPb = GetDlgItem(hDlg, IDC_PROGRESS1);

//Set range 0-100
SendMessage(hPb, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
SendMessage(hPb, PBM_SETSTEP, (WPARAM) 1, 0);
SendMessage(hPb, PBM_SETPOS, (WPARAM) 0, 0);

//Get the size of file
GetFileSizeEx(hf, &fSize);

//Get 64 bit size
lenBytes = (unsigned long long)fSize.QuadPart;
//len message in bits
lenBits = lenBytes > 6) + 1;
lenPadding = lenBytes + 1;
//Calculate padding len msg MOD 512 == 448
while((

Solution

-
As a really quick comment, you can do :

for(i = index, index+=64; i < index; i++, j++, ctBuffer++) { (...) }


instead of

for(i = index; i < (index + 64); i++, j++, ctBuffer++) { (...) } index += 64;


to make the code very slightly shorter and faster.

-
Also, to improve readability, please define your variables in the smallest possible scope and as you initialise them (if possible as a const object), such as :

for (unsigned long long chunk = 0; chunk < nBlocks ; chunk++)


or

const HANDLE hf = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);

Code Snippets

for(i = index, index+=64; i < index; i++, j++, ctBuffer++) { (...) }
for(i = index; i < (index + 64); i++, j++, ctBuffer++) { (...) } index += 64;
for (unsigned long long chunk = 0; chunk < nBlocks ; chunk++)
const HANDLE hf = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);

Context

StackExchange Code Review Q#7443, answer score: 5

Revisions (0)

No revisions yet.