Skip to content

Commit

Permalink
Migrate to .NET 9 and temporarily remove other frameworks support
Browse files Browse the repository at this point in the history
  • Loading branch information
sergezhigunov committed Nov 20, 2024
1 parent fb67481 commit 0fd5b75
Show file tree
Hide file tree
Showing 36 changed files with 136 additions and 358 deletions.
53 changes: 28 additions & 25 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ charset = utf-8-bom

[*.{cs,vb}]
dotnet_sort_system_directives_first = true
dotnet_style_qualification_for_field = false:silent
dotnet_style_qualification_for_property = false:silent
dotnet_style_qualification_for_method = false:silent
dotnet_style_qualification_for_event = false:silent
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_predefined_type_for_member_access = true:silent
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
dotnet_style_qualification_for_field = false:none
dotnet_style_qualification_for_property = false:none
dotnet_style_qualification_for_method = false:none
dotnet_style_qualification_for_event = false:none
dotnet_style_predefined_type_for_locals_parameters_members = true:none
dotnet_style_predefined_type_for_member_access = true:none
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:none
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:none
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:none
dotnet_style_require_accessibility_modifiers = for_non_interface_members:none
dotnet_style_readonly_field = true:suggestion
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:none
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_prefer_auto_properties = true:none
dotnet_style_prefer_conditional_expression_over_assignment = true:none
dotnet_style_prefer_conditional_expression_over_return = true:none
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
dotnet_naming_style.camel_case_style.capitalization = camel_case
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
Expand Down Expand Up @@ -72,21 +72,21 @@ dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private

[*.cs]
csharp_style_var_for_built_in_types = true:silent
csharp_style_var_when_type_is_apparent = true:silent
csharp_style_var_elsewhere = true:silent
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none
csharp_style_expression_bodied_methods = false:none
csharp_style_expression_bodied_constructors = false:none
csharp_style_expression_bodied_operators = false:none
csharp_style_expression_bodied_properties = true:none
csharp_style_expression_bodied_indexers = true:none
csharp_style_expression_bodied_accessors = true:none
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
csharp_prefer_braces = false:silent
csharp_prefer_braces = false:none
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
Expand Down Expand Up @@ -114,6 +114,9 @@ csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true
csharp_style_namespace_declarations = file_scoped
csharp_using_directive_placement = outside_namespace:none
csharp_style_prefer_range_operator = false:none

[*.vb]
visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore -c Release
- name: Test
run: dotnet test --no-build --verbosity normal -c Release -f net8.0
run: dotnet test --no-build --verbosity normal -c Release -f net9.0
7 changes: 0 additions & 7 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)Key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Using Include="System.Security.Cryptography" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>net9.0</TargetFrameworks>
<OutputType>Exe</OutputType>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.100",
"version": "9.0.100",
"rollForward": "latestMinor"
}
}
3 changes: 1 addition & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningLevel>7</WarningLevel>
<IsPackable>true</IsPackable>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand All @@ -17,7 +16,7 @@
<AnalysisLevel>latest-all</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<NoWarn>$(NoWarn);CA1508;CA1859;CA5358;NU1903;SYSLIB0045</NoWarn>
<NoWarn>$(NoWarn);CA5358;SYSLIB0045</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
44 changes: 20 additions & 24 deletions src/OpenGost.Security.Cryptography/CMAC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@ namespace OpenGost.Security.Cryptography;
[ComVisible(true)]
public abstract class CMAC : KeyedHashAlgorithm
{
private static readonly IReadOnlyDictionary<int, byte[]> _irreduciblePolynomials = new Dictionary<int, byte[]>
{
[64] =
private static readonly byte[]
_irreduciblePolynomial64 =
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1B
],
[128] =
_irreduciblePolynomial128 =
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87
],
[256] =
_irreduciblePolynomial256 =
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x25
],
};
];

private SymmetricAlgorithm _symmetricAlgorithm = null!;
private ICryptoTransform? _encryptor;
Expand Down Expand Up @@ -58,12 +56,8 @@ public override byte[] Key
get => (byte[])KeyValue.Clone();
set
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
throw new ArgumentNullException(nameof(value));
#endif

if (_hashing)
throw new CryptographicException(CryptographyStrings.CryptographicSymmetricAlgorithmKeySet);

Expand Down Expand Up @@ -170,19 +164,19 @@ protected override void HashCore(byte[] array, int ibStart, int cbSize)
Buffer.BlockCopy(array, ibStart, _buffer, _bufferLength, bytesToCopy);
ibStart += bytesToCopy;
cbSize -= bytesToCopy;
_encryptor!.TransformBlock(_buffer, 0, _bytesPerBlock, _temp, 0);
_ = _encryptor!.TransformBlock(_buffer, 0, _bytesPerBlock, _temp, 0);
_bufferLength = 0;
}

if (cbSize >= _bytesPerBlock && _bufferLength == _bytesPerBlock)
{
_encryptor!.TransformBlock(_buffer, 0, _bytesPerBlock, _temp, 0);
_ = _encryptor!.TransformBlock(_buffer, 0, _bytesPerBlock, _temp, 0);
_bufferLength = 0;
}

