Skip to content

Commit 7a0411f

Browse files
feat: enable nullable context in the C# wrapper (#362)
Adds the annotations for nullable reference types.
1 parent 82403c4 commit 7a0411f

19 files changed

Lines changed: 309 additions & 388 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1010

1111
- Multiple functions, such as `generate_key` and `hash_password`, now return a `Result` due to the `rand` library upgrade.
1212

13+
## Added
14+
15+
- C#: Nullable annotations were added for nullable reference types.
16+
1317
### Removed
1418

15-
- Removed `EncryptWithKeyAsString`, `EncryptWithPasswordAsString`, `DecryptWithKeyAsString`, `DecryptWithPasswordAsString`,
19+
- C#: Removed `EncryptWithKeyAsString`, `EncryptWithPasswordAsString`, `DecryptWithKeyAsString`, `DecryptWithPasswordAsString`,
1620
`GenerateAPIKey`.
1721

1822
## [0.9.2] - 2025-01-20

‎wrappers/csharp/src/Argon2Parameters.cs‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ internal Argon2Parameters(bool defaultParameters = true)
3131
/// <summary>
3232
/// Gets the size of the raw Argon2 data.
3333
/// </summary>
34-
public static long NativeSize
35-
{
36-
get
37-
{
38-
return Native.GetDefaultArgon2ParametersSizeNative();
39-
}
40-
}
34+
public static long NativeSize => Native.GetDefaultArgon2ParametersSizeNative();
4135

4236
/// <summary>
4337
/// Gets or sets the number of iterations over the memory.
@@ -62,7 +56,7 @@ public static long NativeSize
6256
/// <summary>
6357
/// Gets or sets the associated data.
6458
/// </summary>
65-
internal byte[] AssociatedData { get; set; }
59+
internal byte[]? AssociatedData { get; set; }
6660

6761
/// <summary>
6862
/// Gets or sets the devolutions crypto version.
@@ -72,7 +66,7 @@ public static long NativeSize
7266
/// <summary>
7367
/// Gets or sets the salt used by the algorithm.
7468
/// </summary>
75-
internal byte[] Salt { get; set; }
69+
internal byte[]? Salt { get; set; }
7670

