aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Emby.Server.Implementations/LiveTv/Listings/SchedulesDirect.cs4
-rw-r--r--Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs2
-rw-r--r--Emby.Server.Implementations/Updates/InstallationManager.cs2
-rw-r--r--Jellyfin.Api/Controllers/LiveTvController.cs7
-rw-r--r--MediaBrowser.Common/Cryptography/PasswordHash.cs10
-rw-r--r--MediaBrowser.Common/Hex.cs94
-rw-r--r--benches/Jellyfin.Common.Benches/HexDecodeBenches.cs45
-rw-r--r--benches/Jellyfin.Common.Benches/HexEncodeBenches.cs32
-rw-r--r--benches/Jellyfin.Common.Benches/Jellyfin.Common.Benches.csproj16
-rw-r--r--benches/Jellyfin.Common.Benches/Program.cs14
-rw-r--r--tests/Jellyfin.Common.Tests/HexTests.cs19
-rw-r--r--tests/Jellyfin.Common.Tests/PasswordHashTests.cs5
12 files changed, 17 insertions, 233 deletions
diff --git a/Emby.Server.Implementations/LiveTv/Listings/SchedulesDirect.cs b/Emby.Server.Implementations/LiveTv/Listings/SchedulesDirect.cs
index 5d17ba1de..f181eb7a0 100644
--- a/Emby.Server.Implementations/LiveTv/Listings/SchedulesDirect.cs
+++ b/Emby.Server.Implementations/LiveTv/Listings/SchedulesDirect.cs
@@ -647,7 +647,9 @@ namespace Emby.Server.Implementations.LiveTv.Listings
{
using var options = new HttpRequestMessage(HttpMethod.Post, ApiUrl + "/token");
var hashedPasswordBytes = _cryptoProvider.ComputeHash("SHA1", Encoding.ASCII.GetBytes(password), Array.Empty<byte>());
- string hashedPassword = Hex.Encode(hashedPasswordBytes);
+ // TODO: remove ToLower when Convert.ToHexString supports lowercase
+ // Schedules Direct requires the hex to be lowercase
+ string hashedPassword = Convert.ToHexString(hashedPasswordBytes).ToLowerInvariant();
options.Content = new StringContent("{\"username\":\"" + username + "\",\"password\":\"" + hashedPassword + "\"}", Encoding.UTF8, MediaTypeNames.Application.Json);
using var response = await Send(options, false, null, cancellationToken).ConfigureAwait(false);
diff --git a/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs b/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs
index 140a67541..7bed06de3 100644
--- a/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs
+++ b/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs
@@ -243,7 +243,7 @@ namespace Emby.Server.Implementations.QuickConnect
Span<byte> bytes = stackalloc byte[length];
_rng.GetBytes(bytes);
- return Hex.Encode(bytes);
+ return Convert.ToHexString(bytes);
}
/// <inheritdoc/>
diff --git a/Emby.Server.Implementations/Updates/InstallationManager.cs b/Emby.Server.Implementations/Updates/InstallationManager.cs
index f2c096b8a..ef346dd5d 100644
--- a/Emby.Server.Implementations/Updates/InstallationManager.cs
+++ b/Emby.Server.Implementations/Updates/InstallationManager.cs
@@ -414,7 +414,7 @@ namespace Emby.Server.Implementations.Updates
using var md5 = MD5.Create();
cancellationToken.ThrowIfCancellationRequested();
- var hash = Hex.Encode(md5.ComputeHash(stream));
+ var hash = Convert.ToHexString(md5.ComputeHash(stream));
if (!string.Equals(package.Checksum, hash, StringComparison.OrdinalIgnoreCase))
{
_logger.LogError(
diff --git a/Jellyfin.Api/Controllers/LiveTvController.cs b/Jellyfin.Api/Controllers/LiveTvController.cs
index 410f3a340..56d4b3933 100644
--- a/Jellyfin.Api/Controllers/LiveTvController.cs
+++ b/Jellyfin.Api/Controllers/LiveTvController.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
@@ -17,7 +17,6 @@ using Jellyfin.Api.Helpers;
using Jellyfin.Api.ModelBinders;
using Jellyfin.Api.Models.LiveTvDtos;
using Jellyfin.Data.Enums;
-using MediaBrowser.Common;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Dto;
@@ -1015,7 +1014,9 @@ namespace Jellyfin.Api.Controllers
if (!string.IsNullOrEmpty(pw))
{
using var sha = SHA1.Create();
- listingsProviderInfo.Password = Hex.Encode(sha.ComputeHash(Encoding.UTF8.GetBytes(pw)));
+ // TODO: remove ToLower when Convert.ToHexString supports lowercase
+ // Schedules Direct requires the hex to be lowercase
+ listingsProviderInfo.Password = Convert.ToHexString(sha.ComputeHash(Encoding.UTF8.GetBytes(pw))).ToLowerInvariant();
}
return await _liveTvManager.SaveListingProvider(listingsProviderInfo, validateLogin, validateListings).ConfigureAwait(false);
diff --git a/MediaBrowser.Common/Cryptography/PasswordHash.cs b/MediaBrowser.Common/Cryptography/PasswordHash.cs
index 3e12536ec..3e2eae1c8 100644
--- a/MediaBrowser.Common/Cryptography/PasswordHash.cs
+++ b/MediaBrowser.Common/Cryptography/PasswordHash.cs
@@ -101,13 +101,13 @@ namespace MediaBrowser.Common.Cryptography
// Check if the string also contains a salt
if (splitted.Length - index == 2)
{
- salt = Hex.Decode(splitted[index++]);
- hash = Hex.Decode(splitted[index++]);
+ salt = Convert.FromHexString(splitted[index++]);
+ hash = Convert.FromHexString(splitted[index++]);
}
else
{
salt = Array.Empty<byte>();
- hash = Hex.Decode(splitted[index++]);
+ hash = Convert.FromHexString(splitted[index++]);
}
return new PasswordHash(id, hash, salt, parameters);
@@ -144,11 +144,11 @@ namespace MediaBrowser.Common.Cryptography
if (_salt.Length != 0)
{
str.Append('$')
- .Append(Hex.Encode(_salt, false));
+ .Append(Convert.ToHexString(_salt));
}
return str.Append('$')
- .Append(Hex.Encode(_hash, false)).ToString();
+ .Append(Convert.ToHexString(_hash)).ToString();
}
}
}
diff --git a/MediaBrowser.Common/Hex.cs b/MediaBrowser.Common/Hex.cs
deleted file mode 100644
index 559109f74..000000000
--- a/MediaBrowser.Common/Hex.cs
+++ /dev/null
@@ -1,94 +0,0 @@
-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>
- /// Gets a 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>
- 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 each element of the specified bytes as its hexadecimal string representation.
- /// </summary>
- /// <param name="bytes">An array of bytes.</param>
- /// <param name="lowercase"><c>true</c> to use lowercase hexadecimal characters; otherwise <c>false</c>.</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/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs b/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs
deleted file mode 100644
index d9a107b69..000000000
--- a/benches/Jellyfin.Common.Benches/HexDecodeBenches.cs
+++ /dev/null
@@ -1,45 +0,0 @@
-using System;
-using System.Globalization;
-using BenchmarkDotNet.Attributes;
-using BenchmarkDotNet.Running;
-using MediaBrowser.Common;
-
-namespace Jellyfin.Common.Benches
-{
- [MemoryDiagnoser]
- public class HexDecodeBenches
- {
- private string _data;
-
- [Params(0, 10, 100, 1000, 10000, 1000000)]
- public int N { get; set; }
-
- [GlobalSetup]
- public void GlobalSetup()
- {
- var bytes = new byte[N];
- new Random(42).NextBytes(bytes);
- _data = Hex.Encode(bytes);
- }
-
- [Benchmark]
- public byte[] Decode() => Hex.Decode(_data);
-
- [Benchmark]
- public byte[] DecodeSubString() => DecodeSubString(_data);
-
- private static byte[] DecodeSubString(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;
- }
- }
-}
diff --git a/benches/Jellyfin.Common.Benches/HexEncodeBenches.cs b/benches/Jellyfin.Common.Benches/HexEncodeBenches.cs
deleted file mode 100644
index 7abf93c51..000000000
--- a/benches/Jellyfin.Common.Benches/HexEncodeBenches.cs
+++ /dev/null
@@ -1,32 +0,0 @@
-using System;
-using BenchmarkDotNet.Attributes;
-using BenchmarkDotNet.Running;
-using MediaBrowser.Common;
-
-namespace Jellyfin.Common.Benches
-{
- [MemoryDiagnoser]
- public class HexEncodeBenches
- {
- private byte[] _data;
-
- [Params(0, 10, 100, 1000, 10000, 1000000)]
- public int N { get; set; }
-
- [GlobalSetup]
- public void GlobalSetup()
- {
- _data = new byte[N];
- new Random(42).NextBytes(_data);
- }
-
- [Benchmark]
- public string HexEncode() => Hex.Encode(_data);
-
- [Benchmark]
- public string BitConverterToString() => BitConverter.ToString(_data);
-
- [Benchmark]
- public string BitConverterToStringWithReplace() => BitConverter.ToString(_data).Replace("-", "");
- }
-}
diff --git a/benches/Jellyfin.Common.Benches/Jellyfin.Common.Benches.csproj b/benches/Jellyfin.Common.Benches/Jellyfin.Common.Benches.csproj
deleted file mode 100644
index c564e86e9..000000000
--- a/benches/Jellyfin.Common.Benches/Jellyfin.Common.Benches.csproj
+++ /dev/null
@@ -1,16 +0,0 @@
-<Project Sdk="Microsoft.NET.Sdk">
-
- <PropertyGroup>
- <OutputType>Exe</OutputType>
- <TargetFramework>net5.0</TargetFramework>
- </PropertyGroup>
-
- <ItemGroup>
- <PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
- </ItemGroup>
-
- <ItemGroup>
- <ProjectReference Include="../../MediaBrowser.Common/MediaBrowser.Common.csproj" />
- </ItemGroup>
-
-</Project>
diff --git a/benches/Jellyfin.Common.Benches/Program.cs b/benches/Jellyfin.Common.Benches/Program.cs
deleted file mode 100644
index b218b0dc1..000000000
--- a/benches/Jellyfin.Common.Benches/Program.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-using System;
-using BenchmarkDotNet.Running;
-
-namespace Jellyfin.Common.Benches
-{
- public static class Program
- {
- public static void Main(string[] args)
- {
- _ = BenchmarkRunner.Run<HexEncodeBenches>();
- _ = BenchmarkRunner.Run<HexDecodeBenches>();
- }
- }
-}
diff --git a/tests/Jellyfin.Common.Tests/HexTests.cs b/tests/Jellyfin.Common.Tests/HexTests.cs
deleted file mode 100644
index 5b578d38c..000000000
--- a/tests/Jellyfin.Common.Tests/HexTests.cs
+++ /dev/null
@@ -1,19 +0,0 @@
-using MediaBrowser.Common;
-using Xunit;
-
-namespace Jellyfin.Common.Tests
-{
- public class HexTests
- {
- [Theory]
- [InlineData("")]
- [InlineData("00")]
- [InlineData("01")]
- [InlineData("000102030405060708090a0b0c0d0e0f")]
- [InlineData("0123456789abcdef")]
- public void RoundTripTest(string data)
- {
- Assert.Equal(data, Hex.Encode(Hex.Decode(data)));
- }
- }
-}
diff --git a/tests/Jellyfin.Common.Tests/PasswordHashTests.cs b/tests/Jellyfin.Common.Tests/PasswordHashTests.cs
index 46926f4f8..c4422bd10 100644
--- a/tests/Jellyfin.Common.Tests/PasswordHashTests.cs
+++ b/tests/Jellyfin.Common.Tests/PasswordHashTests.cs
@@ -1,3 +1,4 @@
+using System;
using MediaBrowser.Common;
using MediaBrowser.Common.Cryptography;
using Xunit;
@@ -16,8 +17,8 @@ namespace Jellyfin.Common.Tests
{
var pass = PasswordHash.Parse(passwordHash);
Assert.Equal(id, pass.Id);
- Assert.Equal(salt, Hex.Encode(pass.Salt, false));
- Assert.Equal(hash, Hex.Encode(pass.Hash, false));
+ Assert.Equal(salt, Convert.ToHexString(pass.Salt));
+ Assert.Equal(hash, Convert.ToHexString(pass.Hash));
}
[Theory]