From 47cdbc6ad812cdbec12ae50a046ea04c39faf5ec Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 6 Oct 2022 19:05:26 -0500 Subject: add Base62Extensions --- src/slskd/Common/Cryptography/Base62Extensions.cs | 215 ++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 src/slskd/Common/Cryptography/Base62Extensions.cs (limited to 'src') diff --git a/src/slskd/Common/Cryptography/Base62Extensions.cs b/src/slskd/Common/Cryptography/Base62Extensions.cs new file mode 100644 index 00000000..b9ab3a72 --- /dev/null +++ b/src/slskd/Common/Cryptography/Base62Extensions.cs @@ -0,0 +1,215 @@ +/* + The MIT License (MIT) + + Copyright (c) 2016 - 2020 JoyMoe Interactive Entertainment Limited + + All rights reserved. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + https://github.com/JoyMoe/Base62.Net + */ +// -- suppress analysis + +namespace slskd.Cryptography +{ + using System; + using System.Collections.Generic; + using System.Globalization; + using System.Linq; + using System.Text; + + public static class Base62Extensions + { + private const string DefaultCharacterSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + private const string InvertedCharacterSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + /// + /// Encode a 2-byte number with Base62 + /// + /// String + /// Use inverted character set + /// Base62 string + public static string ToBase62(this short original, bool inverted = false) + { + var array = BitConverter.GetBytes(original); + + if (BitConverter.IsLittleEndian) + { + Array.Reverse(array); + } + + return array.ToBase62(inverted); + } + + /// + /// Encode a 4-byte number with Base62 + /// + /// String + /// Use inverted character set + /// Base62 string + public static string ToBase62(this int original, bool inverted = false) + { + var array = BitConverter.GetBytes(original); + + if (BitConverter.IsLittleEndian) + { + Array.Reverse(array); + } + + return array.ToBase62(inverted); + } + + /// + /// Encode a 8-byte number with Base62 + /// + /// String + /// Use inverted character set + /// Base62 string + public static string ToBase62(this long original, bool inverted = false) + { + var array = BitConverter.GetBytes(original); + + if (BitConverter.IsLittleEndian) + { + Array.Reverse(array); + } + + return array.ToBase62(inverted); + } + + /// + /// Encode a string with Base62 + /// + /// String + /// Use inverted character set + /// Base62 string + public static string ToBase62(this string original, bool inverted = false) + { + return Encoding.UTF8.GetBytes(original).ToBase62(inverted); + } + + /// + /// Encode a byte array with Base62 + /// + /// Byte array + /// Use inverted character set + /// Base62 string + public static string ToBase62(this byte[] original, bool inverted = false) + { + var characterSet = inverted ? InvertedCharacterSet : DefaultCharacterSet; + var arr = Array.ConvertAll(original, t => (int)t); + + var converted = BaseConvert(arr, 256, 62); + var builder = new StringBuilder(); + foreach (var t in converted) + { + builder.Append(characterSet[t]); + } + return builder.ToString(); + } + + /// + /// Decode a base62-encoded string + /// + /// Base62 string + /// Use inverted character set + /// Byte array + public static T FromBase62(this string base62, bool inverted = false) + { + var array = base62.FromBase62(inverted); + + switch (Type.GetTypeCode(typeof(T))) + { + case TypeCode.String: + return (T)Convert.ChangeType(Encoding.UTF8.GetString(array), typeof(T), CultureInfo.InvariantCulture); + case TypeCode.Int16: + if (BitConverter.IsLittleEndian) + { + Array.Reverse(array); + } + + return (T)Convert.ChangeType(BitConverter.ToInt16(array, 0), typeof(T), CultureInfo.InvariantCulture); + case TypeCode.Int32: + if (BitConverter.IsLittleEndian) + { + Array.Reverse(array); + } + + return (T)Convert.ChangeType(BitConverter.ToInt32(array, 0), typeof(T), CultureInfo.InvariantCulture); + case TypeCode.Int64: + if (BitConverter.IsLittleEndian) + { + Array.Reverse(array); + } + + return (T)Convert.ChangeType(BitConverter.ToInt64(array, 0), typeof(T), CultureInfo.InvariantCulture); + default: + throw new Exception($"Type of {typeof(T)} does not support."); + } + } + + /// + /// Decode a base62-encoded string + /// + /// Base62 string + /// Use inverted character set + /// Byte array + public static byte[] FromBase62(this string base62, bool inverted = false) + { + if (string.IsNullOrWhiteSpace(base62)) + { + throw new ArgumentNullException(nameof(base62)); + } + + var characterSet = inverted ? InvertedCharacterSet : DefaultCharacterSet; + var arr = Array.ConvertAll(base62.ToCharArray(), characterSet.IndexOf); + + var converted = BaseConvert(arr, 62, 256); + return Array.ConvertAll(converted, Convert.ToByte); + } + + private static int[] BaseConvert(int[] source, int sourceBase, int targetBase) + { + var result = new List(); + var leadingZeroCount = Math.Min(source.TakeWhile(x => x == 0).Count(), source.Length - 1); + int count; + while ((count = source.Length) > 0) + { + var quotient = new List(); + var remainder = 0; + for (var i = 0; i != count; i++) + { + var accumulator = source[i] + remainder * sourceBase; + var digit = accumulator / targetBase; + remainder = accumulator % targetBase; + if (quotient.Count > 0 || digit > 0) + { + quotient.Add(digit); + } + } + + result.Insert(0, remainder); + source = quotient.ToArray(); + } + result.InsertRange(0, Enumerable.Repeat(0, leadingZeroCount)); + return result.ToArray(); + } + } +} \ No newline at end of file -- cgit v1.2.3