Skip to content

Commit

Permalink
Replace call to deprecated cryptography method
Browse files Browse the repository at this point in the history
BouncyCastle.Cryptography is now used behind the scene with several digest algorithms supported
  • Loading branch information
nurhafiz committed May 4, 2024
1 parent ae40570 commit 4f609d1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
48 changes: 44 additions & 4 deletions src/Toimik.WarcProtocol/DigestFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,59 @@

namespace Toimik.WarcProtocol;

using System.Security.Cryptography;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;

public class DigestFactory(string hashName)
{
public string HashName { get; } = hashName;

[ExcludeFromCodeCoverage]
public virtual string CreateDigest(byte[] buffer)
{
using var algorithm = HashAlgorithm.Create(HashName);
IDigest digest = HashName.ToUpper() switch
{
"BLAKE2B" => new Blake2bDigest(),
"BLAKE2S" => new Blake2sDigest(),
"BLAKE2XS" => new Blake2xsDigest(),
"GOST3411_2012_256" => new Gost3411_2012_256Digest(),
"GOST3411_2012_512" => new Gost3411_2012_512Digest(),
"GOST3411" => new Gost3411Digest(),
"HARAKA256" => new Haraka256Digest(),
"HARAKA512" => new Haraka512Digest(),
"KECCAK" => new KeccakDigest(),
"MD2" => new MD2Digest(),
"MD4" => new MD4Digest(),
"MD5" => new MD5Digest(),
"NULL" => new NullDigest(),
"RIPEMD128" => new RipeMD128Digest(),
"RIPEMD160" => new RipeMD160Digest(),
"RIPEMD256" => new RipeMD256Digest(),
"RIPEMD320" => new RipeMD320Digest(),
"SHA1" => new Sha1Digest(),
"SHA224" => new Sha224Digest(),
"SHA256" => new Sha256Digest(),
"SHA384" => new Sha384Digest(),
"SHA3" => new Sha3Digest(),
"SHA512" => new Sha512Digest(),
"SHAKE" => new ShakeDigest(),
"SM3" => new SM3Digest(),
"TIGER" => new TigerDigest(),
"WHIRLPOOL" => new WhirlpoolDigest(),
_ => throw new ArgumentException($"Unsupported hash algorithm: {HashName}"),
};
digest.Reset();
digest.BlockUpdate(
buffer,
0,
buffer.Length);

byte[] bytes = new byte[digest.GetDigestSize()];
digest.DoFinal(bytes, 0);

// It is assumed that the hash name is valid
var bytes = algorithm!.ComputeHash(buffer);
var builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Toimik.WarcProtocol/Toimik.WarcProtocol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<AssemblyName>Toimik.WarcProtocol</AssemblyName>
<PackageVersion>0.10.4</PackageVersion>
<PackageVersion>0.10.5</PackageVersion>
<Authors>Nurhafiz</Authors>
<Version>0.10.4</Version>
<Version>0.10.5</Version>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Company>Toimik</Company>
<Description>
Expand All @@ -24,6 +24,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.3.0" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 4f609d1

Please sign in to comment.