std::string AlibabaCloud::PDS::ComputeContentETag()

in sdk/src/utils/Utils.cc [472:511]


std::string AlibabaCloud::PDS::ComputeContentETag(std::istream& stream)
{
    auto ctx = EVP_MD_CTX_create();

    unsigned char md_value[EVP_MAX_MD_SIZE];
    unsigned int md_len = 0;

    EVP_MD_CTX_init(ctx);
#ifndef OPENSSL_IS_BORINGSSL
    EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
#endif

    EVP_DigestInit_ex(ctx, EVP_md5(), nullptr);

    auto currentPos = stream.tellg();
    if (currentPos == static_cast<std::streampos>(-1)) {
        currentPos = 0;
        stream.clear();
    }
    stream.seekg(0, stream.beg);

    char streamBuffer[2048];
    while (stream.good())
    {
        stream.read(streamBuffer, 2048);
        auto bytesRead = stream.gcount();

        if (bytesRead > 0)
        {
            EVP_DigestUpdate(ctx, streamBuffer, static_cast<size_t>(bytesRead));
        }
    }

    EVP_DigestFinal_ex(ctx, md_value, &md_len);
    EVP_MD_CTX_destroy(ctx);
    stream.clear();
    stream.seekg(currentPos, stream.beg);

    return HexToString(md_value, md_len);
}