std::string vmsHash::Hash_SHA2(LPCSTR pszFile) { sha2_ctx sha2; vmsFile file; __int64 trb = 0; BYTE abBuf [16000]; sha2_begin (m_nSHA2Strength, &sha2); try { file.Open (pszFile, TRUE); __int64 uSize = file.get_Size (); DWORD dwRead; while (dwRead = file.Read (abBuf, sizeof (abBuf))) { sha2_hash (abBuf, dwRead, &sha2); trb += dwRead; if (m_pEvents) { double f = (double)trb / uSize * 100; if (false == m_pEvents->OnProgressChanged (f)) { trb = 0; break; } } } BYTE abRes [256]; sha2_end (abRes, &sha2); if (trb == 0 && uSize != 0) return ""; return Hash_ResultToStr (abRes, m_nSHA2Strength / 8); }catch (...) {return "";} }
static foreign_t pl_sha_hash_ctx(term_t old_ctx, term_t from, term_t new_ctx, term_t hash) { char *data; size_t datalen; struct context *cp; size_t clen; unsigned char hval[SHA2_MAX_DIGEST_SIZE]; if ( !PL_get_nchars(from, &datalen, &data, CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) ) return FALSE; if ( !PL_get_string_chars(old_ctx, (char **)&cp, &clen) ) return FALSE; if ( clen != sizeof (*cp) || cp->magic != CONTEXT_MAGIC ) { return pl_error(NULL, 0, "Invalid OldContext passed", ERR_DOMAIN, old_ctx, "algorithm"); } if ( cp->opts.algorithm == ALGORITHM_SHA1 ) { sha1_ctx *c1p = &(cp->context.sha1); sha1_hash((unsigned char*)data, (unsigned long)datalen, c1p); if ( !PL_unify_string_nchars(new_ctx, sizeof(*cp), (char*)cp) ) return FALSE; sha1_end((unsigned char *)hval, c1p); } else { sha2_ctx *c1p = &(cp->context.sha2); sha2_hash((unsigned char*)data, (unsigned long)datalen, c1p); if ( !PL_unify_string_nchars(new_ctx, sizeof(*cp), (char*)cp) ) return FALSE; sha2_end((unsigned char *)hval, c1p); } /* . */ return PL_unify_list_ncodes(hash, cp->opts.digest_size, (char*)hval); }