2323using MongoDB . Bson . Serialization ;
2424using MongoDB . Driver . Core . Bindings ;
2525using MongoDB . Driver . Core . Operations ;
26+ using MongoDB . Shared ;
2627
2728namespace MongoDB . Driver . GridFS
2829{
@@ -49,7 +50,7 @@ internal class GridFSForwardOnlyUploadStream<TFileId> : GridFSUploadStream<TFile
4950 private readonly TFileId _id ;
5051 private readonly BsonValue _idAsBsonValue ;
5152 private long _length ;
52- private readonly MD5 _md5 ;
53+ private readonly IncrementalMD5 _md5 ;
5354 private readonly BsonDocument _metadata ;
5455
5556 // constructors
@@ -75,7 +76,7 @@ public GridFSForwardOnlyUploadStream(
7576 _batchSize = batchSize ;
7677
7778 _batch = new List < byte [ ] > ( ) ;
78- _md5 = MD5 . Create ( ) ;
79+ _md5 = IncrementalMD5 . Create ( ) ;
7980
8081 var idSerializer = bucket . Options . SerializerRegistry . GetSerializer < TFileId > ( ) ;
8182 var idSerializationInfo = new BsonSerializationInfo ( "_id" , idSerializer , typeof ( TFileId ) ) ;
@@ -271,7 +272,7 @@ private BsonDocument CreateFilesCollectionDocument()
271272 { "length" , _length } ,
272273 { "chunkSize" , _chunkSizeBytes } ,
273274 { "uploadDate" , uploadDateTime } ,
274- { "md5" , BsonUtils . ToHexString ( _md5 . Hash ) } ,
275+ { "md5" , BsonUtils . ToHexString ( _md5 . GetHashAndReset ( ) ) } ,
275276 { "filename" , _filename } ,
276277 { "contentType" , _contentType , _contentType != null } ,
277278 { "aliases" , ( ) => new BsonArray ( _aliases . Select ( a => new BsonString ( a ) ) ) , _aliases != null } ,
@@ -296,7 +297,7 @@ private IEnumerable<BsonDocument> CreateWriteBatchChunkDocuments()
296297 chunkDocuments . Add ( chunkDocument ) ;
297298
298299 _batchPosition += chunk . Length ;
299- _md5 . TransformBlock ( chunk , 0 , chunk . Length , null , 0 ) ;
300+ _md5 . AppendData ( chunk , 0 , chunk . Length ) ;
300301 }
301302
302303 return chunkDocuments ;
@@ -460,7 +461,6 @@ private void WriteFinalBatch(CancellationToken cancellationToken)
460461 TruncateFinalChunk ( ) ;
461462 WriteBatch ( cancellationToken ) ;
462463 }
463- _md5 . TransformFinalBlock ( new byte [ 0 ] , 0 , 0 ) ;
464464 }
465465
466466 private async Task WriteFinalBatchAsync ( CancellationToken cancellationToken )
@@ -470,7 +470,6 @@ private async Task WriteFinalBatchAsync(CancellationToken cancellationToken)
470470 TruncateFinalChunk ( ) ;
471471 await WriteBatchAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
472472 }
473- _md5 . TransformFinalBlock ( new byte [ 0 ] , 0 , 0 ) ;
474473 }
475474 }
476475}
0 commit comments