Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new method #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/.idea/.idea.Microsoft.Security.Utilities/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/.idea/.idea.Microsoft.Security.Utilities/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/.idea/.idea.Microsoft.Security.Utilities/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/Microsoft.Security.Utilities/CustomAlphabetEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,41 @@ public byte[] Decode(string encodedValue)

return BitConverter.GetBytes(decodedValue);
}

/// <summary>
/// Sets a custom alphabet to be used for all encoding and decoding operations.
/// </summary>
/// <param name="customAlphabet">A string representing the custom alphabet. It must consist of non-whitespace ASCII characters, including letters, numbers, and/or punctuation.</param>
/// <exception cref="ArgumentException">Throws an exception if the alphabet contains duplicates or contains forbidden characters.</exception>
/// <remarks>
/// This method allows dynamic changes to the alphabet used for encoding and decoding data. Before setting a new alphabet, the method performs checks for duplicates and the presence of forbidden characters.
/// </remarks>
public void SetCustomAlphabet(string customAlphabet)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SetCustomAlphabet

Sorry for not noticing this PR earlier! This looks like a good idea. It'd be best to share the argument validated logic between this method and the constructor via a shared helper.

{
if (customAlphabet == null)
{
throw new ArgumentNullException(nameof(customAlphabet));
}

Dictionary<char, uint> newCharToValueMap = new Dictionary<char, uint>();

for (int i = 0; i < customAlphabet.Length; i++)
{
if (newCharToValueMap.ContainsKey(customAlphabet[i]))
{
throw new ArgumentException(nameof(customAlphabet), "Duplicate value detected in the new alphabet.");
}

if (Char.IsWhiteSpace(customAlphabet[i]) || Char.IsSurrogate(customAlphabet[i]) || (int)customAlphabet[i] > 127)
{
throw new ArgumentException(nameof(customAlphabet), $"Forbidden character type detected in the new alphabet: {customAlphabet[i]}.");
}

newCharToValueMap[customAlphabet[i]] = (uint)i;
}

alphabet = customAlphabet;
charToValueMap = newCharToValueMap;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>C:\Users\User\RiderProjects\Fork\bld\bin\AnyCPU_Debug\Microsoft.Security.Utilities\Microsoft.Security.Utilities.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ public void CustomAlphabetEncoder_VerifyStaticAndInstanceDate()

encodedChecksum1.Should().Be(encodedChecksum2);
}

[TestMethod]
public void CustomAlphabetEncoder_SetCustomAlphabetShouldChangeAlphabetAndWorkCorrectly()
{
var testEncoder = new CustomAlphabetEncoder("123");
uint input = 42;
string originalEncoded = testEncoder.Encode(input);

testEncoder.SetCustomAlphabet("ABC");

string newEncoded = testEncoder.Encode(input);
byte[] newDecoded = testEncoder.Decode(newEncoded);
uint newDecodedUint = BitConverter.ToUInt32(newDecoded, 0);

newEncoded.Should().NotBe(originalEncoded);
newDecodedUint.Should().Be(input);
}

private IEnumerable<string> GenerateValidAlphabetTestCases()
{
Expand Down