Rewrite hex encoder/decoder

This commit is contained in:
Bond_009 2019-10-19 00:22:08 +02:00
parent 89a21c96c0
commit a245f5a0d4
32 changed files with 243 additions and 114 deletions

3
.gitignore vendored
View File

@ -268,3 +268,6 @@ doc/
# Deployment artifacts # Deployment artifacts
dist dist
*.exe *.exe
# BenchmarkDotNet artifacts
BenchmarkDotNet.Artifacts

View File

@ -12,7 +12,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@ -17,9 +17,4 @@
<Compile Include="..\SharedVersion.cs" /> <Compile Include="..\SharedVersion.cs" />
</ItemGroup> </ItemGroup>
<PropertyGroup>
<!-- We need at least C# 7.1 for the "default literal" feature-->
<LangVersion>latest</LangVersion>
</PropertyGroup>
</Project> </Project>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -14,7 +14,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <TreatWarningsAsErrors>true</TreatWarningsAsErrors>

View File

@ -2,11 +2,11 @@ using System;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common;
using MediaBrowser.Common.Cryptography; using MediaBrowser.Common.Cryptography;
using MediaBrowser.Controller.Authentication; using MediaBrowser.Controller.Authentication;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Cryptography; using MediaBrowser.Model.Cryptography;
using static MediaBrowser.Common.HexHelper;
namespace Emby.Server.Implementations.Library namespace Emby.Server.Implementations.Library
{ {
@ -122,7 +122,7 @@ namespace Emby.Server.Implementations.Library
{ {
return string.IsNullOrEmpty(user.EasyPassword) return string.IsNullOrEmpty(user.EasyPassword)
? null ? null
: ToHexString(PasswordHash.Parse(user.EasyPassword).Hash); : Hex.Encode(PasswordHash.Parse(user.EasyPassword).Hash);
} }
/// <summary> /// <summary>

View File

@ -8,6 +8,7 @@ using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common;
using MediaBrowser.Common.Cryptography; using MediaBrowser.Common.Cryptography;
using MediaBrowser.Common.Events; using MediaBrowser.Common.Events;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
@ -31,7 +32,6 @@ using MediaBrowser.Model.IO;
using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Users; using MediaBrowser.Model.Users;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using static MediaBrowser.Common.HexHelper;
namespace Emby.Server.Implementations.Library namespace Emby.Server.Implementations.Library
{ {
@ -490,7 +490,7 @@ namespace Emby.Server.Implementations.Library
{ {
return string.IsNullOrEmpty(user.EasyPassword) return string.IsNullOrEmpty(user.EasyPassword)
? null ? null
: ToHexString(PasswordHash.Parse(user.EasyPassword).Hash); : Hex.Encode(PasswordHash.Parse(user.EasyPassword).Hash);
} }
private void ResetInvalidLoginAttemptCount(User user) private void ResetInvalidLoginAttemptCount(User user)

View File

@ -2304,8 +2304,10 @@ namespace Emby.Server.Implementations.LiveTv
if (provider == null) if (provider == null)
{ {
throw new ResourceNotFoundException( throw new ResourceNotFoundException(
string.Format("Couldn't find provider of type: '{0}'", info.Type) string.Format(
); CultureInfo.InvariantCulture,
"Couldn't find provider of type: '{0}'",
info.Type));
} }
await provider.Validate(info, validateLogin, validateListings).ConfigureAwait(false); await provider.Validate(info, validateLogin, validateListings).ConfigureAwait(false);

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
@ -19,7 +18,6 @@ using MediaBrowser.Model.IO;
using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Updates; using MediaBrowser.Model.Updates;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using static MediaBrowser.Common.HexHelper;
namespace Emby.Server.Implementations.Updates namespace Emby.Server.Implementations.Updates
{ {
@ -455,7 +453,7 @@ namespace Emby.Server.Implementations.Updates
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var hash = ToHexString(md5.ComputeHash(stream)); var hash = Hex.Encode(md5.ComputeHash(stream));
if (!string.Equals(package.checksum, hash, StringComparison.OrdinalIgnoreCase)) if (!string.Equals(package.checksum, hash, StringComparison.OrdinalIgnoreCase))
{ {
_logger.LogError( _logger.LogError(

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -8,6 +8,7 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Api.UserLibrary; using MediaBrowser.Api.UserLibrary;
using MediaBrowser.Common;
using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
@ -25,7 +26,6 @@ using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Querying; using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Services; using MediaBrowser.Model.Services;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using static MediaBrowser.Common.HexHelper;
namespace MediaBrowser.Api.LiveTv namespace MediaBrowser.Api.LiveTv
{ {
@ -887,8 +887,9 @@ namespace MediaBrowser.Api.LiveTv
{ {
// SchedulesDirect requires a SHA1 hash of the user's password // SchedulesDirect requires a SHA1 hash of the user's password
// https://github.com/SchedulesDirect/JSON-Service/wiki/API-20141201#obtain-a-token // https://github.com/SchedulesDirect/JSON-Service/wiki/API-20141201#obtain-a-token
using (SHA1 sha = SHA1.Create()) { using (SHA1 sha = SHA1.Create())
return ToHexString( {
return Hex.Encode(
sha.ComputeHash(Encoding.UTF8.GetBytes(str))); sha.ComputeHash(Encoding.UTF8.GetBytes(str)));
} }
} }

View File

@ -10,7 +10,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -4,7 +4,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using static MediaBrowser.Common.HexHelper;
namespace MediaBrowser.Common.Cryptography namespace MediaBrowser.Common.Cryptography
{ {
@ -102,13 +101,13 @@ namespace MediaBrowser.Common.Cryptography
// Check if the string also contains a salt // Check if the string also contains a salt
if (splitted.Length - index == 2) if (splitted.Length - index == 2)
{ {
salt = FromHexString(splitted[index++]); salt = Hex.Decode(splitted[index++]);
hash = FromHexString(splitted[index++]); hash = Hex.Decode(splitted[index++]);
} }
else else
{ {
salt = Array.Empty<byte>(); salt = Array.Empty<byte>();
hash = FromHexString(splitted[index++]); hash = Hex.Decode(splitted[index++]);
} }
return new PasswordHash(id, hash, salt, parameters); return new PasswordHash(id, hash, salt, parameters);
@ -145,11 +144,11 @@ namespace MediaBrowser.Common.Cryptography
if (Salt.Length != 0) if (Salt.Length != 0)
{ {
str.Append('$') str.Append('$')
.Append(ToHexString(Salt)); .Append(Hex.Encode(Salt, false));
} }
return str.Append('$') return str.Append('$')
.Append(ToHexString(Hash)).ToString(); .Append(Hex.Encode(Hash, false)).ToString();
} }
} }
} }

View File

@ -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;
}
}
}
}

View File

@ -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];
}
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Globalization;
namespace MediaBrowser.Common
{
/// <summary>
/// Encoding and decoding hex strings.
/// </summary>
public static class Hex
{
internal const string HexCharsLower = "0123456789abcdef";
internal const string HexCharsUpper = "0123456789ABCDEF";
/// <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)
{
byte[] bytes = new byte[str.Length / 2];
int j = 0;
for (int i = 0; i < str.Length; i += 2)
{
bytes[j++] = byte.Parse(
str.Slice(i, 2),
NumberStyles.HexNumber,
CultureInfo.InvariantCulture);
}
return bytes;
}
}
}

View File

@ -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("-", "");
}
}

