当前位置: 首页>>代码示例>>C#>>正文


C# System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock方法代码示例

本文整理汇总了C#中System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock方法的典型用法代码示例。如果您正苦于以下问题:C# System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock方法的具体用法?C# System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock怎么用?C# System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.MD5CryptoServiceProvider的用法示例。


在下文中一共展示了System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: _Get_File_MD5

        /// <summary>
        /// 计算指定文件的MD5值
        /// </summary>
        /// <param name="file">文件名</param>
        /// <returns>文件的MD5值(若出现异常则返回null)</returns>
        private byte[] _Get_File_MD5(string file)
        {
            byte[] ret = null;

            var csp = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] buffer = new byte[STREAM_BUFFER_SIZE];

            csp.Initialize();

            FileInfo fi = new FileInfo(file);
            FileStream fs = null;
            try
            {
                fs = fi.OpenRead();
            }
            catch (Exception)
            {
                return ret;
            }
            //构造MD5计算事件的参数
            File_MD5_Calculate_Event_Arg e = new File_MD5_Calculate_Event_Arg();
            e.Current_Position = 0;
            e.File_Extension = fi.Extension;
            e.Full_File_Name = fi.FullName;
            e.File_Length = fi.Length;
            e.File_Name = fi.Name;

            if (File_MD5_Begin_Calculate_Event != null)
                File_MD5_Begin_Calculate_Event(e);

            //读取文件计算MD5
            try
            {
                fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);

                int nRead = 0;
                do
                {
                    nRead = fs.Read(buffer, 0, STREAM_BUFFER_SIZE);
                    csp.TransformBlock(buffer, 0, nRead, buffer, 0);

                    e.Current_Position += nRead;

                    if (File_MD5_Calculating_Event != null)
                        File_MD5_Calculating_Event(e);

                } while (nRead != 0);

                csp.TransformFinalBlock(buffer, 0, 0);

                ret = csp.Hash;
            }
            finally
            {
                try
                {
                    fs.Close();
                }
                catch (Exception)
                {
                }
                csp.Clear();
            }

            if (File_MD5_End_Calculate_Event != null)
                File_MD5_End_Calculate_Event(e);

            return ret;
        }
开发者ID:qhgz2013,项目名称:FolderSync,代码行数:74,代码来源:repository.cs

示例2: saveChunks

        public int saveChunks()
        {
            if ( _saved )
            throw new Exception( "already saved!" );

            byte[] b = new byte[GridFS.DEFAULT_CHUNKSIZE];

            long total = 0;
            int cn = 0;

            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();

            BinaryReader reader = new BinaryReader( _in);

            while ( true )
            {
            int start =0;

            while ( start < b.Length )
            {
                int r = reader.Read( b , start , b.Length - start );
                if ( r == 0 )
                    throw new Exception( "i'm doing something wrong" );
                if ( r < 0 )
                    break;
                start += r;
            }

            total += start;

            byte[] mine = b;

            if ( start != b.Length )
            {
                mine = new byte[start];
                Array.ConstrainedCopy( b , 0 , mine , 0 , start );
            }

            DBObject chunk = BasicDBObjectBuilder.start()
                .Add( "files_id" , _id )
                .Add( "n" , cn++ )
                .Add( "data" , mine )
                .Value;

            _fs._chunkCollection.save( chunk );

            if ( start < b.Length )
            {
                x.TransformFinalBlock(b, 0, b.Length);
                break;
            }
            else
            {
                x.TransformBlock(b, 0, b.Length, b, 0)
            }
            }
            _md5 = Util.toHex(x.Hash);
            _length = total;
            _saved = true;
            return cn;
        }
开发者ID:automatonic,项目名称:mongodb-net,代码行数:61,代码来源:GridFSInputFile.cs


注:本文中的System.Security.Cryptography.MD5CryptoServiceProvider.TransformBlock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。