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/slskd/Common | |
| 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/slskd/Common')
| -rw-r--r-- | src/slskd/Common/Cryptography/Random.cs | 35 | ||||
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 23 |
2 files changed, 58 insertions, 0 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> |
