protected AsyncUploaderBase(ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, IPlatformFile file, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, file, expirationDays) { Config = config ?? new FileUploaderConfig(); HashProvider = MD5HashProviderFactory.GetHashProvider().CreateHash(); }
public FilePart GetNextPart(long requestedPartSize) { try { int partSize = Convert.ToInt32(Math.Min(requestedPartSize, fileLength - streamPosition)); byte[] content = new byte[partSize]; stream.Read(content, 0, partSize); bool isLast = streamPosition + partSize >= fileLength; string hash = MD5HashProviderFactory.GetHashProvider().CreateHash().ComputeHash(content); var part = new FilePart { Bytes = content, Hash = hash, Index = partCount, Offset = streamPosition, IsLastPart = isLast }; streamPosition += partSize; partCount += 1; if (isLast) { HasMore = false; fileHash.Finalize(content, 0, content.Length); } else { fileHash.Append(content, 0, content.Length); } return(part); } catch { //improvement: return object to propagate this exception HasMore = false; return(null); } }
protected AsyncUploaderBase( ShareFileClient client, UploadSpecificationRequest uploadSpecificationRequest, Stream stream, FileUploaderConfig config = null, int?expirationDays = null) : base(client, uploadSpecificationRequest, stream, config, expirationDays) { HashProvider = MD5HashProviderFactory.GetHashProvider().CreateHash(); }
public static string GetHash(string value) { byte[] buffer = Encoding.UTF8.GetBytes(value); var hash = MD5HashProviderFactory.GetHashProvider().CreateHash(); hash.Append(buffer, 0, buffer.Length); hash.Finalize(new byte[1], 0, 0); string result = hash.GetComputedHashAsString(); return(result); }
protected async Task CheckResumeAsync() { if (UploadSpecification.IsResume) { if (UploadSpecification.ResumeFileHash != await CalculateHashAsync(UploadSpecification.ResumeOffset)) { HashProvider = MD5HashProviderFactory.GetHashProvider().CreateHash(); UploadSpecification.ResumeIndex = 0; UploadSpecification.ResumeOffset = 0; } else { UploadSpecification.ResumeIndex += 1; } } }
protected void CheckResume() { if (UploadSpecification.IsResume) { if (UploadSpecification.ResumeFileHash != CalculateHash(UploadSpecification.ResumeOffset)) { HashProvider = MD5HashProviderFactory.GetHashProvider().CreateHash(); UploadSpecification.ResumeIndex = 0; UploadSpecification.ResumeOffset = 0; } else { UploadSpecification.ResumeIndex += 1; } } }
private async Task <ReadFileResult> ReadFile(int length) { IBuffer content = bufferAllocator.Allocate(length); Stream dest = content.GetStream(); IMD5HashProvider chunkHash = MD5HashProviderFactory.GetHashProvider().CreateHash(); byte[] b = ArrayPool <byte> .Shared.Rent(Configuration.BufferSize); try { int read = 0; int toRead = length; do { read = await fileStream.ReadAsync(b, offset : 0, count : Math.Min(b.Length, toRead)).ConfigureAwait(false); toRead -= read; await dest.WriteAsync(b, offset : 0, count : read).ConfigureAwait(false); chunkHash.Append(b, offset: 0, size: read); fileHash.Append(b, offset: 0, size: read); } while (read > 0 && toRead > 0); if (toRead > 0) { throw new Exception($"Expected to read {length} bytes, actual read {length - toRead} bytes"); } chunkHash.Finalize(ArrayPool <byte> .Shared.Rent(0), 0, 0); return(new ReadFileResult { Content = content, Hash = chunkHash.GetComputedHashAsString() }); } catch { content.Dispose(); throw; } finally { ArrayPool <byte> .Shared.Return(b); dest.Dispose(); } }
protected async Task <string> CalculateHashAsync(long count) { var localHash = MD5HashProviderFactory.GetHashProvider().CreateHash(); var buffer = new byte[Configuration.BufferSize]; do { var bytesToRead = count < Configuration.BufferSize ? (int)count : Configuration.BufferSize; var bytesRead = await FileStream.ReadAsync(buffer, 0, bytesToRead); if (bytesRead > 0) { localHash.Append(buffer, 0, bytesToRead); HashProvider.Append(buffer, 0, bytesToRead); } count -= bytesRead; } while (count > 0); localHash.Finalize(new byte[1], 0, 0); FileStream.Seek(0, SeekOrigin.Begin); return(localHash.GetComputedHashAsString()); }
protected async Task <string> CalculateHashAsync(long count) { var localHash = MD5HashProviderFactory.GetHashProvider().CreateHash(); using (var fileStream = await File.OpenReadAsync()) { var buffer = new byte[DefaultBufferLength]; do { var bytesToRead = count < DefaultBufferLength ? (int)count : DefaultBufferLength; var bytesRead = fileStream.Read(buffer, 0, bytesToRead); if (bytesRead > 0) { localHash.Append(buffer, 0, bytesToRead); HashProvider.Append(buffer, 0, bytesToRead); } count -= bytesRead; } while (count > 0); } localHash.Finalize(new byte[1], 0, 0); return(localHash.GetComputedHashAsString()); }
private static IMD5HashProvider NewHash() => MD5HashProviderFactory.GetHashProvider().CreateHash();