From e6e1b146185ee554c9646771f8f4a5b137c9ef58 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 4 Dec 2021 13:08:49 -0600 Subject: add Random --- src/slskd/Common/Cryptography/Random.cs | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/slskd/Common/Cryptography/Random.cs (limited to 'src') diff --git a/src/slskd/Common/Cryptography/Random.cs b/src/slskd/Common/Cryptography/Random.cs new file mode 100644 index 00000000..94d4fa24 --- /dev/null +++ b/src/slskd/Common/Cryptography/Random.cs @@ -0,0 +1,35 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Cryptography +{ + using System.Security.Cryptography; + + public static class Random + { + public static byte[] GetBytes(int size) + { + var key = new byte[size]; + using (var generator = RandomNumberGenerator.Create()) + { + generator.GetBytes(key); + } + + return key; + } + } +} -- cgit v1.2.3 From f134723e7e00b99df0f2d5a9656e86bd8ab82de4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 4 Dec 2021 13:09:00 -0600 Subject: add ToBase62String --- src/slskd/Common/Extensions.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 32a9b26e..d678541d 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -22,7 +22,9 @@ namespace slskd using System.IO; using System.Linq; using System.Net.Sockets; + using System.Numerics; using System.Reflection; + using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; @@ -284,6 +286,27 @@ namespace slskd return Path.Combine(path, sanitizedFilename).TrimStart('\\').TrimStart('/'); } + /// + /// Converts the byte array into a base 62 encoded string. + /// + /// The bytes to convert. + /// The converted bytes as a base 62 string. + public static string ToBase62String(this byte[] bytes) + { + const string alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + BigInteger dividend = new BigInteger(bytes); + var builder = new StringBuilder(); + + while (dividend != 0) + { + dividend = BigInteger.DivRem(dividend, alphabet.Length, out BigInteger remainder); + builder.Insert(0, alphabet[Math.Abs((int)remainder)]); + } + + return builder.ToString(); + } + /// /// Creates a copy of this instance with the specified parameters changed. /// -- cgit v1.2.3 From ac8ff7f5a43c8fc0e9b4eb3ebb8854dc053f351a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 4 Dec 2021 13:09:19 -0600 Subject: use random base62 string for jwt secret --- src/slskd/Options.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 15b954f7..151a9ddf 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -801,7 +801,7 @@ namespace slskd [Description("JWT signing key")] [StringLength(255, MinimumLength = 16)] [RequiresRestart] - public string Key { get; private set; } = Guid.NewGuid().ToString(); + public string Key { get; private set; } = Cryptography.Random.GetBytes(16).ToBase62String(); /// /// Gets the TTL for JWTs, in milliseconds. -- cgit v1.2.3 From aaec9e67415e96bb030eb4d6bbb32790ee62d157 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 4 Dec 2021 13:09:37 -0600 Subject: use random base62 string when creating certificate --- src/slskd/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 3c14006a..e4257037 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -198,7 +198,7 @@ namespace slskd if (GenerateCertificate) { - GenerateX509Certificate(password: Guid.NewGuid().ToString(), filename: $"{AppName}.pfx"); + GenerateX509Certificate(password: Cryptography.Random.GetBytes(16).ToBase62String(), filename: $"{AppName}.pfx"); return; } @@ -548,7 +548,7 @@ namespace slskd █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█", }; - var logo = logos[new Random().Next(0, logos.Length)]; + var logo = logos[new System.Random().Next(0, logos.Length)]; var banner = @$" {logo} -- cgit v1.2.3