diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-12-04 13:23:39 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-04 13:23:39 -0600 |
| commit | 39e26f9a940400102bb0b86e259d836362a95cea (patch) | |
| tree | 85eafd7d3af1594df9b0a9cd6514b933b98e2a40 /src | |
| parent | 9ba9581e3de9812db5f0b31e833db1bbc499c7bb (diff) | |
| parent | aaec9e67415e96bb030eb4d6bbb32790ee62d157 (diff) | |
| download | slskd-39e26f9a940400102bb0b86e259d836362a95cea.tar.gz | |
Merge pull request #295 from slskd/more-secure-strings
Use more secure strings for secrets
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Cryptography/Random.cs | 35 | ||||
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 23 | ||||
| -rw-r--r-- | src/slskd/Options.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 4 |
4 files changed, 61 insertions, 3 deletions
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 file="Random.cs" company="slskd Team"> +// 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/. +// </copyright> + +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; + } + } +} 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; @@ -285,6 +287,27 @@ namespace slskd } /// <summary> + /// Converts the byte array into a base 62 encoded string. + /// </summary> + /// <param name="bytes">The bytes to convert.</param> + /// <returns>The converted bytes as a base 62 string.</returns> + 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(); + } + + /// <summary> /// Creates a copy of this instance with the specified parameters changed. /// </summary> /// <param name="o">The options instance to copy.</param> 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(); /// <summary> /// Gets the TTL for JWTs, in milliseconds. 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} |
