aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Helpers
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-04-17 22:55:32 +0800
committercrupest <crupest@outlook.com>2022-04-17 22:55:32 +0800
commit84cb99d087f62dca89fa682feae6738b3350fed4 (patch)
tree03ac3b8064f47a2229941119e0f864ee99e76e72 /BackEnd/Timeline/Helpers
parent7368d0388e78228499f28b33f79891c60639fb57 (diff)
downloadtimeline-84cb99d087f62dca89fa682feae6738b3350fed4.tar.gz
timeline-84cb99d087f62dca89fa682feae6738b3350fed4.tar.bz2
timeline-84cb99d087f62dca89fa682feae6738b3350fed4.zip
...
Diffstat (limited to 'BackEnd/Timeline/Helpers')
-rw-r--r--BackEnd/Timeline/Helpers/SecureRandomExtensions.cs29
1 files changed, 29 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Helpers/SecureRandomExtensions.cs b/BackEnd/Timeline/Helpers/SecureRandomExtensions.cs
new file mode 100644
index 00000000..b4f3f7f0
--- /dev/null
+++ b/BackEnd/Timeline/Helpers/SecureRandomExtensions.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Timeline.Helpers
+{
+ public static class SecureRandomExtensions
+ {
+ private static readonly char[] AlphaDigitString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
+
+ public static string GenerateAlphaDigitString(this RandomNumberGenerator randomNumberGenerator, int length)
+ {
+ if (length <= 0) throw new ArgumentOutOfRangeException(nameof(length));
+
+ var buffer = new byte[length];
+ randomNumberGenerator.GetBytes(buffer);
+
+ StringBuilder stringBuilder = new();
+
+ foreach (byte b in buffer)
+ {
+ stringBuilder.Append(AlphaDigitString[b % AlphaDigitString.Length]);
+ }
+
+ return stringBuilder.ToString();
+ }
+ }
+}
+