Salt C # MD5 ComputeHash to stream

I see no way to salt the MD5.ComputeHash (stream). Am I missing some way to input bytes in a HashAlgorithm?

I tried to execute ComputeHash (byte []) before doing the thread calculation, but, unsurprisingly, it did not affect. Any ideas (other than modifying the file)?

Thank you for your time.

addition To be a little more specific, I want to use a stream to get a hash on a large file that I don't want to load into memory.

FileInfo myFI= new FileInfo("bigfile.dat");
FileStream myIFS = piFile.OpenRead();
MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash ( myIFS );
myIFS.Close ();
+3
source share
5 answers

The answer to the lack of examples in my opinion: you really do not need to solo it.

-, MD5, - :

input = > MD5 = > output

, - ( ). , , , , :

input + salt= > MD5 = > output

. . - -. , MD5 . , .

, , .

, , . ( ) .

+5

:

    private static byte[] _emptyBuffer = new byte[0];

    public static byte[] CalculateMD5(Stream stream)
    {
        return CalculateMD5(stream, 64 * 1024);
    }

    public static byte[] CalculateMD5(Stream stream, int bufferSize)
    {
        MD5 md5Hasher = MD5.Create();

        byte[] buffer = new byte[bufferSize];
        int readBytes;

        while ((readBytes = stream.Read(buffer, 0, bufferSize)) > 0)
        {
            md5Hasher.TransformBlock(buffer, 0, readBytes, buffer, 0);
        }

        md5Hasher.TransformFinalBlock(_emptyBuffer, 0, 0);

        return md5Hasher.Hash;
    }
+4

HMACMD5 Key. , HMAC, -, .

+3

, :

byte[] saltedBytes;
//TODO: fill the saltedBytes;
var hasher=new MD5CryptoServiceProvider();
var memoryStream=new MemoryStream(saltedBytes);
hasher.ComputeHash(memoryStream);
memoryStream.Close;
+1

, , Dabblernl, FileStream, SO MD5SUM #, MD5CryptoServiceProvider .

, :

public class MergedStream : Stream, IDisposable
{
    Stream s1;
    Stream s2;

    public MergedStream(Stream first, Stream second)
    {
        s1 = first;
        s2 = second;
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        int s1count = (int)Math.Min((long)count, s1.Length - s1.Position);
        int bytesRead = 0;

        if (s1count > 0)
        {
            bytesRead += s1.Read(buffer, offset, s1count);
        }

        if (s1count < count)
        {
            bytesRead += s2.Read(buffer, offset + s1count, count - s1count);
        }

        return bytesRead;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        throw new NotImplementedException();
    }

    public override bool CanRead
    {
        get { return s1.CanRead && s2.CanRead; }
    }

    public override bool CanSeek
    {
        get { return s1.CanSeek && s2.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return s1.CanWrite && s2.CanWrite; }
    }

    public override void Flush()
    {
        s1.Flush();
        s2.Flush();
    }

    public override long Length
    {
        get { return s1.Length + s2.Length; }
    }

    public override long Position
    {
        get
        {
            return s1.Position + s2.Position;
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        throw new NotImplementedException();
    }

    public override void SetLength(long value)
    {
        throw new NotImplementedException();
    }

    void IDisposable.Dispose()
    {
        s1.Dispose();
        s2.Dispose();
    }
}

,

        FileStream fs = new FileStream(@"c:\text.txt", FileMode.Open);
        var m = new MemoryStream(ToAnsiiBytes("SALT"), false);
        var ms = new MergedStream(fs, m);

        var C = hasher.ComputeHash(ms);
        PrintHash(Console.Out, C);

ToAnsiiBytes PrintHash - :

    static void HashAndPrint(TextWriter op, string text)
    {
        MD5 md5 = new MD5CryptoServiceProvider();

        byte[] bytes = ToAnsiiBytes(text);

        byte[] hash = md5.ComputeHash(bytes);

        PrintHash(Console.Out, hash);
        Console.Out.WriteLine( " = {0}", text);
    }

    public static void PrintHash(TextWriter op, byte[] hash)
    {
        foreach (byte b in hash)
        {
            op.Write("{0:X2}", b);
        }
    }

c:\text.txt , , , + , "totoSALT"

        FileStream fs = new FileStream(@"c:\text.txt", FileMode.Open);

        var hasher = new MD5CryptoServiceProvider();
        var A = hasher.ComputeHash(fs);
        PrintHash(Console.Out, A);
        Console.Out.WriteLine();

        var salt = new byte[] { 0x53, 0x41, 0x4C, 0x54 };

        var B = hasher.ComputeHash(ToAnsiiBytes("SALT"));
        PrintHash(Console.Out, B);
        Console.Out.WriteLine();

        var m = new MemoryStream(ToAnsiiBytes("SALT"), false);

        fs.Seek(0, SeekOrigin.Begin);
        var ms = new MergedStream(fs, m);

        var C = hasher.ComputeHash(ms);
        PrintHash(Console.Out, C);
        Console.Out.WriteLine();


        HashAndPrint(Console.Out, "toto");
        HashAndPrint(Console.Out, "totoSALT");
        HashAndPrint(Console.Out, "SALT");

F71DBE52628A3F83A77AB494817525C6
8C4F4370C53E0C1E1AE9ACD577DDDBED
308DB2451D6580FEEB09FCF2DC1CEE19
F71DBE52628A3F83A77AB494817525C6 = toto
308DB2451D6580FEEB09FCF2DC1CEE19 = totoSALT
8C4F4370C53E0C1E1AE9ACD577DDDBED = SALT
0

Source: https://habr.com/ru/post/1708583/


All Articles