void CHmac::SetKey(const Byte *key, size_t keySize) { Byte temp[kBlockSize]; size_t i; for (i = 0; i < kBlockSize; i++) temp[i] = 0; if (keySize > kBlockSize) { Sha256_Init(&_sha); Sha256_Update(&_sha, key, keySize); Sha256_Final(&_sha, temp); } else for (i = 0; i < keySize; i++) temp[i] = key[i]; for (i = 0; i < kBlockSize; i++) temp[i] ^= 0x36; Sha256_Init(&_sha); Sha256_Update(&_sha, temp, kBlockSize); for (i = 0; i < kBlockSize; i++) temp[i] ^= 0x36 ^ 0x5C; Sha256_Init(&_sha2); Sha256_Update(&_sha2, temp, kBlockSize); }
void CRandomGenerator::Init() { CSha256 hash; Sha256_Init(&hash); #ifdef _WIN32 DWORD w = ::GetCurrentProcessId(); HASH_UPD(w); w = ::GetCurrentThreadId(); HASH_UPD(w); #else pid_t pid = getpid(); HASH_UPD(pid); pid = getppid(); HASH_UPD(pid); #endif for (unsigned i = 0; i < #ifdef _DEBUG 2; #else 1000; #endif i++) { #ifdef _WIN32 LARGE_INTEGER v; if (::QueryPerformanceCounter(&v)) HASH_UPD(v.QuadPart); #endif #ifdef USE_POSIX_TIME #ifdef USE_POSIX_TIME2 timeval v; if (gettimeofday(&v, 0) == 0) { HASH_UPD(v.tv_sec); HASH_UPD(v.tv_usec); } #endif time_t v2 = time(NULL); HASH_UPD(v2); #endif #ifdef _WIN32 DWORD tickCount = ::GetTickCount(); HASH_UPD(tickCount); #endif for (unsigned j = 0; j < 100; j++) { Sha256_Final(&hash, _buff); Sha256_Init(&hash); Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE); } } Sha256_Final(&hash, _buff); _needInit = false; }
static void Sha256_Final(CSha256 *p, uint8_t *digest) { uint64_t lenInBits = (p->count << 3); uint32_t curBufferPos = (uint32_t)p->count & 0x3F; unsigned i; p->buffer[curBufferPos++] = 0x80; while (curBufferPos != (64 - 8)) { curBufferPos &= 0x3F; if (curBufferPos == 0) Sha256_WriteByteBlock(p); p->buffer[curBufferPos++] = 0; } for (i = 0; i < 8; i++) { p->buffer[curBufferPos++] = (uint8_t)(lenInBits >> 56); lenInBits <<= 8; } Sha256_WriteByteBlock(p); for (i = 0; i < 8; i++) { *digest++ = (uint8_t)(p->state[i] >> 24); *digest++ = (uint8_t)(p->state[i] >> 16); *digest++ = (uint8_t)(p->state[i] >> 8); *digest++ = (uint8_t)(p->state[i]); } Sha256_Init(p); }
/** * @brief Compute a Hash of the data provided using SHA256 and write the result into a buffer * @param[out] hash - buffer to store resulting hash * @param[in] data - pointer to data to hash * @param[in] dataLen - length of data in buffer */ static void Sha256_ComputeHash(uint8_t hash[SHA256_HASH_LENGTH], const uint8_t *data, int dataLen) { Sha256ContextType context; Sha256_Init(&context); Sha256_Update(&context,data, dataLen); Sha256_Final(&context, hash); }
void Sha256_Final(SHA256Context *p, byte *digest) { ulong64 lenInBits = (p->count << 3); ulong32 curBufferPos = (ulong32)p->count & 0x3F; unsigned i; p->buffer[curBufferPos++] = 0x80; while (curBufferPos != (64 - 8)) { curBufferPos &= 0x3F; if (curBufferPos == 0) Sha256_WritebyteBlock(p); p->buffer[curBufferPos++] = 0; } for (i = 0; i < 8; i++) { p->buffer[curBufferPos++] = (byte)(lenInBits >> 56); lenInBits <<= 8; } Sha256_WritebyteBlock(p); for (i = 0; i < 8; i++) { *digest++ = (byte)(p->state[i] >> 24); *digest++ = (byte)(p->state[i] >> 16); *digest++ = (byte)(p->state[i] >> 8); *digest++ = (byte)(p->state[i]); } Sha256_Init(p); }
void Sha256_Onestep(const uint8_t *data, size_t size, uint8_t *digest) { CSha256 p; Sha256_Init(&p); Sha256_Update(&p, data, size); Sha256_Final(&p, digest); }
void Sha256_Final(CSha256 *p, Byte *digest) { UInt64 lenInBits = (p->count << 3); UInt32 curBufferPos = (UInt32)p->count & 0x3F; unsigned i; p->buffer[curBufferPos++] = 0x80; while (curBufferPos != (64 - 8)) { curBufferPos &= 0x3F; if (curBufferPos == 0) Sha256_WriteByteBlock(p); p->buffer[curBufferPos++] = 0; } for (i = 0; i < 8; i++) { p->buffer[curBufferPos++] = (Byte)(lenInBits >> 56); lenInBits <<= 8; } Sha256_WriteByteBlock(p); for (i = 0; i < 8; i++) { *digest++ = (Byte)((p->state[i] >> 24) & 0xFF); *digest++ = (Byte)((p->state[i] >> 16) & 0xFF); *digest++ = (Byte)((p->state[i] >> 8) & 0xFF); *digest++ = (Byte)((p->state[i]) & 0xFF); } Sha256_Init(p); }
void Sha256_Final(CSha256 *p, Byte *digest) { unsigned pos = (unsigned)p->count & 0x3F; unsigned i; p->buffer[pos++] = 0x80; while (pos != (64 - 8)) { pos &= 0x3F; if (pos == 0) Sha256_WriteByteBlock(p); p->buffer[pos++] = 0; } { UInt64 numBits = (p->count << 3); SetBe32(p->buffer + 64 - 8, (UInt32)(numBits >> 32)); SetBe32(p->buffer + 64 - 4, (UInt32)(numBits)); } Sha256_WriteByteBlock(p); for (i = 0; i < 8; i += 2) { UInt32 v0 = p->state[i]; UInt32 v1 = p->state[i + 1]; SetBe32(digest , v0); SetBe32(digest + 4, v1); digest += 8; } Sha256_Init(p); }
unsigned char *Sha256_Data (const unsigned char *data, unsigned int len, unsigned char *buf) { CSha256 ctx; Sha256_Init(&ctx); Sha256_Update(&ctx, data, len); Sha256_Final(&ctx, buf); return buf; }
void XzCheck_Init(CXzCheck *p, int mode) { p->mode = mode; switch (mode) { case XZ_CHECK_CRC32: p->crc = CRC_INIT_VAL; break; case XZ_CHECK_CRC64: p->crc64 = CRC64_INIT_VAL; break; case XZ_CHECK_SHA256: Sha256_Init(&p->sha); break; } }
void CRandomGenerator::Generate(Byte *data, unsigned size) { g_CriticalSection.Enter(); if (_needInit) Init(); while (size > 0) { CSha256 hash; Sha256_Init(&hash); Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE); Sha256_Final(&hash, _buff); Sha256_Init(&hash); UInt32 salt = 0xF672ABD1; HASH_UPD(salt); Sha256_Update(&hash, _buff, SHA256_DIGEST_SIZE); Byte buff[SHA256_DIGEST_SIZE]; Sha256_Final(&hash, buff); for (unsigned i = 0; i < SHA256_DIGEST_SIZE && size > 0; i++, size--) *data++ = buff[i]; } g_CriticalSection.Leave(); }
std::string Sha256(const std::string &s) { uint8_t digest[32]; CSha256 p; Sha256_Init(&p); Sha256_Update(&p, (const uint8_t*)s.data(), s.length()); Sha256_Final(&p, digest); std::stringstream ss; ss << std::setfill('0') << std::hex; for (int i = 0; i < 32; i++) ss << std::setw(2) << (int)digest[i]; return ss.str(); }
/////////////////////////////////////////////////////////////////////////////// // SHA256 of file // Input: // lpFileName : full path of plugin // Output: // sha256 : SHA256 in plain-text (lower case) // BOOL SHA256_Array(char *lpOutChecksum, void *array, int size) { SHA256Context context; Sha256_Init(&context); Sha256_Update(&context, (byte *) array, (size_t) size); unsigned char sha256_digest[32]; Sha256_Final(&context, sha256_digest); hex2ascii(lpOutChecksum, (char *) sha256_digest, sizeof(sha256_digest)); return TRUE; }
/////////////////////////////////////////////////////////////////////////////// // SHA256 of file // Input: // lpFileName : full path of plugin // Output: // sha256 : SHA256 in plain-text (lower case) // BOOL SHA256_Plugin(char *lpFileName, char *lpOutChecksum, BOOL isOld) { if (lpFileName == NULL || lpOutChecksum == NULL) return FALSE; HANDLE hFile = CreateFile(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (hFile == INVALID_HANDLE_VALUE) return FALSE; SHA256Context context; Sha256_Init(&context); void *buffer = malloc(64000); DWORD dwBytesRead = 0; while(ReadFile(hFile, buffer, 64000, &dwBytesRead, NULL) == TRUE) { if (dwBytesRead == 0) // end of file? break; Sha256_Update(&context, (byte *) buffer, (size_t) dwBytesRead); } CloseHandle(hFile); free(buffer); unsigned char sha256_digest[32]; Sha256_Final(&context, sha256_digest); wchar_t unicodesha[32]; if (isOld) { MultiByteToWideChar(CP_ACP, 0, (LPCSTR) sha256_digest, sizeof(sha256_digest), unicodesha, 32); hex2ascii(lpOutChecksum, unicodesha, 32); } else { hex2ascii(lpOutChecksum, (char *)sha256_digest, 32); } return TRUE; }
static PyObject * pylzma_calculate_key(PyObject *self, PyObject *args, PyObject *kwargs) { char *password; PARSE_LENGTH_TYPE pwlen; int cycles; PyObject *pysalt=NULL; char *salt; Py_ssize_t saltlen; char *digest="sha256"; char key[32]; // possible keywords for this function static char *kwlist[] = {"password", "cycles", "salt", "digest", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#i|Os", kwlist, &password, &pwlen, &cycles, &pysalt, &digest)) return NULL; if (pysalt == Py_None) { pysalt = NULL; } else if (!PyBytes_Check(pysalt)) { PyErr_Format(PyExc_TypeError, "salt must be a string, got a %s", pysalt->ob_type->tp_name); return NULL; } if (strcmp(digest, "sha256") != 0) { PyErr_Format(PyExc_TypeError, "digest %s is unsupported", digest); return NULL; } if (pysalt != NULL) { salt = PyBytes_AS_STRING(pysalt); saltlen = PyBytes_Size(pysalt); } else { salt = NULL; saltlen = 0; } if (cycles == 0x3f) { int pos; int i; for (pos = 0; pos < saltlen; pos++) key[pos] = salt[pos]; for (i = 0; i<pwlen && pos < 32; i++) key[pos++] = password[i]; for (; pos < 32; pos++) key[pos] = 0; } else { CSha256 sha; long round; int i; long rounds = (long) 1 << cycles; unsigned char temp[8] = { 0,0,0,0,0,0,0,0 }; Py_BEGIN_ALLOW_THREADS Sha256_Init(&sha); for (round = 0; round < rounds; round++) { Sha256_Update(&sha, (Byte *) salt, saltlen); Sha256_Update(&sha, (Byte *) password, pwlen); Sha256_Update(&sha, (Byte *) temp, 8); for (i = 0; i < 8; i++) if (++(temp[i]) != 0) break; } Sha256_Final(&sha, (Byte *) &key); Py_END_ALLOW_THREADS } return PyBytes_FromStringAndSize(key, 32); }
void Data::calcSHA256() { CSha256 p; Sha256_Init(&p); Sha256_Update(&p, DecompressedData, decompressedSize); Sha256_Final(&p, actualDigest); }
STDMETHODIMP_(void) CSha256Hasher::Init() throw() { Sha256_Init(&_sha); }