Skip to content

Commit 60e5f68

Browse files
committed
Unify AbstractPathHelpers.Paths.cs
1 parent 7a7adbf commit 60e5f68

2 files changed

Lines changed: 40 additions & 21 deletions

File tree

‎src/Core/SecureFolderFS.Core.FileSystem/Helpers/Paths/Abstract/AbstractPathHelpers.Names.cs‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,47 @@ namespace SecureFolderFS.Core.FileSystem.Helpers.Paths.Abstract
88
{
99
public static partial class AbstractPathHelpers
1010
{
11+
/// <inheritdoc cref="EncryptNameAsync(string,OwlCore.Storage.IFolder,SecureFolderFS.Core.FileSystem.FileSystemSpecifics,System.Byte[],System.Threading.CancellationToken)"/>
12+
public static async Task<string> EncryptNameAsync(string plaintextName, IFolder ciphertextParentFolder,
13+
FileSystemSpecifics specifics, CancellationToken cancellationToken = default)
14+
{
15+
if (specifics.Security.NameCrypt is null)
16+
return plaintextName;
17+
18+
var directoryId = AllocateDirectoryId(specifics.Security, plaintextName);
19+
return await EncryptNameAsync(plaintextName, ciphertextParentFolder, specifics, directoryId, cancellationToken);
20+
}
21+
1122
/// <summary>
1223
/// Encrypts the provided <paramref name="plaintextName"/>.
1324
/// </summary>
1425
/// <param name="plaintextName">The name to encrypt.</param>
1526
/// <param name="ciphertextParentFolder">The ciphertext parent folder.</param>
1627
/// <param name="specifics">The <see cref="FileSystemSpecifics"/> instance associated with the item.</param>
28+
/// <param name="expendableDirectoryId">A buffer of size <see cref="Constants.DIRECTORY_ID_SIZE"/> which will be used to hold the Directory ID data.</param>
1729
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that cancels this action.</param>
1830
/// <returns>A <see cref="Task"/> that represents the asynchronous operation. Value is an encrypted name.</returns>
1931
public static async Task<string> EncryptNameAsync(string plaintextName, IFolder ciphertextParentFolder,
20-
FileSystemSpecifics specifics, CancellationToken cancellationToken = default)
32+
FileSystemSpecifics specifics, byte[]? expendableDirectoryId = null, CancellationToken cancellationToken = default)
2133
{
2234
if (specifics.Security.NameCrypt is null)
2335
return plaintextName;
2436

25-
var directoryId = AllocateDirectoryId(specifics.Security, plaintextName);
26-
var result = await GetDirectoryIdAsync(ciphertextParentFolder, specifics, directoryId, cancellationToken);
37+
expendableDirectoryId ??= AllocateDirectoryId(specifics.Security, plaintextName);
38+
var result = await GetDirectoryIdAsync(ciphertextParentFolder, specifics, expendableDirectoryId, cancellationToken);
39+
40+
return specifics.Security.NameCrypt.EncryptName(plaintextName, result ? expendableDirectoryId : ReadOnlySpan<byte>.Empty) + Constants.Names.ENCRYPTED_FILE_EXTENSION;
41+
}
42+
43+
/// <inheritdoc cref="DecryptNameAsync(string,OwlCore.Storage.IFolder,SecureFolderFS.Core.FileSystem.FileSystemSpecifics,System.Byte[],System.Threading.CancellationToken)"/>
44+
public static async Task<string?> DecryptNameAsync(string ciphertextName, IFolder ciphertextParentFolder,
45+
FileSystemSpecifics specifics, CancellationToken cancellationToken = default)
46+
{
47+
if (specifics.Security.NameCrypt is null)
48+
return ciphertextName;
2749

28-
return specifics.Security.NameCrypt.EncryptName(plaintextName, result ? directoryId : ReadOnlySpan<byte>.Empty) + Constants.Names.ENCRYPTED_FILE_EXTENSION;
50+
var directoryId = AllocateDirectoryId(specifics.Security, ciphertextName);
51+
return await DecryptNameAsync(ciphertextName, ciphertextParentFolder, specifics, directoryId, cancellationToken);
2952
}
3053

3154
/// <summary>
@@ -34,21 +57,22 @@ public static async Task<string> EncryptNameAsync(string plaintextName, IFolder
3457
/// <param name="ciphertextName">The name to decrypt.</param>
3558
/// <param name="ciphertextParentFolder">The ciphertext parent folder.</param>
3659
/// <param name="specifics">The <see cref="FileSystemSpecifics"/> instance associated with the item.</param>
60+
/// <param name="expendableDirectoryId">A buffer of size <see cref="Constants.DIRECTORY_ID_SIZE"/> which will be used to hold the Directory ID data.</param>
3761
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that cancels this action.</param>
3862
/// <returns>A <see cref="Task"/> that represents the asynchronous operation. Value is a decrypted name.</returns>
3963
public static async Task<string?> DecryptNameAsync(string ciphertextName, IFolder ciphertextParentFolder,
40-
FileSystemSpecifics specifics, CancellationToken cancellationToken = default)
64+
FileSystemSpecifics specifics, byte[]? expendableDirectoryId, CancellationToken cancellationToken = default)
4165
{
66+
if (specifics.Security.NameCrypt is null)
67+
return ciphertextName;
68+
4269
try
4370
{
44-
if (specifics.Security.NameCrypt is null)
45-
return ciphertextName;
46-
47-
var directoryId = AllocateDirectoryId(specifics.Security, ciphertextName);
48-
var result = await GetDirectoryIdAsync(ciphertextParentFolder, specifics, directoryId, cancellationToken);
71+
expendableDirectoryId ??= AllocateDirectoryId(specifics.Security, ciphertextName);
72+
var result = await GetDirectoryIdAsync(ciphertextParentFolder, specifics, expendableDirectoryId, cancellationToken);
4973

5074
var normalizedName = RemoveCiphertextExtension(ciphertextName);
51-
return specifics.Security.NameCrypt.DecryptName(normalizedName, result ? directoryId : ReadOnlySpan<byte>.Empty);
75+
return specifics.Security.NameCrypt.DecryptName(normalizedName, result ? expendableDirectoryId : ReadOnlySpan<byte>.Empty);
5276
}
5377
catch (Exception)
5478
{

‎src/Core/SecureFolderFS.Core.FileSystem/Helpers/Paths/Abstract/AbstractPathHelpers.Paths.cs‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,13 @@ public static partial class AbstractPathHelpers
5656
foreach (var item in folderChain)
5757
{
5858
// Walk through plaintext folder chain and retrieve ciphertext folders
59-
var subResult = await GetDirectoryIdAsync(finalFolder, specifics, expendableDirectoryId, cancellationToken).ConfigureAwait(false);
60-
var subCiphertextName = specifics.Security.NameCrypt.EncryptName(item.Name, subResult ? expendableDirectoryId : ReadOnlySpan<byte>.Empty);
61-
62-
finalFolder = await finalFolder.GetFolderByNameAsync($"{subCiphertextName}{Constants.Names.ENCRYPTED_FILE_EXTENSION}", cancellationToken);
59+
var subCiphertextName = await EncryptNameAsync(item.Name, finalFolder, specifics, expendableDirectoryId, cancellationToken).ConfigureAwait(false);
60+
finalFolder = await finalFolder.GetFolderByNameAsync(subCiphertextName, cancellationToken);
6361
}
6462

6563
// Encrypt and retrieve the final item
66-
var result = await GetDirectoryIdAsync(finalFolder, specifics, expendableDirectoryId, cancellationToken).ConfigureAwait(false);
67-
var ciphertextName = specifics.Security.NameCrypt.EncryptName(plaintextStorable.Name, result ? expendableDirectoryId : ReadOnlySpan<byte>.Empty);
68-
69-
return await finalFolder.GetFirstByNameAsync($"{ciphertextName}{Constants.Names.ENCRYPTED_FILE_EXTENSION}", cancellationToken);
64+
var ciphertextName = await EncryptNameAsync(plaintextStorable.Name, finalFolder, specifics, expendableDirectoryId, cancellationToken).ConfigureAwait(false);
65+
return await finalFolder.GetFirstByNameAsync(ciphertextName, cancellationToken);
7066
}
7167

7268
public static async Task<string?> GetPlaintextPathAsync(IStorableChild ciphertextStorable, FileSystemSpecifics specifics, CancellationToken cancellationToken)
@@ -83,8 +79,7 @@ public static partial class AbstractPathHelpers
8379
if (!currentParent.Id.Contains(specifics.ContentFolder.Id))
8480
break;
8581

86-
var result = await GetDirectoryIdAsync(currentParent, specifics, expendableDirectoryId, cancellationToken).ConfigureAwait(false);
87-
var plaintextName = specifics.Security.NameCrypt.DecryptName(Path.GetFileNameWithoutExtension(currentStorable.Name), result ? expendableDirectoryId : ReadOnlySpan<byte>.Empty);
82+
var plaintextName = await DecryptNameAsync(currentStorable.Name, currentParent, specifics, expendableDirectoryId, cancellationToken).ConfigureAwait(false);
8883
if (plaintextName is null)
8984
return null;
9085

0 commit comments

Comments
 (0)