aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common
diff options
context:
space:
mode:
authorAndrew Mahone <andrew.mahone@gmail.com>2019-11-04 10:03:18 -0500
committerAndrew Mahone <andrew.mahone@gmail.com>2019-11-04 10:03:18 -0500
commit1600d5b53f89ad4f1af39a6a53d0ef251341b278 (patch)
tree12f5483eba2396ad9b8da2e46f6a0ced073cf5e7 /MediaBrowser.Common
parent90dfe729bb9f8929bf584b93f93aabd84abbef3d (diff)
parent9756bdb76ea625c19b6743a34ec95759d3f6d050 (diff)
Merge remote-tracking branch 'origin/master' into media-attachments-clean
Diffstat (limited to 'MediaBrowser.Common')
-rw-r--r--MediaBrowser.Common/Cryptography/PasswordHash.cs31
-rw-r--r--MediaBrowser.Common/Extensions/CollectionExtensions.cs48
-rw-r--r--MediaBrowser.Common/Extensions/CopyToExtensions.cs26
-rw-r--r--MediaBrowser.Common/Hex.cs94
-rw-r--r--MediaBrowser.Common/HexHelper.cs24
-rw-r--r--MediaBrowser.Common/MediaBrowser.Common.csproj4
6 files changed, 136 insertions, 91 deletions
diff --git a/MediaBrowser.Common/Cryptography/PasswordHash.cs b/MediaBrowser.Common/Cryptography/PasswordHash.cs
index 7741571db9..4c68040978 100644
--- a/MediaBrowser.Common/Cryptography/PasswordHash.cs
+++ b/MediaBrowser.Common/Cryptography/PasswordHash.cs
@@ -4,7 +4,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
-using static MediaBrowser.Common.HexHelper;
namespace MediaBrowser.Common.Cryptography
{
@@ -102,13 +101,13 @@ namespace MediaBrowser.Common.Cryptography
// Check if the string also contains a salt
if (splitted.Length - index == 2)
{
- salt = FromHexString(splitted[index++]);
- hash = FromHexString(splitted[index++]);
+ salt = Hex.Decode(splitted[index++]);
+ hash = Hex.Decode(splitted[index++]);
}
else
{
salt = Array.Empty<byte>();
- hash = FromHexString(splitted[index++]);
+ hash = Hex.Decode(splitted[index++]);
}
return new PasswordHash(id, hash, salt, parameters);
@@ -124,10 +123,10 @@ namespace MediaBrowser.Common.Cryptography
stringBuilder.Append('$');
foreach (var pair in _parameters)
{
- stringBuilder.Append(pair.Key);
- stringBuilder.Append('=');
- stringBuilder.Append(pair.Value);
- stringBuilder.Append(',');
+ stringBuilder.Append(pair.Key)
+ .Append('=')
+ .Append(pair.Value)
+ .Append(',');
}
// Remove last ','
@@ -137,21 +136,19 @@ namespace MediaBrowser.Common.Cryptography
/// <inheritdoc />
public override string ToString()
{
- var str = new StringBuilder();
- str.Append('$');
- str.Append(Id);
+ var str = new StringBuilder()
+ .Append('$')
+ .Append(Id);
SerializeParameters(str);
if (Salt.Length != 0)
{
- str.Append('$');
- str.Append(ToHexString(Salt));
+ str.Append('$')
+ .Append(Hex.Encode(Salt, false));
}
- str.Append('$');
- str.Append(ToHexString(Hash));
-
- return str.ToString();
+ return str.Append('$')
+ .Append(Hex.Encode(Hash, false)).ToString();
}
}
}
diff --git a/MediaBrowser.Common/Extensions/CollectionExtensions.cs b/MediaBrowser.Common/Extensions/CollectionExtensions.cs
deleted file mode 100644
index 2152243985..0000000000
--- a/MediaBrowser.Common/Extensions/CollectionExtensions.cs
+++ /dev/null
@@ -1,48 +0,0 @@
-#pragma warning disable CS1591
-
-using System.Collections.Generic;
-
-namespace MediaBrowser.Common.Extensions
-{
- // The MS CollectionExtensions are only available in netcoreapp
- public static class CollectionExtensions
- {
- public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
- {
- dictionary.TryGetValue(key, out var ret);
- return ret;
- }
-
- /// <summary>
- /// Copies all the elements of the current collection to the specified list
- /// starting at the specified destination array index. The index is specified as a 32-bit integer.
- /// </summary>
- /// <param name="source">The current collection that is the source of the elements.</param>
- /// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
- /// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
- /// <typeparam name="T"></typeparam>
- public static void CopyTo<T>(this IReadOnlyList<T> source, IList<T> destination, int index = 0)
- {
- for (int i = 0; i < source.Count; i++)
- {
- destination[index + i] = source[i];
- }
- }
-
- /// <summary>
- /// Copies all the elements of the current collection to the specified list
- /// starting at the specified destination array index. The index is specified as a 32-bit integer.
- /// </summary>
- /// <param name="source">The current collection that is the source of the elements.</param>
- /// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
- /// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
- /// <typeparam name="T"></typeparam>
- public static void CopyTo<T>(this IReadOnlyCollection<T> source, IList<T> destination, int index = 0)
- {
- foreach (T item in source)
- {
- destination[index++] = item;
- }
- }
- }
-}
diff --git a/MediaBrowser.Common/Extensions/CopyToExtensions.cs b/MediaBrowser.Common/Extensions/CopyToExtensions.cs
new file mode 100644
index 0000000000..78a73f07e0
--- /dev/null
+++ b/MediaBrowser.Common/Extensions/CopyToExtensions.cs
@@ -0,0 +1,26 @@
+using System.Collections.Generic;
+
+namespace MediaBrowser.Common.Extensions
+{
+ /// <summary>
+ /// Provides <c>CopyTo</c> extensions methods for <see cref="IReadOnlyList{T}" />.
+ /// </summary>
+ public static class CollectionExtensions
+ {
+ /// <summary>
+ /// Copies all the elements of the current collection to the specified list
+ /// starting at the specified destination array index. The index is specified as a 32-bit integer.
+ /// </summary>
+ /// <param name="source">The current collection that is the source of the elements.</param>
+ /// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
+ /// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
+ /// <typeparam name="T"></typeparam>
+ public static void CopyTo<T>(this IReadOnlyList<T> source, IList<T> destination, int index = 0)
+ {
+ for (int i = 0; i < source.Count; i++)
+ {
+ destination[index + i] = source[i];
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Hex.cs b/MediaBrowser.Common/Hex.cs
new file mode 100644
index 0000000000..b2d9aea3a7
--- /dev/null
+++ b/MediaBrowser.Common/Hex.cs
@@ -0,0 +1,94 @@
+using System;
+using System.Diagnostics.CodeAnalysis;
+
+namespace MediaBrowser.Common
+{
+ /// <summary>
+ /// Encoding and decoding hex strings.
+ /// </summary>
+ public static class Hex
+ {
+ internal const string HexCharsLower = "0123456789abcdef";
+ internal const string HexCharsUpper = "0123456789ABCDEF";
+
+ internal const int LastHexSymbol = 0x66; // 102: f
+
+ /// <summary>
+ /// Map from an ASCII char to its hex value shifted,
+ /// e.g. <c>b</c> -> 11. 0xFF means it's not a hex symbol.
+ /// </summary>
+ /// <value></value>
+ internal static ReadOnlySpan<byte> HexLookup => new byte[] {
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
+ };
+
+ /// <summary>
+ /// Encodes <c>bytes</c> as a hex string.
+ /// </summary>
+ /// <param name="bytes"></param>
+ /// <param name="lowercase"></param>
+ /// <returns><c>bytes</c> as a hex string.</returns>
+ public static string Encode(ReadOnlySpan<byte> bytes, bool lowercase = true)
+ {
+ var hexChars = lowercase ? HexCharsLower : HexCharsUpper;
+
+ // TODO: use string.Create when it's supports spans
+ // Ref: https://github.com/dotnet/corefx/issues/29120
+ char[] s = new char[bytes.Length * 2];
+ int j = 0;
+ for (int i = 0; i < bytes.Length; i++)
+ {
+ s[j++] = hexChars[bytes[i] >> 4];
+ s[j++] = hexChars[bytes[i] & 0x0f];
+ }
+
+ return new string(s);
+ }
+
+ /// <summary>
+ /// Decodes a hex string into bytes.
+ /// </summary>
+ /// <param name="str">The <see cref="string" />.</param>
+ /// <returns>The decoded bytes.</returns>
+ public static byte[] Decode(ReadOnlySpan<char> str)
+ {
+ if (str.Length == 0)
+ {
+ return Array.Empty<byte>();
+ }
+
+ var unHex = HexLookup;
+
+ int byteLen = str.Length / 2;
+ byte[] bytes = new byte[byteLen];
+ int i = 0;
+ for (int j = 0; j < byteLen; j++)
+ {
+ byte a;
+ byte b;
+ if (str[i] > LastHexSymbol
+ || (a = unHex[str[i++]]) == 0xFF
+ || str[i] > LastHexSymbol
+ || (b = unHex[str[i++]]) == 0xFF)
+ {
+ ThrowArgumentException(nameof(str));
+ break; // Unreachable
+ }
+
+ bytes[j] = (byte)((a * 16) | b);
+ }
+
+ return bytes;
+ }
+
+ [DoesNotReturn]
+ private static void ThrowArgumentException(string paramName)
+ => throw new ArgumentException("Character is not a hex symbol.", paramName);
+ }
+}
diff --git a/MediaBrowser.Common/HexHelper.cs b/MediaBrowser.Common/HexHelper.cs
deleted file mode 100644
index 61007b5b2e..0000000000
--- a/MediaBrowser.Common/HexHelper.cs
+++ /dev/null
@@ -1,24 +0,0 @@
-#pragma warning disable CS1591
-
-using System;
-using System.Globalization;
-
-namespace MediaBrowser.Common
-{
- public static class HexHelper
- {
- public static byte[] FromHexString(string str)
- {
- byte[] bytes = new byte[str.Length / 2];
- for (int i = 0; i < str.Length; i += 2)
- {
- bytes[i / 2] = byte.Parse(str.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
- }
-
- return bytes;
- }
-
- public static string ToHexString(byte[] bytes)
- => BitConverter.ToString(bytes).Replace("-", "");
- }
-}
diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj
index cf3f6c2a44..889fbfa5ab 100644
--- a/MediaBrowser.Common/MediaBrowser.Common.csproj
+++ b/MediaBrowser.Common/MediaBrowser.Common.csproj
@@ -12,7 +12,7 @@
</ItemGroup>
<ItemGroup>
- <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
+ <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.0" />
</ItemGroup>
@@ -21,7 +21,7 @@
</ItemGroup>
<PropertyGroup>
- <TargetFramework>netstandard2.0</TargetFramework>
+ <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>