View File

@ -21,7 +21,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <TreatWarningsAsErrors>true</TreatWarningsAsErrors>

View File

@ -17,7 +17,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -10,7 +10,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -19,7 +19,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -16,7 +16,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -10,7 +10,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>

View File

@ -10,7 +10,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup> </PropertyGroup>

View File

@ -7,7 +7,7 @@
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup> </PropertyGroup>

View File

@ -0,0 +1,42 @@
using System;
using System.Globalization;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using MediaBrowser.Common;
namespace Jellyfin.Common.Benches
{
[MemoryDiagnoser]
public class HexDecodeBenches
{
private const int N = 1000000;
private readonly string data;
public HexDecodeBenches()
{
var tmp = new byte[N];
new Random(42).NextBytes(tmp);
data = Hex.Encode(tmp);
}
public 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;
}
[Benchmark]
public byte[] Decode() => Hex.Decode(data);
[Benchmark]
public byte[] DecodeSubString() => DecodeSubString(data);
}
}

View File

@ -0,0 +1,29 @@
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using MediaBrowser.Common;
namespace Jellyfin.Common.Benches
{
[MemoryDiagnoser]
public class HexEncodeBenches
{
private const int N = 1000;
private readonly byte[] data;
public HexEncodeBenches()
{
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("-", "");
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../MediaBrowser.Common/MediaBrowser.Common.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,14 @@
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>();
}
}
}

View File

@ -0,0 +1,19 @@
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)));
}
}
}

View File

@ -1,6 +1,6 @@
using MediaBrowser.Common;
using MediaBrowser.Common.Cryptography; using MediaBrowser.Common.Cryptography;
using Xunit; using Xunit;
using static MediaBrowser.Common.HexHelper;
namespace Jellyfin.Common.Tests namespace Jellyfin.Common.Tests
{ {
@ -15,8 +15,8 @@ namespace Jellyfin.Common.Tests
{ {
var pass = PasswordHash.Parse(passwordHash); var pass = PasswordHash.Parse(passwordHash);
Assert.Equal(id, pass.Id); Assert.Equal(id, pass.Id);
Assert.Equal(salt, ToHexString(pass.Salt)); Assert.Equal(salt, Hex.Encode(pass.Salt, false));
Assert.Equal(hash, ToHexString(pass.Hash)); Assert.Equal(hash, Hex.Encode(pass.Hash, false));
} }
[Theory] [Theory]