7771
/// <summary>
7872
/// Gets or sets the Argon2 variant used by the algorithm.
@@ -89,7 +83,7 @@ public static long NativeSize
8983
/// </summary>
9084
/// <param name="data">The data to deserialize.</param>
9185
/// <returns>Returns the deserialized parameters.</returns>
92-
public static Argon2Parameters FromByteArray(byte[] data)
86+
public static Argon2Parameters? FromByteArray(byte[]? data)
9387
{
9488
if (data == null)
9589
{
@@ -281,7 +275,7 @@ public byte[] ToByteArray()
281275
// === Salt Length ===
282276
if (this.Salt == null)
283277
{
284-
this.Salt = Array.Empty<byte>();
278+
this.Salt = [];
285279
}
286280

287281
byte[] saltLength = BitConverter.GetBytes(this.Salt.Length);

‎wrappers/csharp/src/DevolutionsCryptoException.cs‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class DevolutionsCryptoException : Exception
1313
/// <param name="managedError">The managed error code.</param>
1414
/// <param name="message">The exception message. (Optional).</param>
1515
/// <param name="exception">The managed exception. (Optional).</param>
16-
public DevolutionsCryptoException(ManagedError managedError, string message = null, Exception exception = null) : base(message)
16+
public DevolutionsCryptoException(ManagedError managedError, string? message = null, Exception? exception = null) : base(message)
1717
{
1818
this.ManagedError = managedError;
1919
this.ManagedException = exception;
@@ -25,7 +25,7 @@ public DevolutionsCryptoException(ManagedError managedError, string message = nu
2525
/// <param name="nativeError">The native error code.</param>
2626
/// <param name="message">The exception message. (Optional).</param>
2727
/// <param name="exception">The managed exception. (Optional).</param>
28-
public DevolutionsCryptoException(NativeError nativeError, string message = null, Exception exception = null) : base(message)
28+
public DevolutionsCryptoException(NativeError nativeError, string? message = null, Exception? exception = null) : base(message)
2929
{
3030
this.NativeError = nativeError;
3131
this.ManagedException = exception;
@@ -39,7 +39,7 @@ public DevolutionsCryptoException(NativeError nativeError, string message = null
3939
/// <summary>
4040
/// Gets or sets if an unknown exception happens this property will contain it..
4141
/// </summary>
42-
public Exception ManagedException { get; set; }
42+
public Exception? ManagedException { get; set; }
4343

4444
/// <summary>
4545
/// Gets override to add additionnal info in the exception message.
@@ -81,21 +81,15 @@ public string GetDetailedMessage()
8181
result = "NativeError :\r\n";
8282
result = result + this.NativeError.Value.ToString() + "\r\n";
8383

84-
if (this.Data != null)
85-
{
86-
this.Data["NativeError"] = this.NativeError.Value.ToString();
87-
}
84+
this.Data["NativeError"] = this.NativeError.Value.ToString();
8885
}
8986

9087
if (this.ManagedError != null)
9188
{
9289
result = "ManagedError : \r\n";
9390
result = result + this.ManagedError.Value.ToString() + "\r\n";
9491

95-
if (this.Data != null)
96-
{
97-
this.Data["ManagedError"] = this.ManagedError.Value.ToString();
98-
}
92+
this.Data["ManagedError"] = this.ManagedError.Value.ToString();
9993

10094
if (this.ManagedException != null)
10195
{

‎wrappers/csharp/src/EncryptionStream.cs‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@ public class EncryptionStream
77
: Stream, IDisposable
88
{
99
private UIntPtr _native_ptr = UIntPtr.Zero;
10-
private readonly int _chunkLength;
11-
private readonly int _tagLength;
1210
private bool _finalBlockTransformed = false;
1311
private readonly bool _leaveOpen;
1412

15-
public int ChunkLength { get { return _chunkLength; } }
13+
public int ChunkLength { get; }
1614

17-
public int TagLength { get { return _tagLength; } }
15+
public int TagLength { get; }
1816

19-
public bool HasFlushedFinalBlock
20-
{
21-
get { return _finalBlockTransformed; }
22-
}
17+
public bool HasFlushedFinalBlock => _finalBlockTransformed;
2318

2419
private readonly byte[] _inputBuffer;
2520

@@ -33,7 +28,7 @@ public EncryptionStream(byte[] key, byte[] aad, int chunkLength, bool asymmetric
3328

3429
public EncryptionStream(byte[] key, byte[] aad, int chunkLength, bool asymmetric, int version, Stream outputStream, bool leaveOpen)
3530
{
36-
_chunkLength = chunkLength;
31+
ChunkLength = chunkLength;
3732
_outputStream = outputStream;
3833
_leaveOpen = leaveOpen;
3934

@@ -51,7 +46,7 @@ public EncryptionStream(byte[] key, byte[] aad, int chunkLength, bool asymmetric
5146
Utils.HandleError(tagSize);
5247
}
5348

54-
_tagLength = (int)tagSize;
49+
TagLength = (int)tagSize;
5550

5651
_inputBuffer = new byte[chunkLength];
5752
}
@@ -108,7 +103,6 @@ public void FlushFinalBlock()
108103

109104
public override void Flush()
110105
{
111-
return;
112106
}
113107

114108
public override int Read(byte[] buffer, int offset, int count)

‎wrappers/csharp/src/Enums.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public enum ManagedError
110110

111111
/// <summary>
112112
/// Error when the native library cannot be loaded
113-
/// </summery>
113+
/// </summary>
114114
NativeLibraryLoad,
115115

116116
/// <summary>

‎wrappers/csharp/src/KeyPair.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public class KeyPair
1010
/// <summary>
1111
/// Gets or sets the private key.
1212
/// </summary>
13-
public byte[] PrivateKey { get; set; }
13+
public byte[]? PrivateKey { get; set; }
1414

1515
/// <summary>
1616
/// Gets the private key as base 64 string.
1717
/// </summary>
18-
public string PrivateKeyString
18+
public string? PrivateKeyString
1919
{
2020
get
2121
{
@@ -31,12 +31,12 @@ public string PrivateKeyString
3131
/// <summary>
3232
/// Gets or sets the public key.
3333
/// </summary>
34-
public byte[] PublicKey { get; set; }
34+
public byte[]? PublicKey { get; set; }
3535

3636
/// <summary>
3737
/// Gets the public key key as base 64 string.
3838
/// </summary>
39-
public string PublicKeyString
39+
public string? PublicKeyString
4040
{
4141
get
4242
{

0 commit comments

Comments
 (0)