-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBaseContentCrypt.cs
More file actions
59 lines (46 loc) · 2.22 KB
/
BaseContentCrypt.cs
File metadata and controls
59 lines (46 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Security.Cryptography;
namespace SecureFolderFS.Core.Cryptography.ContentCrypt
{
/// <inheritdoc cref="IContentCrypt"/>
internal abstract class BaseContentCrypt : IContentCrypt
{
/// <inheritdoc/>
public abstract int ChunkPlaintextSize { get; }
/// <inheritdoc/>
public abstract int ChunkCiphertextSize { get; }
/// <inheritdoc/>
public abstract int ChunkFirstReservedSize { get; }
/// <inheritdoc/>
public abstract void EncryptChunk(ReadOnlySpan<byte> plaintextChunk, long chunkNumber, ReadOnlySpan<byte> header, Span<byte> ciphertextChunk);
/// <inheritdoc/>
public abstract bool DecryptChunk(ReadOnlySpan<byte> ciphertextChunk, long chunkNumber, ReadOnlySpan<byte> header, Span<byte> plaintextChunk);
/// <inheritdoc/>
public virtual long CalculateCiphertextSize(long plaintextSize)
{
var overheadPerChunk = ChunkCiphertextSize - ChunkPlaintextSize;
var fullChunksCount = plaintextSize / ChunkPlaintextSize;
var additionalPlaintextBytes = plaintextSize % ChunkPlaintextSize;
var additionalCiphertextBytes = (additionalPlaintextBytes == 0L) ? 0L : additionalPlaintextBytes + overheadPerChunk;
return ChunkCiphertextSize * fullChunksCount + additionalCiphertextBytes;
}
/// <inheritdoc/>
public virtual long CalculatePlaintextSize(long ciphertextSize)
{
if (ciphertextSize <= 0L)
return 0L;
var chunkOverhead = ChunkCiphertextSize - ChunkPlaintextSize;
var chunksCount = ciphertextSize / ChunkCiphertextSize;
var additionalCiphertextBytes = ciphertextSize % ChunkCiphertextSize;
if (additionalCiphertextBytes > 0 && additionalCiphertextBytes <= chunkOverhead)
return -1L;
var additionalPlaintextBytes = (additionalCiphertextBytes == 0L) ? 0L : additionalCiphertextBytes - chunkOverhead;
var final = ChunkPlaintextSize * chunksCount + additionalPlaintextBytes;
return final >= 0L ? final : -1L;
}
/// <inheritdoc/>
public virtual void Dispose()
{
}
}
}