Remove standalone Sha256State functions.

GitOrigin-RevId: 5db80ea1902a6fe8a635081a8b050a19528f9f90
This commit is contained in:
levlam 2019-07-23 03:50:03 +03:00
parent ac6a83da50
commit 38e4310b71
6 changed files with 41 additions and 48 deletions

View File

@ -150,7 +150,7 @@ std::pair<uint32, UInt128> Transport::calc_message_key2(const AuthKey &auth_key,
uint8 msg_key_large_raw[32];
MutableSlice msg_key_large(msg_key_large_raw, sizeof(msg_key_large_raw));
state.extract(msg_key_large);
state.extract(msg_key_large, true);
// msg_key = substr (msg_key_large, 8, 16);
UInt128 res;

View File

@ -300,7 +300,7 @@ Result<ValueHash> Decryptor::finish() {
}
UInt256 res;
sha256_state_.extract(as_slice(res));
sha256_state_.extract(as_slice(res), true);
return ValueHash{res};
}

View File

@ -69,7 +69,7 @@ Status FileHashUploader::loop_impl() {
if (state_ == State::NetRequest) {
// messages.getDocumentByHash#338e2464 sha256:bytes size:int mime_type:string = Document;
auto hash = BufferSlice(32);
sha256_state_.extract(hash.as_slice());
sha256_state_.extract(hash.as_slice(), true);
auto mime_type = MimeType::from_extension(PathView(local_.path_).extension(), "image/gif");
auto query =
telegram_api::messages_getDocumentByHash(std::move(hash), static_cast<int32>(size_), std::move(mime_type));

View File

@ -123,7 +123,7 @@ class ConcurrentHashMap {
return ValueT{};
}
static ValueT migrate_value() {
return (ValueT)(1); // c-style convertion because reinterpret_cast<int>(1) is CE in MSVC
return (ValueT)(1); // c-style conversion because reinterpret_cast<int>(1) is CE in MSVC
}
ValueT insert(KeyT key, ValueT value) {

View File

@ -377,60 +377,61 @@ string sha512(Slice data) {
return result;
}
struct Sha256StateImpl {
SHA256_CTX ctx;
class Sha256State::Impl {
public:
SHA256_CTX ctx_;
};
Sha256State::Sha256State() = default;
Sha256State::Sha256State(Sha256State &&other) {
impl = std::move(other.impl);
is_inited = other.is_inited;
other.is_inited = false;
impl_ = std::move(other.impl_);
is_inited_ = other.is_inited_;
other.is_inited_ = false;
}
Sha256State &Sha256State::operator=(Sha256State &&other) {
Sha256State copy(std::move(other));
using std::swap;
swap(impl, copy.impl);
swap(is_inited, copy.is_inited);
swap(impl_, copy.impl_);
swap(is_inited_, copy.is_inited_);
return *this;
}
Sha256State::~Sha256State() {
if (is_inited) {
if (is_inited_) {
char result[32];
extract(MutableSlice{result, 32});
CHECK(!is_inited);
CHECK(!is_inited_);
}
}
void sha256_init(Sha256State *state) {
if (!state->impl) {
state->impl = make_unique<Sha256StateImpl>();
void Sha256State::init() {
if (!impl_) {
impl_ = make_unique<Sha256State::Impl>();
}
CHECK(!state->is_inited);
int err = SHA256_Init(&state->impl->ctx);
CHECK(!is_inited_);
int err = SHA256_Init(&impl_->ctx_);
LOG_IF(FATAL, err != 1);
state->is_inited = true;
is_inited_ = true;
}
void sha256_update(Slice data, Sha256State *state) {
CHECK(state->impl);
CHECK(state->is_inited);
int err = SHA256_Update(&state->impl->ctx, data.ubegin(), data.size());
void Sha256State::feed(Slice data) {
CHECK(impl_);
CHECK(is_inited_);
int err = SHA256_Update(&impl_->ctx_, data.ubegin(), data.size());
LOG_IF(FATAL, err != 1);
}
void sha256_final(Sha256State *state, MutableSlice output, bool destroy) {
void Sha256State::extract(MutableSlice output, bool destroy) {
CHECK(output.size() >= 32);
CHECK(state->impl);
CHECK(state->is_inited);
int err = SHA256_Final(output.ubegin(), &state->impl->ctx);
CHECK(impl_);
CHECK(is_inited_);
int err = SHA256_Final(output.ubegin(), &impl_->ctx_);
LOG_IF(FATAL, err != 1);
state->is_inited = false;
is_inited_ = false;
if (destroy) {
state->impl.reset();
impl_.reset();
}
}

View File

@ -69,14 +69,8 @@ string sha256(Slice data) TD_WARN_UNUSED_RESULT;
string sha512(Slice data) TD_WARN_UNUSED_RESULT;
struct Sha256StateImpl;
struct Sha256State;
void sha256_init(Sha256State *state);
void sha256_update(Slice data, Sha256State *state);
void sha256_final(Sha256State *state, MutableSlice output, bool destroy = true);
struct Sha256State {
class Sha256State {
public:
Sha256State();
Sha256State(const Sha256State &other) = delete;
Sha256State &operator=(const Sha256State &other) = delete;
@ -84,18 +78,16 @@ struct Sha256State {
Sha256State &operator=(Sha256State &&other);
~Sha256State();
void init() {
sha256_init(this);
}
void feed(Slice data) {
sha256_update(data, this);
}
void extract(MutableSlice dest) {
sha256_final(this, dest, false);
}
void init();
unique_ptr<Sha256StateImpl> impl;
bool is_inited = false;
void feed(Slice data);
void extract(MutableSlice dest, bool destroy = false);
private:
class Impl;
unique_ptr<Impl> impl_;
bool is_inited_ = false;
};
void md5(Slice input, MutableSlice output);