|
2 | 2 | using System.Threading; |
3 | 3 | using System.Threading.Tasks; |
4 | 4 | using OwlCore.Storage; |
| 5 | +using SecureFolderFS.Core.Cryptography; |
5 | 6 | using SecureFolderFS.Core.FileSystem.FileNames; |
6 | 7 |
|
7 | 8 | namespace SecureFolderFS.Core.FileSystem.Helpers.Paths.Abstract |
@@ -40,6 +41,36 @@ public static async Task<string> EncryptNameAsync(string plaintextName, IFolder |
40 | 41 | return specifics.Security.NameCrypt.EncryptName(plaintextName, result ? expendableDirectoryId : ReadOnlySpan<byte>.Empty) + Constants.Names.ENCRYPTED_FILE_EXTENSION; |
41 | 42 | } |
42 | 43 |
|
| 44 | + /// <summary> |
| 45 | + /// Encrypts the provided <paramref name="plaintextName"/>. |
| 46 | + /// </summary> |
| 47 | + /// <param name="plaintextName">The name to encrypt.</param> |
| 48 | + /// <param name="ciphertextParentFolder">The ciphertext parent folder.</param> |
| 49 | + /// <param name="contentFolder">The content folder.</param> |
| 50 | + /// <param name="security">The <see cref="Security"/> instance associated with the item.</param> |
| 51 | + /// <param name="cancellationToken">A <see cref="CancellationToken"/> that cancels this action.</param> |
| 52 | + /// <returns>A <see cref="Task"/> that represents the asynchronous operation. Value is an encrypted name.</returns> |
| 53 | + public static async Task<string> EncryptNameAsync(string plaintextName, IFolder ciphertextParentFolder, IFolder contentFolder, |
| 54 | + Security security, CancellationToken cancellationToken = default) |
| 55 | + { |
| 56 | + if (security.NameCrypt is null) |
| 57 | + return plaintextName; |
| 58 | + |
| 59 | + var directoryId = AllocateDirectoryId(security, plaintextName); |
| 60 | + var result = await GetDirectoryIdAsync(ciphertextParentFolder, contentFolder, directoryId, cancellationToken); |
| 61 | + |
| 62 | + return security.NameCrypt.EncryptName(plaintextName, result ? directoryId : ReadOnlySpan<byte>.Empty) + Constants.Names.ENCRYPTED_FILE_EXTENSION; |
| 63 | + } |
| 64 | + |
| 65 | + public static async Task<string> EncryptNewNameAsync(string plaintextName, byte[] newDirectoryId, |
| 66 | + Security security, CancellationToken cancellationToken = default) |
| 67 | + { |
| 68 | + if (security.NameCrypt is null) |
| 69 | + return plaintextName; |
| 70 | + |
| 71 | + return security.NameCrypt.EncryptName(plaintextName, newDirectoryId) + Constants.Names.ENCRYPTED_FILE_EXTENSION; |
| 72 | + } |
| 73 | + |
43 | 74 | /// <inheritdoc cref="DecryptNameAsync(string,OwlCore.Storage.IFolder,SecureFolderFS.Core.FileSystem.FileSystemSpecifics,System.Byte[],System.Threading.CancellationToken)"/> |
44 | 75 | public static async Task<string?> DecryptNameAsync(string ciphertextName, IFolder ciphertextParentFolder, |
45 | 76 | FileSystemSpecifics specifics, CancellationToken cancellationToken = default) |
|
0 commit comments