-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAesSivNameCrypt.cs
More file actions
48 lines (43 loc) · 1.47 KB
/
AesSivNameCrypt.cs
File metadata and controls
48 lines (43 loc) · 1.47 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
using SecureFolderFS.Core.Cryptography.SecureStore;
using System;
using System.Security.Cryptography;
using SecureFolderFS.Core.Cryptography.Cipher;
namespace SecureFolderFS.Core.Cryptography.NameCrypt
{
/// <inheritdoc cref="INameCrypt"/>
internal sealed class AesSivNameCrypt : BaseNameCrypt
{
private readonly AesSiv256 _aesSiv256;
public AesSivNameCrypt(KeyPair keyPair, string fileNameEncodingId)
: base(fileNameEncodingId)
{
_aesSiv256 = keyPair.UseKeys((dekKey, macKey) =>
{
// Note: AesSiv256 requires a byte[] key.
return AesSiv256.CreateInstance(dekKey.ToArray(), macKey.ToArray());
});
}
/// <inheritdoc/>
protected override byte[] EncryptFileName(ReadOnlySpan<byte> plaintextFileNameBuffer, ReadOnlySpan<byte> directoryId)
{
return _aesSiv256.Encrypt(plaintextFileNameBuffer, directoryId);
}
/// <inheritdoc/>
protected override byte[]? DecryptFileName(ReadOnlySpan<byte> ciphertextFileNameBuffer, ReadOnlySpan<byte> directoryId)
{
try
{
return _aesSiv256.Decrypt(ciphertextFileNameBuffer, directoryId);
}
catch (CryptographicException)
{
return null;
}
}
/// <inheritdoc/>
public override void Dispose()
{
_aesSiv256.Dispose();
}
}
}