diff --git a/common/ASC.Common/Utils/RandomString.cs b/common/ASC.Common/Utils/RandomString.cs index 2a8d3bd77f2..a7adf92428e 100644 --- a/common/ASC.Common/Utils/RandomString.cs +++ b/common/ASC.Common/Utils/RandomString.cs @@ -25,6 +25,7 @@ using System; +using System.Security.Cryptography; using System.Text; namespace ASC.Common.Utils @@ -35,10 +36,9 @@ public static string Generate(int length) { const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; var res = new StringBuilder(); - var rnd = new Random(); while (0 < length--) { - res.Append(valid[rnd.Next(valid.Length)]); + res.Append(valid[RandomNumberGenerator.GetInt32(valid.Length)]); } return res.ToString(); } diff --git a/common/ASC.Core.Common/Encryption/EncryptionSettings.cs b/common/ASC.Core.Common/Encryption/EncryptionSettings.cs index cdac5e98a1b..559a939b45d 100644 --- a/common/ASC.Core.Common/Encryption/EncryptionSettings.cs +++ b/common/ASC.Core.Common/Encryption/EncryptionSettings.cs @@ -172,16 +172,15 @@ public string GeneratePassword(int length, int numberOfNonAlphanumericCharacters if (num < numberOfNonAlphanumericCharacters) { - var random = new Random(); for (var j = 0; j < numberOfNonAlphanumericCharacters - num; j++) { int num3; do { - num3 = random.Next(0, length); + num3 = RandomNumberGenerator.GetInt32(0, length); } while (!char.IsLetterOrDigit(array2[num3])); - array2[num3] = punctuations[random.Next(0, punctuations.Length)]; + array2[num3] = punctuations[RandomNumberGenerator.GetInt32(0, punctuations.Length)]; } } diff --git a/common/ASC.Data.Storage/GoogleCloud/GoogleCloudStorage.cs b/common/ASC.Data.Storage/GoogleCloud/GoogleCloudStorage.cs index 74d64519b6a..e2ebfb5ff05 100644 --- a/common/ASC.Data.Storage/GoogleCloud/GoogleCloudStorage.cs +++ b/common/ASC.Data.Storage/GoogleCloud/GoogleCloudStorage.cs @@ -32,6 +32,7 @@ using System.IO; using System.Linq; using System.Net.Http; +using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -776,9 +777,7 @@ public override string UploadChunk(string domain, for (var i = 0; i < MAX_RETRIES; i++) { - var random = new Random(); - - millisecondsTimeout = Math.Min(Convert.ToInt32(Math.Pow(2, i)) + random.Next(0, 1000), 32 * 1000); + millisecondsTimeout = Math.Min(Convert.ToInt32(Math.Pow(2, i)) + RandomNumberGenerator.GetInt32(1000), 32 * 1000); try { diff --git a/products/ASC.Files/Core/Model/FileWrapper.cs b/products/ASC.Files/Core/Model/FileWrapper.cs index 39de1bef1b4..cc9160abed1 100644 --- a/products/ASC.Files/Core/Model/FileWrapper.cs +++ b/products/ASC.Files/Core/Model/FileWrapper.cs @@ -131,7 +131,7 @@ public static FileWrapper GetSample() //Updated = ApiDateTime.GetSample(), //Created = ApiDateTime.GetSample(), //CreatedBy = EmployeeWraper.GetSample(), - Id = new Random().Next(), + Id = 10, RootFolderType = FolderType.BUNCH, Shared = false, Title = "Some titile.txt", diff --git a/products/ASC.Files/Core/Model/FolderWrapper.cs b/products/ASC.Files/Core/Model/FolderWrapper.cs index 55519827df4..6d5738bde67 100644 --- a/products/ASC.Files/Core/Model/FolderWrapper.cs +++ b/products/ASC.Files/Core/Model/FolderWrapper.cs @@ -80,14 +80,14 @@ public static FolderWrapper GetSample() //Updated = ApiDateTime.GetSample(), //Created = ApiDateTime.GetSample(), //CreatedBy = EmployeeWraper.GetSample(), - Id = new Random().Next(), + Id = 10, RootFolderType = FolderType.BUNCH, Shared = false, Title = "Some titile", //UpdatedBy = EmployeeWraper.GetSample(), - FilesCount = new Random().Next(), - FoldersCount = new Random().Next(), - ParentId = new Random().Next(), + FilesCount = 5, + FoldersCount = 7, + ParentId = 10, IsShareable = null }; } diff --git a/web/ASC.Web.Core/Sms/SmsKeyStorage.cs b/web/ASC.Web.Core/Sms/SmsKeyStorage.cs index c5dc7d9cf10..0d892d4398c 100644 --- a/web/ASC.Web.Core/Sms/SmsKeyStorage.cs +++ b/web/ASC.Web.Core/Sms/SmsKeyStorage.cs @@ -27,6 +27,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Security.Cryptography; using ASC.Common; using ASC.Common.Caching; @@ -120,7 +121,7 @@ public bool GenerateKey(string phone, out string key) return false; } - key = new Random().Next((int)Math.Pow(10, KeyLength - 1), (int)Math.Pow(10, KeyLength)).ToString(CultureInfo.InvariantCulture); + key = RandomNumberGenerator.GetInt32((int)Math.Pow(10, KeyLength - 1), (int)Math.Pow(10, KeyLength)).ToString(CultureInfo.InvariantCulture); phoneKeys[key] = DateTime.UtcNow; KeyCache.Insert(cacheKey, phoneKeys, DateTime.UtcNow.Add(StoreInterval)); diff --git a/web/ASC.Web.Core/Users/UserManagerWrapper.cs b/web/ASC.Web.Core/Users/UserManagerWrapper.cs index 09133c0921f..0b058d1ffc7 100644 --- a/web/ASC.Web.Core/Users/UserManagerWrapper.cs +++ b/web/ASC.Web.Core/Users/UserManagerWrapper.cs @@ -27,6 +27,7 @@ using System; using System.Globalization; using System.Net.Mail; +using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; @@ -294,16 +295,14 @@ public static string GeneratePassword() return Guid.NewGuid().ToString(); } - private static readonly Random Rnd = new Random(); - internal static string GeneratePassword(int minLength, int maxLength, string noise) { - var length = Rnd.Next(minLength, maxLength + 1); + var length = RandomNumberGenerator.GetInt32(minLength, maxLength + 1); var pwd = string.Empty; while (length-- > 0) { - pwd += noise.Substring(Rnd.Next(noise.Length - 1), 1); + pwd += noise.Substring(RandomNumberGenerator.GetInt32(noise.Length - 1), 1); } return pwd; }