Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void CSHA1::Update(const UINT_8* pbData, UINT_32 uLen)
- {
- UINT_32 j = ((m_count[0] >> 3) & 0x3F);
- if((m_count[0] += (uLen << 3)) < (uLen << 3))
- ++m_count[1]; // Overflow
- m_count[1] += (uLen >> 29);
- UINT_32 i;
- if((j + uLen) > 63)
- {
- i = 64 - j;
- memcpy(&m_buffer[j], pbData, i);
- Transform(m_state, m_buffer);
- for( ; (i + 63) < uLen; i += 64)
- Transform(m_state, &pbData[i]);
- j = 0;
- }
- else i = 0;
- if((uLen - i) != 0)
- memcpy(&m_buffer[j], &pbData[i], uLen - i);
- }
- #ifdef SHA1_UTILITY_FUNCTIONS
- // Hash in file contents
- bool CSHA1::HashFile(const TCHAR* tszFileName)
- {
- if(tszFileName == NULL) return false;
- FILE* fpIn = _tfopen(tszFileName, _T("rb"));
- if(fpIn == NULL) return false;
- _fseeki64(fpIn, 0, SEEK_END);
- const INT_64 lFileSize = _ftelli64(fpIn);
- _fseeki64(fpIn, 0, SEEK_SET);
- const INT_64 lMaxBuf = SHA1_MAX_FILE_BUFFER;
- UINT_8 vData[SHA1_MAX_FILE_BUFFER];
- INT_64 lRemaining = lFileSize;
- while(lRemaining > 0)
- {
- const size_t uMaxRead = static_cast<size_t>((lRemaining > lMaxBuf) ?
- lMaxBuf : lRemaining);
- const size_t uRead = fread(vData, 1, uMaxRead, fpIn);
- if(uRead == 0)
- {
- fclose(fpIn);
- return false;
- }
- Update(vData, static_cast<UINT_32>(uRead));
- lRemaining -= static_cast<INT_64>(uRead);
- }
- fclose(fpIn);
- return (lRemaining == 0);
- }
- #endif
- void CSHA1::Final()
- {
- UINT_32 i;
- UINT_8 finalcount[8];
- for(i = 0; i < 8; ++i)
- finalcount[i] = (UINT_8)((m_count[((i >= 4) ? 0 : 1)]
- >> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
- Update((UINT_8*)"\200", 1);
- while ((m_count[0] & 504) != 448)
- Update((UINT_8*)"\0", 1);
- Update(finalcount, 8); // Cause a SHA1Transform()
- for(i = 0; i < 20; ++i)
- m_digest[i] = (UINT_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8)) & 0xFF);
- // Wipe variables for security reasons
- #ifdef SHA1_WIPE_VARIABLES
- memset(m_buffer, 0, 64);
- memset(m_state, 0, 20);
- memset(m_count, 0, 8);
- memset(finalcount, 0, 8);
- Transform(m_state, m_buffer);
- #endif
- }
Add Comment
Please, Sign In to add comment