Skip to content

Commit

Permalink
Throw proper errors in PasswordBasedKeyExporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ektrah committed May 4, 2024
1 parent 665e72a commit 7a3273f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/Cryptography/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ internal static ArgumentNullException ArgumentNull_Password(
return new ArgumentNullException(paramName, ResourceManager.GetString(nameof(ArgumentNull_Password)));
}

internal static ArgumentNullException ArgumentNull_Scheme(
string paramName)
{
return new ArgumentNullException(paramName, ResourceManager.GetString(nameof(ArgumentNull_Scheme)));
}

internal static ArgumentNullException ArgumentNull_SharedSecret(
string paramName)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Cryptography/Error.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
<data name="ArgumentNull_Password" xml:space="preserve">
<value>Password cannot be null.</value>
</data>
<data name="ArgumentNull_Scheme" xml:space="preserve">
<value>Encryption scheme cannot be null.</value>
</data>
<data name="ArgumentNull_SharedSecret" xml:space="preserve">
<value>Shared secret cannot be null.</value>
</data>
Expand Down
16 changes: 8 additions & 8 deletions src/Experimental/PasswordBased/PasswordBasedKeyExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static byte[] Export(
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
throw Error.ArgumentNull_Key(nameof(key));
}

KeyBlobFormat format = SelectKeyBlobFormat(key.Algorithm);
Expand All @@ -31,7 +31,7 @@ public static byte[] Export(
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
throw Error.ArgumentNull_Key(nameof(key));
}

KeyBlobFormat format = SelectKeyBlobFormat(key.Algorithm);
Expand All @@ -48,7 +48,7 @@ public static Key Import(
{
if (algorithm == null)
{
throw new ArgumentNullException(nameof(algorithm));
throw Error.ArgumentNull_Algorithm(nameof(algorithm));
}

KeyBlobFormat format = SelectKeyBlobFormat(algorithm);
Expand All @@ -65,7 +65,7 @@ public static Key Import(
{
if (algorithm == null)
{
throw new ArgumentNullException(nameof(algorithm));
throw Error.ArgumentNull_Algorithm(nameof(algorithm));
}

KeyBlobFormat format = SelectKeyBlobFormat(algorithm);
Expand All @@ -80,7 +80,7 @@ public static Key Import(
{
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
throw Error.ArgumentNull_Scheme(nameof(scheme));
}

Reader reader = new(blob);
Expand All @@ -96,7 +96,7 @@ public static Key Import(
{
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
throw Error.ArgumentNull_Scheme(nameof(scheme));
}

Reader reader = new(blob);
Expand All @@ -114,7 +114,7 @@ internal static byte[] Encrypt(
{
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
throw Error.ArgumentNull_Scheme(nameof(scheme));
}

byte[] ciphertext = scheme.Encrypt(password, salt, nonce, plaintext);
Expand All @@ -133,7 +133,7 @@ internal static byte[] Encrypt(
{
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
throw Error.ArgumentNull_Scheme(nameof(scheme));
}

byte[] ciphertext = scheme.Encrypt(password, salt, nonce, plaintext);
Expand Down

0 comments on commit 7a3273f

Please sign in to comment.