while (cbSize > _bytesPerBlock)
{
_encryptor!.TransformBlock(array, ibStart, _bytesPerBlock, _temp, 0);
_ = _encryptor!.TransformBlock(array, ibStart, _bytesPerBlock, _temp, 0);
ibStart += _bytesPerBlock;
cbSize -= _bytesPerBlock;
}
Expand Down Expand Up @@ -242,17 +236,18 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

#region Creation factory methods

/// <summary>
/// Creates an instance of the default implementation of <see cref="CMAC"/> algorithm.
/// </summary>
/// <returns>
/// A new instance of <see cref="CMAC"/>.
/// </returns>
[ComVisible(false)]
[Obsolete(Obsoletions.DefaultCryptoAlgorithmsMessage,
DiagnosticId = Obsoletions.DefaultCryptoAlgorithmsDiagId,
UrlFormat = Obsoletions.SharedUrlFormat)]
public static new CMAC Create()
=> Create(CryptoConstants.CMACGrasshopperAlgorithmName);
=> new CMACGrasshopper();

/// <summary>
/// Creates an instance of a specified implementation of <see cref="CMAC"/> algorithm.
Expand All @@ -267,8 +262,6 @@ protected override void Dispose(bool disposing)
public static new CMAC Create(string algorithmName)
=> (CMAC)CryptoConfig.CreateFromName(algorithmName)!;

#endregion

private void EnsureEncryptorInitialized()
{
if (_encryptor is null)
Expand Down Expand Up @@ -319,9 +312,12 @@ private static void LeftShiftLittleEndianOneBit(byte[] data)

private static byte[] GetIrreduciblePolynomial(int blockSize)
{
if (_irreduciblePolynomials.TryGetValue(blockSize, out var result))
return result;

throw new CryptographicException(CryptographyStrings.CryptographicInvalidBlockSize);
return blockSize switch
{
64 => _irreduciblePolynomial64,
128 => _irreduciblePolynomial128,
256 => _irreduciblePolynomial256,
_ => throw new CryptographicException(CryptographyStrings.CryptographicInvalidBlockSize),
};
}
}
12 changes: 2 additions & 10 deletions src/OpenGost.Security.Cryptography/ECCurveOidMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,23 +453,15 @@ private static IReadOnlyDictionary<string, ECCurve> ECCurveOidDictionary

public static bool OidValueRegistered(string oidValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(oidValue);
#else
if (oidValue is null)
throw new ArgumentNullException(nameof(oidValue));
#endif

return ECCurveOidDictionary.ContainsKey(oidValue);
}

public static ECCurve GetExplicitCurveByOid(string oidValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(oidValue);
#else
if (oidValue is null)
throw new ArgumentNullException(nameof(oidValue));
#endif

if (!ECCurveOidDictionary.ContainsKey(oidValue))
throw new NotImplementedException();

Expand Down
19 changes: 1 addition & 18 deletions src/OpenGost.Security.Cryptography/GostECDsa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ protected GostECDsa()
KeySizeValue = 512;
}

#region Creation factory methods

/// <summary>
/// Creates an instance of the default implementation of <see cref="GostECDsa"/> algorithm.
/// </summary>
Expand All @@ -37,20 +35,5 @@ protected GostECDsa()
/// </returns>
[ComVisible(false)]
public static new GostECDsa Create()
=> Create(CryptoConstants.GostECDsaAlgorithmName);

/// <summary>
/// Creates an instance of a specified implementation of <see cref="GostECDsa"/> algorithm.
/// </summary>
/// <param name="algorithmName">
/// The name of the specific implementation of <see cref="GostECDsa"/> to be used.
/// </param>
/// <returns>
/// A new instance of <see cref="GostECDsa"/> using the specified implementation.
/// </returns>
[ComVisible(false)]
public static new GostECDsa Create(string algorithmName)
=> (GostECDsa)CryptoConfig.CreateFromName(algorithmName)!;

#endregion
=> new GostECDsaManaged();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using OpenGost.Security.Cryptography.Asn1;
using OpenGost.Security.Cryptography.Properties;

#pragma warning disable IDE0130
namespace System.Security.Cryptography.X509Certificates;
#pragma warning restore IDE0130

using static CryptoConstants;

Expand Down Expand Up @@ -32,12 +34,7 @@ public static class GostECDsaCertificateExtensions
/// </exception>
public static GostECDsa? GetGostECDsaPublicKey(this X509Certificate2 certificate)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(certificate);
#else
if (certificate is null)
throw new ArgumentNullException(nameof(certificate));
#endif

if (!IsGostECDsa(certificate))
return null;
Expand Down Expand Up @@ -87,7 +84,7 @@ public static class GostECDsaCertificateExtensions
private static bool IsGostECDsa(X509Certificate2 certificate)
{
var value = certificate.PublicKey.Oid.Value;
if (value != GostECDsa256OidValue && value != GostECDsa512OidValue)
if (value is not GostECDsa256OidValue and not GostECDsa512OidValue)
return false;

foreach (var extension in certificate.Extensions)
Expand Down
Loading

0 comments on commit 0fd5b75

Please sign in to comment.