|
1 | 1 | using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
2 | 3 | using SecureFolderFS.Core.Cryptography.ContentCrypt; |
3 | 4 | using SecureFolderFS.Core.Cryptography.HeaderCrypt; |
4 | 5 | using SecureFolderFS.Core.Cryptography.NameCrypt; |
@@ -48,35 +49,50 @@ private Security(KeyPair keyPair) |
48 | 49 | public static Security CreateNew(KeyPair keyPair, string contentCipherId, string fileNameCipherId, string fileNameEncodingId) |
49 | 50 | { |
50 | 51 | // Initialize crypt implementation |
51 | | - IHeaderCrypt headerCrypt = contentCipherId switch |
| 52 | + var headerCrypt = GetHeaderCrypt(keyPair, contentCipherId); |
| 53 | + var contentCrypt = GetContentCrypt(contentCipherId, keyPair); |
| 54 | + var nameCrypt = GetNameCrypt(keyPair, fileNameCipherId, fileNameEncodingId); |
| 55 | + |
| 56 | + return new(keyPair) |
| 57 | + { |
| 58 | + ContentCrypt = contentCrypt, |
| 59 | + HeaderCrypt = headerCrypt, |
| 60 | + NameCrypt = nameCrypt |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + public static IHeaderCrypt GetHeaderCrypt(KeyPair keyPair, string contentCipherId) |
| 65 | + { |
| 66 | + return contentCipherId switch |
52 | 67 | { |
53 | 68 | CipherId.AES_CTR_HMAC => new AesCtrHmacHeaderCrypt(keyPair), |
54 | 69 | CipherId.AES_GCM => new AesGcmHeaderCrypt(keyPair), |
55 | 70 | CipherId.XCHACHA20_POLY1305 => new XChaChaHeaderCrypt(keyPair), |
56 | 71 | CipherId.NONE => new NoHeaderCrypt(keyPair), |
57 | 72 | _ => throw new ArgumentOutOfRangeException(nameof(contentCipherId)) |
58 | 73 | }; |
59 | | - IContentCrypt contentCrypt = contentCipherId switch |
| 74 | + } |
| 75 | + |
| 76 | + public static IContentCrypt GetContentCrypt(string contentCipherId, KeyPair keyPair) |
| 77 | + { |
| 78 | + return contentCipherId switch |
60 | 79 | { |
61 | 80 | CipherId.AES_CTR_HMAC => new AesCtrHmacContentCrypt(keyPair.MacKey), |
62 | 81 | CipherId.AES_GCM => new AesGcmContentCrypt(), |
63 | 82 | CipherId.XCHACHA20_POLY1305 => new XChaChaContentCrypt(), |
64 | 83 | CipherId.NONE => new NoContentCrypt(), |
65 | 84 | _ => throw new ArgumentOutOfRangeException(nameof(contentCipherId)) |
66 | 85 | }; |
67 | | - INameCrypt? nameCrypt = fileNameCipherId switch |
| 86 | + } |
| 87 | + |
| 88 | + public static INameCrypt? GetNameCrypt(KeyPair keyPair, string fileNameCipherId, string fileNameEncodingId) |
| 89 | + { |
| 90 | + return fileNameCipherId switch |
68 | 91 | { |
69 | 92 | CipherId.AES_SIV => new AesSivNameCrypt(keyPair, fileNameEncodingId), |
70 | 93 | CipherId.NONE => null, |
71 | 94 | _ => throw new ArgumentOutOfRangeException(nameof(fileNameCipherId)) |
72 | 95 | }; |
73 | | - |
74 | | - return new(keyPair) |
75 | | - { |
76 | | - ContentCrypt = contentCrypt, |
77 | | - HeaderCrypt = headerCrypt, |
78 | | - NameCrypt = nameCrypt |
79 | | - }; |
80 | 96 | } |
81 | 97 |
|
82 | 98 | /// <inheritdoc/> |
|
0 commit comments