Files
hcs/Hcs.Client/GostCryptography/Reflection/CryptographyUtils.cs
HOME-LAPTOP\kshkulev 33ab055b43 Add project
Basic formatting applied. Unnecessary comments have been removed. Suspicious code is covered by TODO.
2025-08-12 11:21:10 +09:00

58 lines
1.8 KiB
C#

using System;
using System.Reflection;
using System.Security.Cryptography;
namespace GostCryptography.Reflection
{
static class CryptographyUtils
{
private static readonly object ObjToHashAlgorithmMethodSync = new object();
private static volatile MethodInfo _objToHashAlgorithmMethod;
public static HashAlgorithm ObjToHashAlgorithm(object hashAlg)
{
if (hashAlg == null)
{
throw ExceptionUtility.ArgumentNull(nameof(hashAlg));
}
HashAlgorithm hashAlgorithm = null;
if (_objToHashAlgorithmMethod == null)
{
lock (ObjToHashAlgorithmMethodSync)
{
if (_objToHashAlgorithmMethod == null)
{
var utilsType = Type.GetType("System.Security.Cryptography.Utils");
if (utilsType != null)
{
_objToHashAlgorithmMethod = utilsType.GetMethod("ObjToHashAlgorithm", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(object) }, null);
}
}
}
}
if (_objToHashAlgorithmMethod != null)
{
try
{
hashAlgorithm = _objToHashAlgorithmMethod.Invoke(null, new[] { hashAlg }) as HashAlgorithm;
}
catch (TargetInvocationException exception)
{
if (exception.InnerException != null)
{
throw exception.InnerException;
}
throw;
}
}
return hashAlgorithm;
}
}
}