From d4bbbdb65cd8c9a0d0e96f0d2f53799f7ba340cb Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 24 Jan 2021 01:34:40 +0300 Subject: [PATCH] Replaced rand_value util function with openssl::RandomValue. --- Telegram/SourceFiles/api/api_sending.cpp | 5 ++- Telegram/SourceFiles/apiwrap.cpp | 14 +++--- .../SourceFiles/boxes/add_contact_box.cpp | 3 +- .../SourceFiles/boxes/create_poll_box.cpp | 3 +- .../boxes/peer_list_controllers.cpp | 3 +- Telegram/SourceFiles/calls/calls_call.cpp | 2 +- .../SourceFiles/calls/calls_group_call.cpp | 3 +- .../chat_helpers/field_autocomplete.cpp | 3 +- Telegram/SourceFiles/core/utils.h | 6 --- Telegram/SourceFiles/data/data_session.cpp | 7 +-- .../data/stickers/data_stickers.cpp | 3 +- .../SourceFiles/history/history_message.cpp | 3 +- .../history_view_voice_record_bar.cpp | 3 +- .../inline_bots/inline_bot_result.cpp | 3 +- .../media/view/media_view_overlay_widget.cpp | 3 +- .../SourceFiles/mtproto/config_loader.cpp | 4 +- .../SourceFiles/mtproto/connection_http.cpp | 3 +- .../SourceFiles/mtproto/connection_tcp.cpp | 6 +-- .../SourceFiles/mtproto/session_private.cpp | 2 +- .../passport/passport_encryption.cpp | 2 +- .../passport/passport_form_controller.cpp | 2 +- .../platform/mac/notifications_manager_mac.mm | 3 +- .../details/storage_file_utilities.cpp | 2 +- .../SourceFiles/storage/localimageloader.cpp | 10 ++--- .../SourceFiles/storage/localimageloader.h | 43 +++++++++++++++++-- .../window/notifications_utilities.cpp | 5 ++- .../window/themes/window_theme.cpp | 3 +- .../window/themes/window_theme_editor_box.cpp | 5 ++- .../themes/window_themes_generate_name.cpp | 6 ++- 29 files changed, 106 insertions(+), 54 deletions(-) diff --git a/Telegram/SourceFiles/api/api_sending.cpp b/Telegram/SourceFiles/api/api_sending.cpp index 967048a39c..fea236ed28 100644 --- a/Telegram/SourceFiles/api/api_sending.cpp +++ b/Telegram/SourceFiles/api/api_sending.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "api/api_sending.h" #include "api/api_text_entities.h" +#include "base/openssl_help.h" #include "base/unixtime.h" #include "data/data_document.h" #include "data/data_photo.h" @@ -76,7 +77,7 @@ void SendExistingMedia( const auto newId = FullMsgId( peerToChannel(peer->id), session->data().nextLocalMessageId()); - const auto randomId = rand_value(); + const auto randomId = openssl::RandomValue(); auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media; auto clientFlags = NewMessageClientFlags(); @@ -249,7 +250,7 @@ bool SendDice(Api::MessageToSend &message) { const auto newId = FullMsgId( peerToChannel(peer->id), session->data().nextLocalMessageId()); - const auto randomId = rand_value(); + const auto randomId = openssl::RandomValue(); auto &histories = history->owner().histories(); auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media; diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index af47774f12..820df09291 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -4025,7 +4025,7 @@ void ApiWrap::forwardMessages( ids.reserve(count); randomIds.reserve(count); for (const auto item : items) { - const auto randomId = rand_value(); + const auto randomId = openssl::RandomValue(); if (genClientSideMessage) { if (const auto message = item->toHistoryMessage()) { const auto newId = FullMsgId( @@ -4348,7 +4348,7 @@ void ApiWrap::sendMessage(MessageToSend &&message) { auto newId = FullMsgId( peerToChannel(peer->id), _session->data().nextLocalMessageId()); - auto randomId = rand_value(); + auto randomId = openssl::RandomValue(); TextUtilities::Trim(sending); @@ -4481,7 +4481,7 @@ void ApiWrap::sendBotStart(not_null bot, PeerData *chat) { sendMessage(std::move(message)); return; } - const auto randomId = rand_value(); + const auto randomId = openssl::RandomValue(); request(MTPmessages_StartBot( bot->inputUser, chat ? chat->input : MTP_inputPeerEmpty(), @@ -4507,7 +4507,7 @@ void ApiWrap::sendInlineResult( const auto newId = FullMsgId( peerToChannel(peer->id), _session->data().nextLocalMessageId()); - const auto randomId = rand_value(); + const auto randomId = openssl::RandomValue(); auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media; auto clientFlags = NewMessageClientFlags(); @@ -4659,7 +4659,7 @@ void ApiWrap::sendMedia( not_null item, const MTPInputMedia &media, Api::SendOptions options) { - const auto randomId = rand_value(); + const auto randomId = openssl::RandomValue(); _session->data().registerMessageRandomId(randomId, item->fullId()); sendMediaWithRandomId(item, media, options, randomId); @@ -4727,7 +4727,7 @@ void ApiWrap::sendAlbumWithUploaded( const MessageGroupId &groupId, const MTPInputMedia &media) { const auto localId = item->fullId(); - const auto randomId = rand_value(); + const auto randomId = openssl::RandomValue(); _session->data().registerMessageRandomId(randomId, localId); const auto albumIt = _sendingAlbums.find(groupId.raw()); @@ -5275,7 +5275,7 @@ void ApiWrap::createPoll( MTP_int(replyTo), PollDataToInputMedia(&data), MTP_string(), - MTP_long(rand_value()), + MTP_long(openssl::RandomValue()), MTPReplyMarkup(), MTPVector(), MTP_int(action.options.scheduled) diff --git a/Telegram/SourceFiles/boxes/add_contact_box.cpp b/Telegram/SourceFiles/boxes/add_contact_box.cpp index 599edfc10f..296fa10e60 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.cpp +++ b/Telegram/SourceFiles/boxes/add_contact_box.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "mtproto/sender.h" #include "base/flat_set.h" +#include "base/openssl_help.h" #include "boxes/confirm_box.h" #include "boxes/confirm_phone_box.h" // ExtractPhonePrefix. #include "boxes/photo_crop_box.h" @@ -382,7 +383,7 @@ void AddContactBox::save() { lastName = QString(); } _sentName = firstName; - _contactId = rand_value(); + _contactId = openssl::RandomValue(); _addRequest = _session->api().request(MTPcontacts_ImportContacts( MTP_vector( 1, diff --git a/Telegram/SourceFiles/boxes/create_poll_box.cpp b/Telegram/SourceFiles/boxes/create_poll_box.cpp index 07d0dd0c51..7bd2ba965c 100644 --- a/Telegram/SourceFiles/boxes/create_poll_box.cpp +++ b/Telegram/SourceFiles/boxes/create_poll_box.cpp @@ -30,6 +30,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/unique_qptr.h" #include "base/event_filter.h" #include "base/call_delayed.h" +#include "base/openssl_help.h" #include "window/window_session_controller.h" #include "styles/style_layers.h" #include "styles/style_boxes.h" @@ -885,7 +886,7 @@ not_null CreatePollBox::setupSolution( object_ptr CreatePollBox::setupContent() { using namespace Settings; - const auto id = rand_value(); + const auto id = openssl::RandomValue(); const auto error = lifetime().make_state(Error::Question); auto result = object_ptr(this); diff --git a/Telegram/SourceFiles/boxes/peer_list_controllers.cpp b/Telegram/SourceFiles/boxes/peer_list_controllers.cpp index 3d85441083..b745c48eb8 100644 --- a/Telegram/SourceFiles/boxes/peer_list_controllers.cpp +++ b/Telegram/SourceFiles/boxes/peer_list_controllers.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "boxes/peer_list_controllers.h" +#include "base/openssl_help.h" #include "boxes/confirm_box.h" #include "ui/widgets/checkbox.h" #include "ui/ui_utility.h" @@ -35,7 +36,7 @@ void ShareBotGame(not_null bot, not_null chat) { auto &histories = history->owner().histories(); const auto requestType = Data::Histories::RequestType::Send; histories.sendRequest(history, requestType, [=](Fn finish) { - const auto randomId = rand_value(); + const auto randomId = openssl::RandomValue(); const auto api = &chat->session().api(); history->sendRequestId = api->request(MTPmessages_SendMedia( MTP_flags(0), diff --git a/Telegram/SourceFiles/calls/calls_call.cpp b/Telegram/SourceFiles/calls/calls_call.cpp index 2adc749122..74bd55bdb1 100644 --- a/Telegram/SourceFiles/calls/calls_call.cpp +++ b/Telegram/SourceFiles/calls/calls_call.cpp @@ -232,7 +232,7 @@ void Call::startOutgoing() { _api.request(MTPphone_RequestCall( MTP_flags(flags), _user->inputUser, - MTP_int(rand_value()), + MTP_int(openssl::RandomValue()), MTP_bytes(_gaHash), MTP_phoneCallProtocol( MTP_flags(MTPDphoneCallProtocol::Flag::f_udp_p2p diff --git a/Telegram/SourceFiles/calls/calls_group_call.cpp b/Telegram/SourceFiles/calls/calls_group_call.cpp index d4b55dac33..6d21986616 100644 --- a/Telegram/SourceFiles/calls/calls_group_call.cpp +++ b/Telegram/SourceFiles/calls/calls_group_call.cpp @@ -24,6 +24,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_group_call.h" #include "data/data_session.h" #include "base/global_shortcuts.h" +#include "base/openssl_help.h" #include "webrtc/webrtc_media_devices.h" #include "webrtc/webrtc_create_adm.h" @@ -201,7 +202,7 @@ void GroupCall::playConnectingSoundOnce() { void GroupCall::start() { _createRequestId = _api.request(MTPphone_CreateGroupCall( _peer->input, - MTP_int(rand_value()) + MTP_int(openssl::RandomValue()) )).done([=](const MTPUpdates &result) { _acceptFields = true; _peer->session().api().applyUpdates(result); diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp index 36eddd08eb..87ee66884e 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp @@ -33,6 +33,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/ui_utility.h" #include "ui/cached_round_corners.h" #include "base/unixtime.h" +#include "base/openssl_help.h" #include "window/window_session_controller.h" #include "facades.h" #include "styles/style_chat.h" @@ -636,7 +637,7 @@ void FieldAutocomplete::showAnimated() { return; } if (_cache.isNull()) { - _stickersSeed = rand_value(); + _stickersSeed = openssl::RandomValue(); _scroll->show(); _cache = Ui::GrabWidget(this); } diff --git a/Telegram/SourceFiles/core/utils.h b/Telegram/SourceFiles/core/utils.h index 1d7f833ec9..f2d47c438f 100644 --- a/Telegram/SourceFiles/core/utils.h +++ b/Telegram/SourceFiles/core/utils.h @@ -117,12 +117,6 @@ inline std::array hashMd5Hex(const void *data, int size) { // good random (using openssl implementation) void memset_rand(void *data, uint32 len); -template -T rand_value() { - T result; - memset_rand(&result, sizeof(result)); - return result; -} QString translitRusEng(const QString &rus); QString rusKeyboardLayoutSwitch(const QString &from); diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 39b2353fde..beee6604c0 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -59,6 +59,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/platform/base_platform_info.h" #include "base/unixtime.h" #include "base/call_delayed.h" +#include "base/openssl_help.h" #include "facades.h" // Notify::switchInlineBotButtonReceived #include "app.h" #include "styles/style_boxes.h" // st::backgroundSize @@ -2427,7 +2428,7 @@ PhotoData *Session::photoFromWeb( return nullptr; } return photo( - rand_value(), + openssl::RandomValue(), uint64(0), QByteArray(), base::unixtime::now(), @@ -2692,7 +2693,7 @@ DocumentData *Session::documentFromWeb( const ImageLocation &thumbnailLocation, const ImageLocation &videoThumbnailLocation) { const auto result = document( - rand_value(), + openssl::RandomValue(), uint64(0), QByteArray(), base::unixtime::now(), @@ -2714,7 +2715,7 @@ DocumentData *Session::documentFromWeb( const ImageLocation &thumbnailLocation, const ImageLocation &videoThumbnailLocation) { const auto result = document( - rand_value(), + openssl::RandomValue(), uint64(0), QByteArray(), base::unixtime::now(), diff --git a/Telegram/SourceFiles/data/stickers/data_stickers.cpp b/Telegram/SourceFiles/data/stickers/data_stickers.cpp index 96f5566bbd..13cb46bc00 100644 --- a/Telegram/SourceFiles/data/stickers/data_stickers.cpp +++ b/Telegram/SourceFiles/data/stickers/data_stickers.cpp @@ -24,6 +24,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtproto_config.h" #include "ui/toast/toast.h" #include "ui/image/image_location_factory.h" +#include "base/openssl_help.h" #include "base/unixtime.h" #include "styles/style_chat_helpers.h" @@ -313,7 +314,7 @@ bool Stickers::applyArchivedResultFake() { const auto raw = set.get(); if ((raw->flags & MTPDstickerSet::Flag::f_installed_date) && !(raw->flags & MTPDstickerSet_ClientFlag::f_special)) { - if (rand_value() % 128 < 64) { + if (openssl::RandomValue() % 128 < 64) { const auto data = MTP_stickerSet( MTP_flags(raw->flags | MTPDstickerSet::Flag::f_archived), MTP_int(raw->installDate), diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 0185d871d5..8c1a11eaf5 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "history/history_message.h" +#include "base/openssl_help.h" #include "lang/lang_keys.h" #include "mainwidget.h" #include "mainwindow.h" @@ -291,7 +292,7 @@ void FastShareMessage(not_null item) { auto generateRandom = [&] { auto result = QVector(data->msgIds.size()); for (auto &value : result) { - value = rand_value(); + value = openssl::RandomValue(); } return result; }; diff --git a/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp b/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp index 13c8eeb1e5..4bc2f6a01a 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "api/api_send_progress.h" #include "base/event_filter.h" +#include "base/openssl_help.h" #include "base/unixtime.h" #include "boxes/confirm_box.h" #include "core/application.h" @@ -99,7 +100,7 @@ enum class FilterType { [[nodiscard]] not_null DummyDocument( not_null owner) { return owner->document( - rand_value(), + openssl::RandomValue(), uint64(0), QByteArray(), base::unixtime::now(), diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp index 73e8336110..e8e2e75e7d 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "inline_bots/inline_bot_result.h" #include "api/api_text_entities.h" +#include "base/openssl_help.h" #include "data/data_photo.h" #include "data/data_document.h" #include "data/data_session.h" @@ -417,7 +418,7 @@ void Result::createGame(not_null session) { return; } - const auto gameId = rand_value(); + const auto gameId = openssl::RandomValue(); _game = session->data().game( gameId, 0, diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 26dba31530..9e403dd5b6 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -51,6 +51,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_session_controller.h" #include "window/window_controller.h" #include "base/platform/base_platform_info.h" +#include "base/openssl_help.h" #include "base/unixtime.h" #include "main/main_account.h" #include "main/main_domain.h" // Domain::activeSessionValue. @@ -2688,7 +2689,7 @@ void OverlayWidget::initThemePreview() { const auto weakSession = base::make_weak(&_document->session()); const auto path = _document->location().name(); - const auto id = _themePreviewId = rand_value(); + const auto id = _themePreviewId = openssl::RandomValue(); const auto weak = Ui::MakeWeak(this); crl::async([=, data = std::move(current)]() mutable { auto preview = GeneratePreview( diff --git a/Telegram/SourceFiles/mtproto/config_loader.cpp b/Telegram/SourceFiles/mtproto/config_loader.cpp index ef2151f507..949c449471 100644 --- a/Telegram/SourceFiles/mtproto/config_loader.cpp +++ b/Telegram/SourceFiles/mtproto/config_loader.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "mtproto/config_loader.h" +#include "base/openssl_help.h" #include "mtproto/special_config_request.h" #include "mtproto/facade.h" #include "mtproto/mtproto_dc_options.h" @@ -174,7 +175,8 @@ void ConfigLoader::sendSpecialRequest() { } const auto weak = base::make_weak(this); - const auto index = rand_value() % _specialEndpoints.size(); + const auto index = openssl::RandomValue() + % _specialEndpoints.size(); const auto endpoint = _specialEndpoints.begin() + index; _specialEnumCurrent = specialToRealDcId(endpoint->dcId); diff --git a/Telegram/SourceFiles/mtproto/connection_http.cpp b/Telegram/SourceFiles/mtproto/connection_http.cpp index 3f9d005b7c..c4a6bdc1cc 100644 --- a/Telegram/SourceFiles/mtproto/connection_http.cpp +++ b/Telegram/SourceFiles/mtproto/connection_http.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "mtproto/connection_http.h" +#include "base/openssl_help.h" #include "base/qthelp_url.h" namespace MTP { @@ -20,7 +21,7 @@ constexpr auto kFullConnectionTimeout = crl::time(8000); HttpConnection::HttpConnection(QThread *thread, const ProxyData &proxy) : AbstractConnection(thread, proxy) -, _checkNonce(rand_value()) { +, _checkNonce(openssl::RandomValue()) { _manager.moveToThread(thread); _manager.setProxy(ToNetworkProxy(proxy)); } diff --git a/Telegram/SourceFiles/mtproto/connection_tcp.cpp b/Telegram/SourceFiles/mtproto/connection_tcp.cpp index f195c7c44c..90958753df 100644 --- a/Telegram/SourceFiles/mtproto/connection_tcp.cpp +++ b/Telegram/SourceFiles/mtproto/connection_tcp.cpp @@ -192,11 +192,11 @@ bytes::span TcpConnection::Protocol::VersionD::finalizePacket( Expects(buffer.size() > 2 && buffer.size() < 0x1000003U); const auto intsSize = uint32(buffer.size() - 2); - const auto padding = rand_value() & 0x0F; + const auto padding = openssl::RandomValue() & 0x0F; const auto bytesSize = intsSize * sizeof(mtpPrime) + padding; buffer[1] = bytesSize; for (auto added = 0; added < padding; added += 4) { - buffer.push_back(rand_value()); + buffer.push_back(openssl::RandomValue()); } return bytes::make_span(buffer).subspan(4, 4 + bytesSize); @@ -244,7 +244,7 @@ TcpConnection::TcpConnection( const ProxyData &proxy) : AbstractConnection(thread, proxy) , _instance(instance) -, _checkNonce(rand_value()) { +, _checkNonce(openssl::RandomValue()) { } ConnectionPointer TcpConnection::clone(const ProxyData &proxy) { diff --git a/Telegram/SourceFiles/mtproto/session_private.cpp b/Telegram/SourceFiles/mtproto/session_private.cpp index 9ddab55b85..ea5b3b30fb 100644 --- a/Telegram/SourceFiles/mtproto/session_private.cpp +++ b/Telegram/SourceFiles/mtproto/session_private.cpp @@ -2518,7 +2518,7 @@ void SessionPrivate::authKeyChecked() { resendAll(); } // else receive salt in bad_server_salt first, then try to send all the requests - _pingIdToSend = rand_value(); // get server_salt + _pingIdToSend = openssl::RandomValue(); // get server_salt _sessionData->queueNeedToResumeAndSend(); } diff --git a/Telegram/SourceFiles/passport/passport_encryption.cpp b/Telegram/SourceFiles/passport/passport_encryption.cpp index 4d9b3520ea..3e3d4684f8 100644 --- a/Telegram/SourceFiles/passport/passport_encryption.cpp +++ b/Telegram/SourceFiles/passport/passport_encryption.cpp @@ -334,7 +334,7 @@ EncryptedData EncryptData( constexpr auto kFromPadding = kMinPadding + kAlignTo - 1; constexpr auto kPaddingDelta = kMaxPadding - kFromPadding; const auto randomPadding = kFromPadding - + (rand_value() % kPaddingDelta); + + (openssl::RandomValue() % kPaddingDelta); const auto padding = randomPadding - ((bytes.size() + randomPadding) % kAlignTo); Assert(padding >= kMinPadding && padding <= kMaxPadding); diff --git a/Telegram/SourceFiles/passport/passport_form_controller.cpp b/Telegram/SourceFiles/passport/passport_form_controller.cpp index c0a928f402..a1288a8b8b 100644 --- a/Telegram/SourceFiles/passport/passport_form_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_form_controller.cpp @@ -1421,7 +1421,7 @@ void FormController::restoreScan( void FormController::prepareFile( EditFile &file, const QByteArray &content) { - const auto fileId = rand_value(); + const auto fileId = openssl::RandomValue(); file.fields.size = content.size(); file.fields.id = fileId; file.fields.dcId = _controller->session().mainDcId(); diff --git a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm index dec09219e3..522f585bbc 100644 --- a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm +++ b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/platform/base_platform_info.h" #include "platform/platform_specific.h" #include "base/platform/mac/base_utilities_mac.h" +#include "base/openssl_help.h" #include "history/history.h" #include "ui/empty_userpic.h" #include "main/main_session.h" @@ -229,7 +230,7 @@ private: }; Manager::Private::Private(Manager *manager) -: _managerId(rand_value()) +: _managerId(openssl::RandomValue()) , _managerIdString(QString::number(_managerId)) , _delegate([[NotificationDelegate alloc] initWithManager:manager managerId:_managerId]) { updateDelegate(); diff --git a/Telegram/SourceFiles/storage/details/storage_file_utilities.cpp b/Telegram/SourceFiles/storage/details/storage_file_utilities.cpp index 55dacabdd2..9591255cbd 100644 --- a/Telegram/SourceFiles/storage/details/storage_file_utilities.cpp +++ b/Telegram/SourceFiles/storage/details/storage_file_utilities.cpp @@ -58,7 +58,7 @@ FileKey GenerateKey(const QString &basePath) { path.reserve(basePath.size() + 0x11); path += basePath; do { - result = rand_value(); + result = openssl::RandomValue(); path.resize(basePath.size()); path += ToFilePart(result); } while (!result || KeyAlreadyUsed(path)); diff --git a/Telegram/SourceFiles/storage/localimageloader.cpp b/Telegram/SourceFiles/storage/localimageloader.cpp index d9c53522fb..bad70f0a6f 100644 --- a/Telegram/SourceFiles/storage/localimageloader.cpp +++ b/Telegram/SourceFiles/storage/localimageloader.cpp @@ -58,7 +58,7 @@ PreparedFileThumbnail PrepareFileThumbnail(QImage &&original) { return {}; } auto result = PreparedFileThumbnail(); - result.id = rand_value(); + result.id = openssl::RandomValue(); const auto scaled = (width > kThumbnailSize || height > kThumbnailSize); const auto scaledWidth = [&] { return (width > height) @@ -222,7 +222,7 @@ SendMediaReady PreparePeerPhoto(MTP::DcId dcId, PeerId peerId, QImage &&image) { push("b", scaled(320)); push("c", std::move(image), jpeg); - const auto id = rand_value(); + const auto id = openssl::RandomValue(); const auto photo = MTP_photo( MTP_flags(0), MTP_long(id), @@ -402,7 +402,7 @@ void TaskQueueWorker::onTaskAdded() { _inTaskAdded = false; } -SendingAlbum::SendingAlbum() : groupId(rand_value()) { +SendingAlbum::SendingAlbum() : groupId(openssl::RandomValue()) { } void SendingAlbum::fillMedia( @@ -496,7 +496,7 @@ FileLoadTask::FileLoadTask( const TextWithTags &caption, std::shared_ptr album, MsgId msgIdToEdit) -: _id(rand_value()) +: _id(openssl::RandomValue()) , _session(session) , _dcId(session->mainDcId()) , _to(to) @@ -518,7 +518,7 @@ FileLoadTask::FileLoadTask( const VoiceWaveform &waveform, const FileLoadTo &to, const TextWithTags &caption) -: _id(rand_value()) +: _id(openssl::RandomValue()) , _session(session) , _dcId(session->mainDcId()) , _to(to) diff --git a/Telegram/SourceFiles/storage/localimageloader.h b/Telegram/SourceFiles/storage/localimageloader.h index 02a403d9b5..25f7729981 100644 --- a/Telegram/SourceFiles/storage/localimageloader.h +++ b/Telegram/SourceFiles/storage/localimageloader.h @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "base/openssl_help.h" #include "base/variant.h" #include "api/api_common.h" #include "ui/chat/attach/attach_prepare.h" @@ -22,13 +23,47 @@ enum class SendMediaType { }; struct SendMediaPrepare { - SendMediaPrepare(const QString &file, const PeerId &peer, SendMediaType type, MsgId replyTo) : id(rand_value()), file(file), peer(peer), type(type), replyTo(replyTo) { + SendMediaPrepare( + const QString &file, + const PeerId &peer, + SendMediaType type, + MsgId replyTo) : id(openssl::RandomValue()), + file(file), + peer(peer), + type(type), + replyTo(replyTo) { } - SendMediaPrepare(const QImage &img, const PeerId &peer, SendMediaType type, MsgId replyTo) : id(rand_value()), img(img), peer(peer), type(type), replyTo(replyTo) { + SendMediaPrepare( + const QImage &img, + const PeerId &peer, + SendMediaType type, + MsgId replyTo) : id(openssl::RandomValue()), + img(img), + peer(peer), + type(type), + replyTo(replyTo) { } - SendMediaPrepare(const QByteArray &data, const PeerId &peer, SendMediaType type, MsgId replyTo) : id(rand_value()), data(data), peer(peer), type(type), replyTo(replyTo) { + SendMediaPrepare( + const QByteArray &data, + const PeerId &peer, + SendMediaType type, + MsgId replyTo) : id(openssl::RandomValue()), + data(data), + peer(peer), + type(type), + replyTo(replyTo) { } - SendMediaPrepare(const QByteArray &data, int duration, const PeerId &peer, SendMediaType type, MsgId replyTo) : id(rand_value()), data(data), peer(peer), type(type), duration(duration), replyTo(replyTo) { + SendMediaPrepare( + const QByteArray &data, + int duration, + const PeerId &peer, + SendMediaType type, + MsgId replyTo) : id(openssl::RandomValue()), + data(data), + peer(peer), + type(type), + duration(duration), + replyTo(replyTo) { } PhotoId id; QString file; diff --git a/Telegram/SourceFiles/window/notifications_utilities.cpp b/Telegram/SourceFiles/window/notifications_utilities.cpp index 34b0c9cb0f..724584ceef 100644 --- a/Telegram/SourceFiles/window/notifications_utilities.cpp +++ b/Telegram/SourceFiles/window/notifications_utilities.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/notifications_utilities.h" #include "base/platform/base_platform_file_utilities.h" +#include "base/openssl_help.h" #include "core/application.h" #include "data/data_peer.h" #include "ui/empty_userpic.h" @@ -59,7 +60,9 @@ QString CachedUserpics::get( } else { v.until = 0; } - v.path = cWorkingDir() + qsl("tdata/temp/") + QString::number(rand_value(), 16) + qsl(".png"); + v.path = u"%1tdata/temp/%2.png"_q + .arg(cWorkingDir()) + .arg(QString::number(openssl::RandomValue(), 16)); if (key.first || key.second) { if (peer->isSelf()) { const auto method = (_type == Type::Rounded) diff --git a/Telegram/SourceFiles/window/themes/window_theme.cpp b/Telegram/SourceFiles/window/themes/window_theme.cpp index 6e7543734e..40d8526dd9 100644 --- a/Telegram/SourceFiles/window/themes/window_theme.cpp +++ b/Telegram/SourceFiles/window/themes/window_theme.cpp @@ -18,6 +18,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "storage/localstorage.h" #include "storage/localimageloader.h" #include "storage/file_upload.h" +#include "base/openssl_help.h" #include "base/parse_helper.h" #include "base/zlib_help.h" #include "base/unixtime.h" @@ -476,7 +477,7 @@ SendMediaReady PrepareWallPaper(MTP::DcId dcId, const QImage &image) { attributes.push_back(MTP_documentAttributeImageSize( MTP_int(image.width()), MTP_int(image.height()))); - const auto id = rand_value(); + const auto id = openssl::RandomValue(); const auto document = MTP_document( MTP_flags(0), MTP_long(id), diff --git a/Telegram/SourceFiles/window/themes/window_theme_editor_box.cpp b/Telegram/SourceFiles/window/themes/window_theme_editor_box.cpp index 38c57002d3..f343907d80 100644 --- a/Telegram/SourceFiles/window/themes/window_theme_editor_box.cpp +++ b/Telegram/SourceFiles/window/themes/window_theme_editor_box.cpp @@ -32,6 +32,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/base_file_utilities.h" #include "base/zlib_help.h" #include "base/unixtime.h" +#include "base/openssl_help.h" #include "data/data_session.h" #include "data/data_document.h" #include "data/data_cloud_themes.h" @@ -341,7 +342,7 @@ bool CopyColorsToPalette( auto result = QString(); result.reserve(kRandomSlugSize); for (auto i = 0; i != kRandomSlugSize; ++i) { - const auto value = rand_value() % values; + const auto value = openssl::RandomValue() % values; if (value < letters) { result.append(char('A' + value)); } else if (value < 2 * letters) { @@ -447,7 +448,7 @@ SendMediaReady PrepareThemeMedia( auto attributes = QVector( 1, MTP_documentAttributeFilename(MTP_string(filename))); - const auto id = rand_value(); + const auto id = openssl::RandomValue(); const auto document = MTP_document( MTP_flags(0), MTP_long(id), diff --git a/Telegram/SourceFiles/window/themes/window_themes_generate_name.cpp b/Telegram/SourceFiles/window/themes/window_themes_generate_name.cpp index 65d407aee4..9e56ae5924 100644 --- a/Telegram/SourceFiles/window/themes/window_themes_generate_name.cpp +++ b/Telegram/SourceFiles/window/themes/window_themes_generate_name.cpp @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "window/themes/window_themes_generate_name.h" +#include "base/openssl_help.h" + namespace Window { namespace Theme { namespace { @@ -337,13 +339,13 @@ QString GenerateName(const QColor &accent) { return result; }; const auto random = [&](const std::vector &values) { - const auto index = rand_value() % values.size(); + const auto index = openssl::RandomValue() % values.size(); return capitalized(values[index]); }; const auto min = ranges::min_element(kColors, pred); Assert(min != end(kColors)); const auto color = capitalized(min->second); - return (rand_value() % 2 == 0) + return (openssl::RandomValue() % 2 == 0) ? random(kAdjectives) + ' ' + color : color + ' ' + random(kSubjectives); }