diff --git a/perf/MicroBenchmarks/Core/EncryptedStringAttributeTests.cs b/perf/MicroBenchmarks/Core/EncryptedStringAttributeTests.cs index 8b8f194f36..b7c397dfe5 100644 --- a/perf/MicroBenchmarks/Core/EncryptedStringAttributeTests.cs +++ b/perf/MicroBenchmarks/Core/EncryptedStringAttributeTests.cs @@ -1,5 +1,5 @@ +using BenchmarkDotNet.Attributes; using Bit.Core.Utilities; -using BenchmarkDotNet.Attributes; namespace Bit.MicroBenchmarks.Core; diff --git a/src/Core/Utilities/EncryptedStringAttribute.cs b/src/Core/Utilities/EncryptedStringAttribute.cs index 0f672d1bdb..841ab3be62 100644 --- a/src/Core/Utilities/EncryptedStringAttribute.cs +++ b/src/Core/Utilities/EncryptedStringAttribute.cs @@ -18,18 +18,18 @@ public class EncryptedStringAttribute : ValidationAttribute { _encryptionTypeMap = new() { - [EncryptionType.AesCbc256_B64] = 2, - [EncryptionType.AesCbc128_HmacSha256_B64] = 3, - [EncryptionType.AesCbc256_HmacSha256_B64] = 3, - [EncryptionType.Rsa2048_OaepSha256_B64] = 1, - [EncryptionType.Rsa2048_OaepSha1_B64] = 1, - [EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64] = 2, - [EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64] = 2, + [EncryptionType.AesCbc256_B64] = 2, // iv|ct + [EncryptionType.AesCbc128_HmacSha256_B64] = 3, // iv|ct|mac + [EncryptionType.AesCbc256_HmacSha256_B64] = 3, // iv|ct|mac + [EncryptionType.Rsa2048_OaepSha256_B64] = 1, // rsaCt + [EncryptionType.Rsa2048_OaepSha1_B64] = 1, // rsaCt + [EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64] = 2, // rsaCt|mac + [EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64] = 2, // rsaCt|mac }; #if DEBUG var enumValues = Enum.GetValues(); - Debug.Assert(enumValues.Length == _encryptionTypeMap.Count, + Debug.Assert(enumValues.Length == _encryptionTypeMap.Count, $"New {nameof(EncryptionType)} enums should be added to the {nameof(_encryptionTypeMap)}"); #endif } @@ -75,7 +75,7 @@ public class EncryptedStringAttribute : ValidationAttribute var pieces = 1; var findIndex = encryptionPiecesChunk.IndexOf('|'); - while(findIndex != -1) + while (findIndex != -1) { pieces++; encryptionPiecesChunk = encryptionPiecesChunk[++findIndex..]; @@ -131,11 +131,13 @@ public class EncryptedStringAttribute : ValidationAttribute { if (requiredPieces == 1) { + // Only one more part is needed so don't split and check the chunk if (!IsValidBase64(rest)) { return false; } + // Make sure there isn't another split character possibly denoting another chunk return rest.IndexOf('|') == -1; } else @@ -153,6 +155,7 @@ public class EncryptedStringAttribute : ValidationAttribute } } + // This current piece is valid so we can count down requiredPieces--; } @@ -176,6 +179,7 @@ public class EncryptedStringAttribute : ValidationAttribute } finally { + // Check if we rented the pool and if so, return it. if (pooledChunks != null) { ArrayPool.Shared.Return(pooledChunks); diff --git a/src/Core/Utilities/SpanExtensions.cs b/src/Core/Utilities/SpanExtensions.cs index ee73ac87eb..cc17a51568 100644 --- a/src/Core/Utilities/SpanExtensions.cs +++ b/src/Core/Utilities/SpanExtensions.cs @@ -1,8 +1,8 @@ -namespace Bit.Core.Utilities; +namespace Bit.Core.Utilities; public static class SpanExtensions { - public static bool TrySplitBy(this ReadOnlySpan input, + public static bool TrySplitBy(this ReadOnlySpan input, char splitChar, out ReadOnlySpan chunk, out ReadOnlySpan rest) { var splitIndex = input.IndexOf(splitChar);