[Mono-bugs] [Bug 345510] CryptoStream ignores the ICryptoTransform. CanTransformMultipleBlocks property when reading

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Mon Dec 3 11:26:06 EST 2007


Sebastien Pouliot <spouliot at novell.com> changed:

           What    |Removed                                         |Added
           Severity|Normal                                          |Enhancement
             Status|ASSIGNED                                        |NEEDINFO
      Info Provider|                                                |lordhabbit at gmail.com

--- Comment #2 from Sebastien Pouliot <spouliot at novell.com>  2007-12-03 09:26:06 MST ---
Here are my results for a 90mb file using sha1sum, HashAlgorithm.ComputeHash
and CryptoStream

poupou at pollux:~/src/bugzilla> l
-rw-r--r-- 1 poupou users 90298052 2007-09-07 17:43

poupou at pollux:~/src/bugzilla> time sha1sum

real    0m2.314s
user    0m1.440s
sys     0m0.104s

poupou at pollux:~/src/bugzilla> time mono 345510.exe SHA1 hash

real    0m3.556s
user    0m3.416s
sys     0m0.072s

poupou at pollux:~/src/bugzilla> time mono 345510.exe SHA1 cryptostream

real    0m3.550s
user    0m3.444s
sys     0m0.048s

I'm actually impressed that the later is as fast as ComputeHash since it
involves extra steps. This also means that my CryptoStream code is different
than yours ;-)

Here's mine:

using System;
using System.IO;
using System.Security.Cryptography;

class Digest {

        private const int Size = 4096;

        static byte[] hash (HashAlgorithm algo, Stream s)
                return algo.ComputeHash (s);

        static byte[] cryptostream (HashAlgorithm algo, Stream s)
                // note: it could make sense to use a BufferedStream
                byte[] data = new byte[Size];
                using (CryptoStream cs = new CryptoStream (Stream.Null, algo,
CryptoStreamMode.Write)) {
                        int size = s.Read (data, 0, Size);
                        while (size > 0) {
                                cs.Write (data, 0, size);
                                size = s.Read (data, 0, Size);
                return algo.Hash;

        // args: algorithm method filename
        static void Main (string [] args)
                using (FileStream fs = File.OpenRead (args [2])) {
                        using (HashAlgorithm algo = HashAlgorithm.Create (args
[0])) {
                                byte[] digest = null;

                                switch (args [1].ToLower ()) {
                                case "hash":
                                        digest = hash (algo, fs);
                                case "cryptostream":
                                        digest = cryptostream (algo, fs);
                                        throw new NotSupportedException (args
                                Console.WriteLine (BitConverter.ToString

Downgrading priority to Enhancement since the original test case _probably_
occurs only when reading byte-by-byte (which is bad for performance, crypto or

Please attach your own test case so I can see where the bottleneck lies in this
particular case.

Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

More information about the mono-bugs mailing list