jellyfin-server/Emby.Common.Implementations/IO/SharpCifs/Util/Sharpen/MessageDigest.cs

123 lines
3.5 KiB
C#
Raw Normal View History

2017-04-02 00:36:06 +00:00
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
namespace SharpCifs.Util.Sharpen
{
public abstract class MessageDigest
2017-06-21 06:46:57 +00:00
{
public void Digest(byte[] buffer, int o, int len)
{
byte[] d = Digest();
d.CopyTo(buffer, o);
}
public byte[] Digest(byte[] buffer)
{
Update(buffer);
return Digest();
}
public abstract byte[] Digest();
public abstract int GetDigestLength();
public static MessageDigest GetInstance(string algorithm)
{
switch (algorithm.ToLower())
{
case "sha-1":
//System.Security.CryptographySHA1Managed not found
//return new MessageDigest<SHA1Managed> ();
return new MessageDigest<System.Security.Cryptography.SHA1>();
case "md5":
return new MessageDigest<Md5Managed>();
2017-04-02 00:36:06 +00:00
}
2017-06-21 06:46:57 +00:00
throw new NotSupportedException(
string.Format("The requested algorithm \"{0}\" is not supported.", algorithm));
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public abstract void Reset();
public abstract void Update(byte[] b);
public abstract void Update(byte b);
public abstract void Update(byte[] b, int offset, int len);
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public class MessageDigest<TAlgorithm>
: MessageDigest where TAlgorithm : HashAlgorithm //, new() //use static `Create` method
{
private TAlgorithm _hash;
2017-04-02 00:36:06 +00:00
//private CryptoStream _stream; //don't work .NET Core
private MemoryStream _stream;
2017-06-21 06:46:57 +00:00
public MessageDigest()
{
Init();
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public override byte[] Digest()
{
2017-04-02 00:36:06 +00:00
//CryptoStream -> MemoryStream, needless method
//_stream.FlushFinalBlock ();
//HashAlgorithm.`Hash` property deleted
//byte[] hash = _hash.Hash;
byte[] hash = _hash.ComputeHash(_stream.ToArray());
2017-06-21 06:46:57 +00:00
Reset();
return hash;
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public void Dispose()
{
if (_stream != null)
{
_stream.Dispose();
}
_stream = null;
2017-04-02 00:36:06 +00:00
}
2017-06-21 06:46:57 +00:00
public override int GetDigestLength()
{
return (_hash.HashSize / 8);
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
private void Init()
{
2017-04-02 00:36:06 +00:00
//use static `Create` method
//_hash = Activator.CreateInstance<TAlgorithm> ();
var createMethod = typeof(TAlgorithm).GetRuntimeMethod("Create", new Type[0]);
2017-06-21 06:46:57 +00:00
_hash = (TAlgorithm)createMethod.Invoke(null, new object[] { });
2017-04-02 00:36:06 +00:00
//HashAlgorithm cannot cast `ICryptoTransform` on .NET Core, gave up using CryptoStream.
//_stream = new CryptoStream(Stream.Null, _hash, CryptoStreamMode.Write);
//_stream = new CryptoStream(_tmpStream, (ICryptoTransform)_hash, CryptoStreamMode.Write);
_stream = new MemoryStream();
}
2017-06-21 06:46:57 +00:00
public override void Reset()
{
Dispose();
Init();
}
public override void Update(byte[] input)
{
_stream.Write(input, 0, input.Length);
}
public override void Update(byte input)
{
_stream.WriteByte(input);
}
public override void Update(byte[] input, int index, int count)
{
if (count < 0)
Console.WriteLine("Argh!");
_stream.Write(input, index, count);
}
}
2017-04-02 00:36:06 +00:00
}