diff --git a/CHANGELOG.md b/CHANGELOG.md index 43a8ad15..7b255ebe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Multiple functions, such as `generate_key` and `hash_password`, now return a `Result` due to the `rand` library upgrade. +## Added + +- C#: Nullable annotations were added for nullable reference types. + ### Removed -- Removed `EncryptWithKeyAsString`, `EncryptWithPasswordAsString`, `DecryptWithKeyAsString`, `DecryptWithPasswordAsString`, +- C#: Removed `EncryptWithKeyAsString`, `EncryptWithPasswordAsString`, `DecryptWithKeyAsString`, `DecryptWithPasswordAsString`, `GenerateAPIKey`. ## [0.9.2] - 2025-01-20 diff --git a/wrappers/csharp/src/Argon2Parameters.cs b/wrappers/csharp/src/Argon2Parameters.cs index b318e537..4e9fd347 100644 --- a/wrappers/csharp/src/Argon2Parameters.cs +++ b/wrappers/csharp/src/Argon2Parameters.cs @@ -31,13 +31,7 @@ internal Argon2Parameters(bool defaultParameters = true) /// /// Gets the size of the raw Argon2 data. /// - public static long NativeSize - { - get - { - return Native.GetDefaultArgon2ParametersSizeNative(); - } - } + public static long NativeSize => Native.GetDefaultArgon2ParametersSizeNative(); /// /// Gets or sets the number of iterations over the memory. @@ -62,7 +56,7 @@ public static long NativeSize /// /// Gets or sets the associated data. /// - internal byte[] AssociatedData { get; set; } + internal byte[]? AssociatedData { get; set; } /// /// Gets or sets the devolutions crypto version. @@ -72,7 +66,7 @@ public static long NativeSize /// /// Gets or sets the salt used by the algorithm. /// - internal byte[] Salt { get; set; } + internal byte[]? Salt { get; set; } /// /// Gets or sets the Argon2 variant used by the algorithm. @@ -89,7 +83,7 @@ public static long NativeSize /// /// The data to deserialize. /// Returns the deserialized parameters. - public static Argon2Parameters FromByteArray(byte[] data) + public static Argon2Parameters? FromByteArray(byte[]? data) { if (data == null) { @@ -281,7 +275,7 @@ public byte[] ToByteArray() // === Salt Length === if (this.Salt == null) { - this.Salt = Array.Empty(); + this.Salt = []; } byte[] saltLength = BitConverter.GetBytes(this.Salt.Length); diff --git a/wrappers/csharp/src/DevolutionsCryptoException.cs b/wrappers/csharp/src/DevolutionsCryptoException.cs index 865760b7..c35e708d 100644 --- a/wrappers/csharp/src/DevolutionsCryptoException.cs +++ b/wrappers/csharp/src/DevolutionsCryptoException.cs @@ -13,7 +13,7 @@ public class DevolutionsCryptoException : Exception /// The managed error code. /// The exception message. (Optional). /// The managed exception. (Optional). - public DevolutionsCryptoException(ManagedError managedError, string message = null, Exception exception = null) : base(message) + public DevolutionsCryptoException(ManagedError managedError, string? message = null, Exception? exception = null) : base(message) { this.ManagedError = managedError; this.ManagedException = exception; @@ -25,7 +25,7 @@ public DevolutionsCryptoException(ManagedError managedError, string message = nu /// The native error code. /// The exception message. (Optional). /// The managed exception. (Optional). - public DevolutionsCryptoException(NativeError nativeError, string message = null, Exception exception = null) : base(message) + public DevolutionsCryptoException(NativeError nativeError, string? message = null, Exception? exception = null) : base(message) { this.NativeError = nativeError; this.ManagedException = exception; @@ -39,7 +39,7 @@ public DevolutionsCryptoException(NativeError nativeError, string message = null /// /// Gets or sets if an unknown exception happens this property will contain it.. /// - public Exception ManagedException { get; set; } + public Exception? ManagedException { get; set; } /// /// Gets override to add additionnal info in the exception message. @@ -81,10 +81,7 @@ public string GetDetailedMessage() result = "NativeError :\r\n"; result = result + this.NativeError.Value.ToString() + "\r\n"; - if (this.Data != null) - { - this.Data["NativeError"] = this.NativeError.Value.ToString(); - } + this.Data["NativeError"] = this.NativeError.Value.ToString(); } if (this.ManagedError != null) @@ -92,10 +89,7 @@ public string GetDetailedMessage() result = "ManagedError : \r\n"; result = result + this.ManagedError.Value.ToString() + "\r\n"; - if (this.Data != null) - { - this.Data["ManagedError"] = this.ManagedError.Value.ToString(); - } + this.Data["ManagedError"] = this.ManagedError.Value.ToString(); if (this.ManagedException != null) { diff --git a/wrappers/csharp/src/EncryptionStream.cs b/wrappers/csharp/src/EncryptionStream.cs index d834a82d..bce2968a 100644 --- a/wrappers/csharp/src/EncryptionStream.cs +++ b/wrappers/csharp/src/EncryptionStream.cs @@ -7,19 +7,14 @@ public class EncryptionStream : Stream, IDisposable { private UIntPtr _native_ptr = UIntPtr.Zero; - private readonly int _chunkLength; - private readonly int _tagLength; private bool _finalBlockTransformed = false; private readonly bool _leaveOpen; - public int ChunkLength { get { return _chunkLength; } } + public int ChunkLength { get; } - public int TagLength { get { return _tagLength; } } + public int TagLength { get; } - public bool HasFlushedFinalBlock - { - get { return _finalBlockTransformed; } - } + public bool HasFlushedFinalBlock => _finalBlockTransformed; private readonly byte[] _inputBuffer; @@ -33,7 +28,7 @@ public EncryptionStream(byte[] key, byte[] aad, int chunkLength, bool asymmetric public EncryptionStream(byte[] key, byte[] aad, int chunkLength, bool asymmetric, int version, Stream outputStream, bool leaveOpen) { - _chunkLength = chunkLength; + ChunkLength = chunkLength; _outputStream = outputStream; _leaveOpen = leaveOpen; @@ -51,7 +46,7 @@ public EncryptionStream(byte[] key, byte[] aad, int chunkLength, bool asymmetric Utils.HandleError(tagSize); } - _tagLength = (int)tagSize; + TagLength = (int)tagSize; _inputBuffer = new byte[chunkLength]; } @@ -108,7 +103,6 @@ public void FlushFinalBlock() public override void Flush() { - return; } public override int Read(byte[] buffer, int offset, int count) diff --git a/wrappers/csharp/src/Enums.cs b/wrappers/csharp/src/Enums.cs index e37d54c2..934dd2c3 100644 --- a/wrappers/csharp/src/Enums.cs +++ b/wrappers/csharp/src/Enums.cs @@ -110,7 +110,7 @@ public enum ManagedError /// /// Error when the native library cannot be loaded - /// + /// NativeLibraryLoad, /// diff --git a/wrappers/csharp/src/KeyPair.cs b/wrappers/csharp/src/KeyPair.cs index 402fd9a7..0aecbc9e 100644 --- a/wrappers/csharp/src/KeyPair.cs +++ b/wrappers/csharp/src/KeyPair.cs @@ -10,12 +10,12 @@ public class KeyPair /// /// Gets or sets the private key. /// - public byte[] PrivateKey { get; set; } + public byte[]? PrivateKey { get; set; } /// /// Gets the private key as base 64 string. /// - public string PrivateKeyString + public string? PrivateKeyString { get { @@ -31,12 +31,12 @@ public string PrivateKeyString /// /// Gets or sets the public key. /// - public byte[] PublicKey { get; set; } + public byte[]? PublicKey { get; set; } /// /// Gets the public key key as base 64 string. /// - public string PublicKeyString + public string? PublicKeyString { get { diff --git a/wrappers/csharp/src/Managed.cs b/wrappers/csharp/src/Managed.cs index 9477c910..b3abb26b 100644 --- a/wrappers/csharp/src/Managed.cs +++ b/wrappers/csharp/src/Managed.cs @@ -35,7 +35,7 @@ public static class Managed /// The private key to mix. /// The public key. /// Returns the resulting shared key. - public static byte[] MixKeyExchange(byte[] privateKey, byte[] publicKey) + public static byte[] MixKeyExchange(byte[]? privateKey, byte[]? publicKey) { if (publicKey == null || privateKey == null) { @@ -83,7 +83,15 @@ public static Argon2Parameters GetDefaultArgon2Parameters() Utils.HandleError(res); } - return Argon2Parameters.FromByteArray(rawParameters); + Argon2Parameters? parameters = Argon2Parameters.FromByteArray(rawParameters); + + if (parameters == null) + { + // This should never happen, we should always be able to deserialize the default parameters. + throw new DevolutionsCryptoException(ManagedError.Error); + } + + return parameters; } /// @@ -94,9 +102,9 @@ public static Argon2Parameters GetDefaultArgon2Parameters() /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a base 64 encoded string. - public static string EncryptWithKeyAsBase64String(string data, byte[] key, byte[] aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) + public static string? EncryptWithKeyAsBase64String(string data, byte[] key, byte[]? aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) { - byte[] cipher = Encrypt(Utils.StringToUtf8ByteArray(data), key, aad, version); + byte[]? cipher = Encrypt(Utils.StringToUtf8ByteArray(data), key, aad, version); return Utils.EncodeToBase64String(cipher); } @@ -109,9 +117,9 @@ public static string EncryptWithKeyAsBase64String(string data, byte[] key, byte[ /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a byte array. - public static byte[] EncryptWithKey(string data, byte[] key, byte[] aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) + public static byte[]? EncryptWithKey(string data, byte[] key, byte[]? aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) { - byte[] cipher = Encrypt(Utils.StringToUtf8ByteArray(data), key, aad, version); + byte[]? cipher = Encrypt(Utils.StringToUtf8ByteArray(data), key, aad, version); return cipher; } @@ -141,10 +149,9 @@ public static byte[] DeriveKeyArgon2(byte[] key, Argon2Parameters parameters) } byte[] result = new byte[parameters.Length]; + byte[] parametersRaw = parameters.ToByteArray(); - byte[] parameters_raw = parameters.ToByteArray(); - - long res = Native.DeriveKeyArgon2Native(key, (UIntPtr)key.Length, parameters_raw, (UIntPtr)parameters_raw.Length, result, (UIntPtr)result.Length); + long res = Native.DeriveKeyArgon2Native(key, (UIntPtr)key.Length, parametersRaw, (UIntPtr)parametersRaw.Length, result, (UIntPtr)result.Length); if (res < 0) { @@ -162,7 +169,7 @@ public static byte[] DeriveKeyArgon2(byte[] key, Argon2Parameters parameters) /// The amount of iterations. 10 000 Recommended by NIST. /// The resulting key length. /// Returns the derived password. - public static byte[] DeriveKey(byte[] key, byte[] salt = null, uint iterations = 10000, uint length = 32) + public static byte[] DeriveKey(byte[] key, byte[]? salt = null, uint iterations = 10000, uint length = 32) { return DeriveKeyPbkdf2(key, salt, iterations, length); } @@ -175,7 +182,7 @@ public static byte[] DeriveKey(byte[] key, byte[] salt = null, uint iterations = /// The amount of iterations. 10 000 Recommended by NIST. /// The resulting key length. /// Returns the derived password. - public static byte[] DeriveKeyPbkdf2(byte[] key, byte[] salt = null, uint iterations = 10000, uint length = 32) + public static byte[] DeriveKeyPbkdf2(byte[] key, byte[]? salt = null, uint iterations = 10000, uint length = 32) { if (key == null || key.Length == 0) { @@ -204,7 +211,7 @@ public static byte[] DeriveKeyPbkdf2(byte[] key, byte[] salt = null, uint iterat /// The amount of iterations. 10 000 Recommended by NIST. /// The resulting key length. /// Returns the derived password. - public static byte[] DerivePassword(string password, byte[] salt = null, uint iterations = 10000, uint length = 32) + public static byte[] DerivePassword(string password, byte[]? salt = null, uint iterations = 10000, uint length = 32) { return DeriveKey(Utils.StringToUtf8ByteArray(password), salt, iterations, length); } @@ -216,7 +223,7 @@ public static byte[] DerivePassword(string password, byte[] salt = null, uint it /// The salt. (Optional). /// The amount of iterations. 10 000 Recommended by NIST. /// Returns the decryption result in a byte array. - public static byte[] DerivePassword(string password, string salt, uint iterations = 10000) + public static byte[] DerivePassword(string password, string? salt, uint iterations = 10000) { return DeriveKey(Utils.StringToUtf8ByteArray(password), Utils.StringToUtf8ByteArray(salt), iterations); } @@ -229,7 +236,7 @@ public static byte[] DerivePassword(string password, string salt, uint iteration /// Additional authenticated data. (Optional). /// The fallback decryptor to use if the data is not from Devolutions Crypto. /// Returns the decryption result in a byte array. - public static byte[] Decrypt(byte[] data, byte[] key, byte[] aad = null, ILegacyDecryptor legacyDecryptor = null) + public static byte[]? Decrypt(byte[]? data, byte[] key, byte[]? aad = null, ILegacyDecryptor? legacyDecryptor = null) { if (data == null || data.Length == 0) { @@ -272,7 +279,7 @@ public static byte[] Decrypt(byte[] data, byte[] key, byte[] aad = null, ILegacy /// The private key to use for decryption. /// Additional authenticated data. (Optional). /// Returns the decryption result in a byte array. - public static byte[] DecryptAsymmetric(byte[] data, byte[] privateKey, byte[] aad = null) + public static byte[]? DecryptAsymmetric(byte[]? data, byte[] privateKey, byte[]? aad = null) { if (data == null || data.Length == 0) { @@ -309,9 +316,9 @@ public static byte[] DecryptAsymmetric(byte[] data, byte[] privateKey, byte[] aa /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a base 64 encoded string. - public static string EncryptWithKeyAsBase64String(byte[] data, byte[] key, byte[] aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) + public static string? EncryptWithKeyAsBase64String(byte[] data, byte[] key, byte[]? aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) { - byte[] cipher = Encrypt(data, key, aad, version); + byte[]? cipher = Encrypt(data, key, aad, version); return Utils.EncodeToBase64String(cipher); } @@ -324,9 +331,9 @@ public static string EncryptWithKeyAsBase64String(byte[] data, byte[] key, byte[ /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a byte array. - public static byte[] EncryptWithKey(byte[] data, byte[] key, byte[] aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) + public static byte[]? EncryptWithKey(byte[] data, byte[] key, byte[]? aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) { - byte[] cipher = Encrypt(data, key, aad, version); + byte[]? cipher = Encrypt(data, key, aad, version); return cipher; } @@ -340,7 +347,7 @@ public static byte[] EncryptWithKey(byte[] data, byte[] key, byte[] aad = null, /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a base 64 encoded string. - public static string EncryptWithPasswordAsBase64String(byte[] data, string password, uint iterations = 10000, byte[] aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) + public static string? EncryptWithPasswordAsBase64String(byte[]? data, string password, uint iterations = 10000, byte[]? aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) { if (data == null || data.Length == 0) { @@ -359,7 +366,7 @@ public static string EncryptWithPasswordAsBase64String(byte[] data, string passw byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, keySize); - byte[] cipher = Encrypt(data, key, aad, cipherTextVersion); + byte[]? cipher = Encrypt(data, key, aad, cipherTextVersion); return Utils.EncodeToBase64String(cipher); } @@ -373,7 +380,7 @@ public static string EncryptWithPasswordAsBase64String(byte[] data, string passw /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a base 64 encoded string. - public static string EncryptBase64WithPasswordAsString(string b64data, string password, uint iterations = 10000, byte[] aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) + public static string? EncryptBase64WithPasswordAsString(string b64data, string password, uint iterations = 10000, byte[]? aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) { return EncryptBase64WithPasswordAsBase64String(b64data, password, iterations, aad, cipherTextVersion); } @@ -387,11 +394,11 @@ public static string EncryptBase64WithPasswordAsString(string b64data, string pa /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a base 64 encoded string. - public static string EncryptBase64WithPasswordAsBase64String( + public static string? EncryptBase64WithPasswordAsBase64String( string b64data, string password, uint iterations = 10000, - byte[] aad = null, + byte[]? aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) { if (string.IsNullOrEmpty(b64data)) @@ -411,7 +418,7 @@ public static string EncryptBase64WithPasswordAsBase64String( byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, keySize); - byte[] cipher = Encrypt(Utils.Base64StringToByteArray(b64data), key, aad, cipherTextVersion); + byte[]? cipher = Encrypt(Utils.Base64StringToByteArray(b64data), key, aad, cipherTextVersion); return Utils.EncodeToBase64String(cipher); } @@ -446,7 +453,7 @@ public static byte[] GenerateKey(uint keySize) /// The hash in bytes. Must be a hash from the method HashPassword(). /// The fallback hasher to use if the hash is not from Devolutions Crypto. /// Returns true if the password matches with the hash. - public static bool VerifyPassword(byte[] password, byte[] hash, ILegacyHasher legacyHasher = null) + public static bool VerifyPassword(byte[] password, byte[] hash, ILegacyHasher? legacyHasher = null) { if (password == null || hash == null) { @@ -540,7 +547,7 @@ public static KeyPair GenerateKeyPair() /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as byte array. - public static byte[] Encrypt(byte[] data, byte[] key, byte[] aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) + public static byte[]? Encrypt(byte[]? data, byte[] key, byte[]? aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) { if (data == null || data.Length == 0) { @@ -579,7 +586,7 @@ public static byte[] Encrypt(byte[] data, byte[] key, byte[] aad = null, CipherT /// The keypair to use to sign the data. /// The signature version to use. (Latest is recommended). /// Returns the signature result as byte array. - public static byte[] Sign(byte[] data, SigningKeyPair keypair, SignatureVersion version = SIGNATURE_VERSION) + public static byte[]? Sign(byte[]? data, SigningKeyPair keypair, SignatureVersion version = SIGNATURE_VERSION) { if (data == null || data.Length == 0) { @@ -612,7 +619,7 @@ public static byte[] Sign(byte[] data, SigningKeyPair keypair, SignatureVersion /// The public key that was used to sign the data. /// The signature to verify. /// Returns false if the data, the signature or the public key is invalid or true if everything is valid. - public static bool VerifySignature(byte[] data, SigningPublicKey publicKey, byte[] signature) + public static bool VerifySignature(byte[]? data, SigningPublicKey publicKey, byte[] signature) { if (data == null || data.Length == 0) { @@ -668,7 +675,7 @@ public static SigningKeyPair GenerateSigningKeyPair(SignatureVersion version = S /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as byte array. - public static byte[] EncryptAsymmetric(byte[] data, byte[] publicKey, byte[] aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) + public static byte[]? EncryptAsymmetric(byte[]? data, byte[] publicKey, byte[]? aad = null, CipherTextVersion version = CIPHERTEXT_VERSION) { if (data == null || data.Length == 0) { @@ -709,7 +716,7 @@ public static byte[] EncryptAsymmetric(byte[] data, byte[] publicKey, byte[] aad /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a base 64 encoded string. - public static string EncryptWithPasswordAsBase64String(string data, string password, uint iterations = 10000, byte[] aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) + public static string? EncryptWithPasswordAsBase64String(string data, string password, uint iterations = 10000, byte[]? aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) { if (string.IsNullOrEmpty(data)) { @@ -728,7 +735,7 @@ public static string EncryptWithPasswordAsBase64String(string data, string passw byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, keySize); - byte[] cipher = Encrypt(Utils.StringToUtf8ByteArray(data), key, aad, cipherTextVersion); + byte[]? cipher = Encrypt(Utils.StringToUtf8ByteArray(data), key, aad, cipherTextVersion); return Utils.EncodeToBase64String(cipher); } @@ -742,7 +749,7 @@ public static string EncryptWithPasswordAsBase64String(string data, string passw /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a byte array. - public static byte[] EncryptWithPassword(byte[] data, string password, uint iterations = 10000, byte[] aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) + public static byte[]? EncryptWithPassword(byte[]? data, string password, uint iterations = 10000, byte[]? aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) { if (data == null || data.Length == 0) { @@ -761,7 +768,7 @@ public static byte[] EncryptWithPassword(byte[] data, string password, uint iter byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, keySize); - byte[] cipher = Encrypt(data, key, aad, cipherTextVersion); + byte[]? cipher = Encrypt(data, key, aad, cipherTextVersion); return cipher; } @@ -775,9 +782,11 @@ public static byte[] EncryptWithPassword(byte[] data, string password, uint iter /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a byte array. - public static byte[] EncryptBase64WithPassword(string b64data, string password, uint iterations = 10000, byte[] aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) + public static byte[]? EncryptBase64WithPassword(string? b64data, string password, uint iterations = 10000, byte[]? aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) { - if (string.IsNullOrEmpty(b64data)) + // sduquette 2025-11-12: Explicitly comparing b64data to null because the NotNullWhen directive used by string.IsNullOrEmpty + // is not available in netstandard2.0. + if (b64data == null || string.IsNullOrEmpty(b64data)) { return null; } @@ -794,7 +803,7 @@ public static byte[] EncryptBase64WithPassword(string b64data, string password, byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, keySize); - byte[] cipher = Encrypt(Utils.Base64StringToByteArray(b64data), key, aad, cipherTextVersion); + byte[]? cipher = Encrypt(Utils.Base64StringToByteArray(b64data), key, aad, cipherTextVersion); return cipher; } @@ -808,7 +817,7 @@ public static byte[] EncryptBase64WithPassword(string b64data, string password, /// Additional authenticated data. (Optional). /// The cipher version to use. (Latest is recommended). /// Returns the encryption result as a byte array. - public static byte[] EncryptWithPassword(string data, string password, uint iterations = 10000, byte[] aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) + public static byte[]? EncryptWithPassword(string data, string password, uint iterations = 10000, byte[]? aad = null, CipherTextVersion cipherTextVersion = CIPHERTEXT_VERSION) { if (string.IsNullOrEmpty(data)) { @@ -827,7 +836,7 @@ public static byte[] EncryptWithPassword(string data, string password, uint iter byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, keySize); - byte[] cipher = Encrypt(Utils.StringToUtf8ByteArray(data), key, aad, cipherTextVersion); + byte[]? cipher = Encrypt(Utils.StringToUtf8ByteArray(data), key, aad, cipherTextVersion); return cipher; } @@ -840,9 +849,9 @@ public static byte[] EncryptWithPassword(string data, string password, uint iter /// Additional authenticated data. (Optional). /// The fallback decryptor to use if the data is not from Devolutions Crypto. /// Returns the decryption result as a UTF8 encoded string. - public static string DecryptWithKeyAsUtf8String(string b64data, byte[] key, byte[] aad = null, ILegacyDecryptor legacyDecryptor = null) + public static string? DecryptWithKeyAsUtf8String(string b64data, byte[] key, byte[]? aad = null, ILegacyDecryptor? legacyDecryptor = null) { - byte[] result = Decrypt(Utils.Base64StringToByteArray(b64data), key, aad, legacyDecryptor); + byte[]? result = Decrypt(Utils.Base64StringToByteArray(b64data), key, aad, legacyDecryptor); return Utils.ByteArrayToUtf8String(result); } @@ -855,9 +864,9 @@ public static string DecryptWithKeyAsUtf8String(string b64data, byte[] key, byte /// Additional authenticated data. (Optional). /// The fallback decryptor to use if the data is not from Devolutions Crypto. /// Returns the decryption result as a byte array. - public static byte[] DecryptWithKey(string b64data, byte[] key, byte[] aad = null, ILegacyDecryptor legacyDecryptor = null) + public static byte[]? DecryptWithKey(string b64data, byte[] key, byte[]? aad = null, ILegacyDecryptor? legacyDecryptor = null) { - byte[] result = Decrypt(Utils.Base64StringToByteArray(b64data), key, aad, legacyDecryptor); + byte[]? result = Decrypt(Utils.Base64StringToByteArray(b64data), key, aad, legacyDecryptor); return result; } @@ -870,9 +879,9 @@ public static byte[] DecryptWithKey(string b64data, byte[] key, byte[] aad = nul /// Additional authenticated data. (Optional). /// The fallback decryptor to use if the data is not from Devolutions Crypto. /// Returns the decryption result as a UTF8 encoded string. - public static string DecryptWithKeyAsUtf8String(byte[] data, byte[] key, byte[] aad = null, ILegacyDecryptor legacyDecryptor = null) + public static string? DecryptWithKeyAsUtf8String(byte[] data, byte[] key, byte[]? aad = null, ILegacyDecryptor? legacyDecryptor = null) { - byte[] result = Decrypt(data, key, aad, legacyDecryptor); + byte[]? result = Decrypt(data, key, aad, legacyDecryptor); return Utils.ByteArrayToUtf8String(result); } @@ -885,11 +894,9 @@ public static string DecryptWithKeyAsUtf8String(byte[] data, byte[] key, byte[] /// Additional authenticated data. (Optional). /// The fallback decryptor to use if the data is not from Devolutions Crypto. /// Returns the decryption result as a byte array. - public static byte[] DecryptWithKey(byte[] data, byte[] key, byte[] aad = null, ILegacyDecryptor legacyDecryptor = null) + public static byte[]? DecryptWithKey(byte[] data, byte[] key, byte[]? aad = null, ILegacyDecryptor? legacyDecryptor = null) { - byte[] result = Decrypt(data, key, aad, legacyDecryptor); - - return result; + return Decrypt(data, key, aad, legacyDecryptor); } /// @@ -900,7 +907,7 @@ public static byte[] DecryptWithKey(byte[] data, byte[] key, byte[] aad = null, /// The number of iterations used to derive the password. /// Additional authenticated data. (Optional). /// Returns the decryption result as a UTF8 encoded string. - public static string DecryptWithPasswordAsUtf8String(byte[] data, string password, uint iterations = 10000, byte[] aad = null) + public static string? DecryptWithPasswordAsUtf8String(byte[]? data, string password, uint iterations = 10000, byte[]? aad = null) { if (data == null || data.Length == 0) { @@ -908,33 +915,21 @@ public static string DecryptWithPasswordAsUtf8String(byte[] data, string passwor } // There was a bug in DeriveKey v1 where the generated key was 256 bytes instead of 256 bits, only in C#. - // This is unfortunatly the best way we found to fix it while keeping backward compatibility. + // This is unfortunately the best way we found to fix it while keeping backward compatibility. // We try to decrypt with a 256 bits key, and if it doesn't work(InvalidMac means either the data or the key is invalid), // we try with the buggy 256 bytes key. byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, 32); - if (key == null) - { - return null; - } - - byte[] result = DecryptSafe(data, key, out DevolutionsCryptoException exception, aad); + byte[]? result = DecryptSafe(data, key, out DevolutionsCryptoException? exception, aad); - if (exception != null && exception.NativeError == NativeError.InvalidMac) + if (exception is { NativeError: NativeError.InvalidMac }) { - key = null; - result = null; - key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, 256); - if (key == null) - { - return null; - } - result = Decrypt(data, key, aad); return Utils.ByteArrayToUtf8String(result); } - else if (exception != null) + + if (exception != null) { throw exception; } @@ -964,13 +959,13 @@ public static byte[] JoinShares(byte[][] shares) byte[] secret = new byte[secretLength]; // Get unmanaged references - GCHandle[] handles = new GCHandle[(int)nbShares]; + GCHandle[] handles = new GCHandle[nbShares]; for (int i = 0; i < shares.Length; i++) { handles[i] = GCHandle.Alloc(shares[i], GCHandleType.Pinned); } - IntPtr[] pointers = new IntPtr[(int)nbShares]; + IntPtr[] pointers = new IntPtr[nbShares]; for (int i = 0; i < handles.Length; i++) { pointers[i] = handles[i].AddrOfPinnedObject(); @@ -1002,38 +997,25 @@ public static byte[] JoinShares(byte[][] shares) /// The number of iterations used to derive the password. /// Additional authenticated data. (Optional). /// Returns the decryption result as a UTF8 encoded string. - public static string DecryptWithPasswordAsUtf8String(string b64data, string password, uint iterations = 10000, byte[] aad = null) + public static string? DecryptWithPasswordAsUtf8String(string? b64data, string password, uint iterations = 10000, byte[]? aad = null) { - if (string.IsNullOrEmpty(b64data)) + // sduquette 2025-11-12: Explicitly comparing b64data to null because the NotNullWhen directive used by string.IsNullOrEmpty + // is not available in netstandard2.0. + if (b64data == null || string.IsNullOrEmpty(b64data)) { return null; } // There was a bug in DeriveKey v1 where the generated key was 256 bytes instead of 256 bits, only in C#. - // This is unfortunatly the best way we found to fix it while keeping backward compatibility. + // This is unfortunately the best way we found to fix it while keeping backward compatibility. // We try to decrypt with a 256 bits key, and if it doesn't work(InvalidMac means either the data or the key is invalid), // we try with the buggy 256 bytes key. byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, 32); + byte[]? result = DecryptSafe(Utils.Base64StringToByteArray(b64data), key, out DevolutionsCryptoException? exception, aad); - if (key == null) - { - return null; - } - - byte[] result = DecryptSafe(Utils.Base64StringToByteArray(b64data), key, out DevolutionsCryptoException exception, aad); - - if (exception != null && exception.NativeError == NativeError.InvalidMac) + if (exception is { NativeError: NativeError.InvalidMac }) { - key = null; - result = null; - key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, 256); - - if (key == null) - { - return null; - } - result = Decrypt(Utils.Base64StringToByteArray(b64data), key, aad); } else if (exception != null) @@ -1062,13 +1044,13 @@ public static byte[][] GenerateSharedKey(int nbShares, int threshold, int secret shares[i] = new byte[sharesLength]; } - GCHandle[] handles = new GCHandle[(int)nbShares]; + GCHandle[] handles = new GCHandle[nbShares]; for (int i = 0; i < shares.Length; i++) { handles[i] = GCHandle.Alloc(shares[i], GCHandleType.Pinned); } - IntPtr[] pointers = new IntPtr[(int)nbShares]; + IntPtr[] pointers = new IntPtr[nbShares]; for (int i = 0; i < handles.Length; i++) { pointers[i] = handles[i].AddrOfPinnedObject(); @@ -1097,7 +1079,7 @@ public static byte[][] GenerateSharedKey(int nbShares, int threshold, int secret /// The number of iterations used to derive the password. /// Additional authenticated data. (Optional). /// Returns the decryption result as a byte array. - public static byte[] DecryptWithPassword(byte[] data, string password, uint iterations = 10000, byte[] aad = null) + public static byte[]? DecryptWithPassword(byte[]? data, string password, uint iterations = 10000, byte[]? aad = null) { if (data == null || data.Length == 0) { @@ -1105,30 +1087,15 @@ public static byte[] DecryptWithPassword(byte[] data, string password, uint iter } //// There was a bug in DeriveKey v1 where the generated key was 256 bytes instead of 256 bits, only in C#. - //// This is unfortunatly the best way we found to fix it while keeping backward compatibility. + //// This is unfortunately the best way we found to fix it while keeping backward compatibility. //// We try to decrypt with a 256 bits key, and if it doesn't work(InvalidMac means either the data or the key is invalid), //// we try with the buggy 256 bytes key. byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, 32); + byte[]? result = DecryptSafe(data, key, out DevolutionsCryptoException? exception, aad); - if (key == null) + if (exception is { NativeError: NativeError.InvalidMac }) { - return null; - } - - byte[] result = DecryptSafe(data, key, out DevolutionsCryptoException exception, aad); - - if (exception != null && exception.NativeError == NativeError.InvalidMac) - { - key = null; - result = null; - key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, 256); - - if (key == null) - { - return null; - } - result = Decrypt(data, key, aad); } else if (exception != null) @@ -1147,7 +1114,7 @@ public static byte[] DecryptWithPassword(byte[] data, string password, uint iter /// The number of iterations used to derive the password. /// Additional authenticated data. (Optional). /// Returns the decryption result as a byte array. - public static byte[] DecryptWithPassword(string b64data, string password, uint iterations = 10000, byte[] aad = null) + public static byte[]? DecryptWithPassword(string b64data, string password, uint iterations = 10000, byte[]? aad = null) { if (string.IsNullOrEmpty(b64data)) { @@ -1155,30 +1122,16 @@ public static byte[] DecryptWithPassword(string b64data, string password, uint i } // There was a bug in DeriveKey v1 where the generated key was 256 bytes instead of 256 bits, only in C#. - // This is unfortunatly the best way we found to fix it while keeping backward compatibility. + // This is unfortunately the best way we found to fix it while keeping backward compatibility. // We try to decrypt with a 256 bits key, and if it doesn't work(InvalidMac means either the data or the key is invalid), // we try with the buggy 256 bytes key. byte[] key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, 32); - if (key == null) - { - return null; - } + byte[]? result = DecryptSafe(Utils.Base64StringToByteArray(b64data), key, out DevolutionsCryptoException? exception, aad); - byte[] result = DecryptSafe(Utils.Base64StringToByteArray(b64data), key, out DevolutionsCryptoException exception, aad); - - if (exception != null && exception.NativeError == NativeError.InvalidMac) + if (exception is { NativeError: NativeError.InvalidMac }) { - key = null; - result = null; - key = DeriveKey(Utils.StringToUtf8ByteArray(password), null, iterations, 256); - - if (key == null) - { - return null; - } - result = Decrypt(Utils.Base64StringToByteArray(b64data), key, aad); } else if (exception != null) @@ -1197,7 +1150,7 @@ public static byte[] DecryptWithPassword(string b64data, string password, uint i /// The exception if an error occurs. /// Additional authenticated data. (Optional). /// Returns the decryption result in a byte array. - internal static byte[] DecryptSafe(byte[] data, byte[] key, out DevolutionsCryptoException exception, byte[] aad = null) + internal static byte[]? DecryptSafe(byte[]? data, byte[]? key, out DevolutionsCryptoException? exception, byte[]? aad = null) { exception = null; @@ -1232,7 +1185,7 @@ internal static byte[] DecryptSafe(byte[] data, byte[] key, out DevolutionsCrypt private static bool SharesLengthAreValid(byte[][] shares) { - if (shares == null || shares.Length == 0) + if (shares.Length == 0) { return false; } diff --git a/wrappers/csharp/src/Native.cs b/wrappers/csharp/src/Native.cs index 0dd70e44..535dd91b 100644 --- a/wrappers/csharp/src/Native.cs +++ b/wrappers/csharp/src/Native.cs @@ -52,8 +52,8 @@ static Native() Version assemblyVersion = assembly.GetName().Version; Version managedVersion = Version.Parse(ManagedVersion); - - if(managedVersion.Revision == -1) + + if (managedVersion.Revision == -1) { managedVersion = Version.Parse(ManagedVersion + ".0"); } @@ -68,7 +68,7 @@ static Native() } [Obsolete("This method has been deprecated. Use Managed.Decrypt instead.")] - public static byte[] Decrypt(byte[] data, byte[] key) + public static byte[]? Decrypt(byte[] data, byte[] key) { return Managed.Decrypt(data, key); } @@ -86,7 +86,7 @@ public static byte[] DeriveKey(byte[] key, byte[] salt, uint iterations = 10000, } [Obsolete("This method has been deprecated. Use Managed.Encrypt instead.")] - public static byte[] Encrypt(byte[] data, byte[] key, uint version = 0) + public static byte[]? Encrypt(byte[] data, byte[] key, uint version = 0) { return Managed.Encrypt(data, key, null, (CipherTextVersion)version); } diff --git a/wrappers/csharp/src/SigningKeyPair.cs b/wrappers/csharp/src/SigningKeyPair.cs index e8a41f6b..a0cce353 100644 --- a/wrappers/csharp/src/SigningKeyPair.cs +++ b/wrappers/csharp/src/SigningKeyPair.cs @@ -10,8 +10,9 @@ public class SigningKeyPair /// /// Initializes a new instance of the class. /// - public SigningKeyPair() + public SigningKeyPair(byte[] payload) { + this.Payload = payload; } /// @@ -26,9 +27,7 @@ public SigningKeyPair() /// Returns the deserialized parameters. public static SigningKeyPair FromByteArray(byte[] data) { - SigningKeyPair keypair = new SigningKeyPair(); - keypair.Payload = data; - + SigningKeyPair keypair = new SigningKeyPair(data); return keypair; } diff --git a/wrappers/csharp/src/SigningPublicKey.cs b/wrappers/csharp/src/SigningPublicKey.cs index 692b4ecd..d0cceab0 100644 --- a/wrappers/csharp/src/SigningPublicKey.cs +++ b/wrappers/csharp/src/SigningPublicKey.cs @@ -8,8 +8,9 @@ public class SigningPublicKey /// /// Initializes a new instance of the class. /// - public SigningPublicKey() + public SigningPublicKey(byte[] payload) { + this.Payload = payload; } /// @@ -24,9 +25,7 @@ public SigningPublicKey() /// Returns the deserialized parameters. public static SigningPublicKey FromByteArray(byte[] data) { - SigningPublicKey publicKey = new SigningPublicKey(); - publicKey.Payload = data; - + SigningPublicKey publicKey = new(data); return publicKey; } diff --git a/wrappers/csharp/src/Utils.cs b/wrappers/csharp/src/Utils.cs index 51135652..5d84f605 100644 --- a/wrappers/csharp/src/Utils.cs +++ b/wrappers/csharp/src/Utils.cs @@ -14,7 +14,7 @@ public static class Utils /// /// The data to convert. /// A byte array. - public static byte[] Base64StringToByteArray(string data) + public static byte[]? Base64StringToByteArray(string data) { if (string.IsNullOrEmpty(data)) { @@ -30,7 +30,7 @@ public static byte[] Base64StringToByteArray(string data) /// The data to convert. /// A UTF8 encoded string. [Obsolete("This method has been deprecated. Use ByteArrayToUtf8String instead.")] - public static string ByteArrayToString(byte[] data) + public static string? ByteArrayToString(byte[] data) { return ByteArrayToUtf8String(data); } @@ -40,7 +40,7 @@ public static string ByteArrayToString(byte[] data) /// /// The data to convert. /// A UTF8 encoded string. - public static string ByteArrayToUtf8String(byte[] data) + public static string? ByteArrayToUtf8String(byte[]? data) { if (data == null || data.Length == 0) { @@ -90,17 +90,10 @@ public static byte[] ConcatArrays(params byte[][] list) /// The second value to compare. /// Returns false if the values are not equal is invalid or true if the values are equal. If there is an error, /// it will trigger a DevolutionsCryptoException. - public static bool ConstantTimeEquals(string x, string y) + public static bool ConstantTimeEquals(string? x, string? y) { - if (x == null) - { - x = string.Empty; - } - - if (y == null) - { - y = string.Empty; - } + x ??= string.Empty; + y ??= string.Empty; byte[] xBytes = Encoding.UTF8.GetBytes(x); byte[] yBytes = Encoding.UTF8.GetBytes(y); @@ -130,7 +123,7 @@ public static bool ConstantTimeEquals(Guid x, Guid y) /// The second value to compare. /// Returns false if the values are not equal is invalid or true if the values are equal. If there is an error, /// it will trigger a DevolutionsCryptoException. - public static bool ConstantTimeEquals(byte[] x, byte[] y) + public static bool ConstantTimeEquals(byte[]? x, byte[]? y) { if (x == null || y == null) { @@ -153,7 +146,7 @@ public static bool ConstantTimeEquals(byte[] x, byte[] y) /// The data to convert. /// A byte array. [Obsolete("This method has been deprecated. Use DecodeFromBase64 instead.")] - public static byte[] Decode(string data) + public static byte[]? Decode(string data) { return DecodeFromBase64(data); } @@ -163,7 +156,7 @@ public static byte[] Decode(string data) /// /// The data to convert. /// A byte array. - public static byte[] DecodeFromBase64(string base64) + public static byte[]? DecodeFromBase64(string base64) { if (string.IsNullOrEmpty(base64)) { @@ -179,9 +172,9 @@ public static byte[] DecodeFromBase64(string base64) byte[] buffer = new byte[length]; - long decode_res = Native.DecodeNative(base64, (UIntPtr)base64.Length, buffer, (UIntPtr)buffer.Length); + long decodeResult = Native.DecodeNative(base64, (UIntPtr)base64.Length, buffer, (UIntPtr)buffer.Length); - if (decode_res == -1) + if (decodeResult == -1) { return null; } @@ -192,25 +185,27 @@ public static byte[] DecodeFromBase64(string base64) /// /// Converts a base 64 url string to a byte array. /// - /// The data to convert. + /// The data to convert. /// A byte array. - public static byte[] DecodeFromBase64Url(string base64url) + public static byte[]? DecodeFromBase64Url(string? base64Url) { - if (string.IsNullOrEmpty(base64url)) + // sduquette 2025-11-12: Explicitly comparing base64Url to null because the NotNullWhen directive used by string.IsNullOrEmpty + // is not available in netstandard2.0. + if (base64Url == null || string.IsNullOrEmpty(base64Url)) { return null; } - byte[] buffer = new byte[base64url.Length]; + byte[] buffer = new byte[base64Url.Length]; - long decode_res = Native.DecodeUrlNative(base64url, (UIntPtr)base64url.Length, buffer, (UIntPtr)buffer.Length); + long decodeResult = Native.DecodeUrlNative(base64Url, (UIntPtr)base64Url.Length, buffer, (UIntPtr)buffer.Length); - if (decode_res == -1) + if (decodeResult == -1) { return null; } - Array.Resize(ref buffer, (int)decode_res); + Array.Resize(ref buffer, (int)decodeResult); return buffer; } @@ -220,18 +215,7 @@ public static byte[] DecodeFromBase64Url(string base64url) /// /// The data to convert. /// A base 64 string. - [Obsolete("This method has been deprecated. Use EncodeToBase64 instead.")] - public static string Encode(byte[] data) - { - return EncodeToBase64String(data); - } - - /// - /// Converts a byte array to a base 64 encoded string. - /// - /// The data to convert. - /// A base 64 string. - public static string EncodeToBase64String(byte[] data) + public static string? EncodeToBase64String(byte[]? data) { if (data == null || data.Length == 0) { @@ -247,7 +231,7 @@ public static string EncodeToBase64String(byte[] data) byte[] buffer = new byte[length]; - long encode_res = Native.EncodeNative(data, (UIntPtr)data.Length, buffer, (UIntPtr)buffer.Length); + long encodeResult = Native.EncodeNative(data, (UIntPtr)data.Length, buffer, (UIntPtr)buffer.Length); return ByteArrayToUtf8String(buffer); } @@ -257,7 +241,7 @@ public static string EncodeToBase64String(byte[] data) /// /// The data to convert. /// A base 64 url string. - public static string EncodeToBase64UrlString(byte[] data) + public static string? EncodeToBase64UrlString(byte[]? data) { if (data == null || data.Length == 0) { @@ -268,9 +252,9 @@ public static string EncodeToBase64UrlString(byte[] data) byte[] buffer = new byte[length]; - long encode_res = Native.EncodeUrlNative(data, (UIntPtr)data.Length, buffer, (UIntPtr)buffer.Length); + long encodeResult = Native.EncodeUrlNative(data, (UIntPtr)data.Length, buffer, (UIntPtr)buffer.Length); - Array.Resize(ref buffer, (int)encode_res); + Array.Resize(ref buffer, (int)encodeResult); return ByteArrayToUtf8String(buffer); } @@ -281,9 +265,11 @@ public static string EncodeToBase64UrlString(byte[] data) /// /// The base 64 string to calculate the resulting length. /// The original buffer length. - public static int GetDecodedBase64StringLength(string base64) + public static int GetDecodedBase64StringLength(string? base64) { - if (string.IsNullOrEmpty(base64)) + // sduquette 2025-11-12: Explicitly comparing base64 to null because the NotNullWhen directive used by string.IsNullOrEmpty + // is not available in netstandard2.0. + if (base64 == null || string.IsNullOrEmpty(base64)) { return 0; } @@ -331,11 +317,6 @@ public static int GetDecodedLength(string base64) /// The resulting base 64 buffer lentgh. public static int GetEncodedBase64StringLength(byte[] buffer) { - if (buffer == null) - { - return 0; - } - return ((4 * buffer.Length / 3) + 3) & ~3; } @@ -359,7 +340,7 @@ public static int GetEncodedLength(byte[] buffer) /// Block size. /// Parallelism factor. /// The resulting hash. - public static string ScryptSimple(byte[] password, byte[] salt, byte logN, uint r, uint p) + public static string? ScryptSimple(byte[] password, byte[] salt, byte logN, uint r, uint p) { if (password == null || salt == null) { @@ -398,39 +379,23 @@ public static byte[] StringToByteArray(string data) /// /// The string to convert. /// A UTF8 string in a byte array. - public static byte[] StringToUtf8ByteArray(string data) + public static byte[] StringToUtf8ByteArray(string? data) { if (data == null) { - return null; + return []; } return Encoding.UTF8.GetBytes(data); } - /// - /// Converts a byte array to a base 64 encoded string. - /// - /// The data to convert. - /// A base 64 string. - [Obsolete("This method has been deprecated. Use EncodeToBase64 instead.")] - public static string ToBase64String(byte[] bytes) - { - if (bytes == null || bytes.Length == 0) - { - return null; - } - - return Encode(bytes); - } - /// /// Validate that the buffer is from the Devolutions Crypto Library. /// /// The buffer to validate. /// The data type to validate. /// Returns true if the buffer received matches the data type. - public static bool ValidateHeader(byte[] data, DataType type) + public static bool ValidateHeader(byte[]? data, DataType type) { if (data == null) { @@ -456,7 +421,7 @@ public static bool ValidateHeader(byte[] data, DataType type) /// Returns true if the base 64 string received matches the data type. public static bool ValidateHeaderFromBase64(string base64, DataType type) { - byte[] data = DecodeFromBase64(base64); + byte[]? data = DecodeFromBase64(base64); return ValidateHeader(data, type); } @@ -469,7 +434,7 @@ public static bool ValidateHeaderFromBase64(string base64, DataType type) /// The stream to validate. /// The data type to validate. /// Returns true if the stream data received matches the data type. - public static bool ValidateHeaderFromStream(Stream stream, DataType type) + public static bool ValidateHeaderFromStream(Stream? stream, DataType type) { if (stream == null) { diff --git a/wrappers/csharp/src/devolutions-crypto.csproj b/wrappers/csharp/src/devolutions-crypto.csproj index 93c009fc..77daeb59 100644 --- a/wrappers/csharp/src/devolutions-crypto.csproj +++ b/wrappers/csharp/src/devolutions-crypto.csproj @@ -1,25 +1,23 @@ - + netstandard2.0;net48 devolutions_crypto Devolutions.Crypto + 13.0 + enable - + DevolutionsCrypto.dll Always - + libDevolutionsCrypto.so Always - + libDevolutionsCrypto.dylib Always - \ No newline at end of file diff --git a/wrappers/csharp/tests/unit-tests/Conformity.cs b/wrappers/csharp/tests/unit-tests/Conformity.cs index f048ac8f..eeddfed4 100644 --- a/wrappers/csharp/tests/unit-tests/Conformity.cs +++ b/wrappers/csharp/tests/unit-tests/Conformity.cs @@ -17,9 +17,9 @@ public class Conformity [TestMethod] public void DecryptAsymmetricV2() { - byte[] decryptResult = Managed.DecryptAsymmetric( + byte[]? decryptResult = Managed.DecryptAsymmetric( Utils.Base64StringToByteArray("DQwCAAIAAgCIG9L2MTiumytn7H/p5I3aGVdhV3WUL4i8nIeMWIJ1YRbNQ6lEiQDAyfYhbs6gg1cD7+5Ft2Q5cm7ArsGfiFYWnscm1y7a8tAGfjFFTonzrg=="), - Utils.Base64StringToByteArray("DQwBAAEAAQAAwQ3oJvU6bq2iZlJwAzvbmqJczNrFoeWPeIyJP9SSbQ==")); + Utils.Base64StringToByteArray("DQwBAAEAAQAAwQ3oJvU6bq2iZlJwAzvbmqJczNrFoeWPeIyJP9SSbQ==")!); Assert.IsTrue(Utils.ByteArrayToUtf8String(decryptResult) == "testdata"); } @@ -27,9 +27,9 @@ public void DecryptAsymmetricV2() [TestMethod] public void DecryptAsymmetricAadV2() { - byte[] decryptResult = Managed.DecryptAsymmetric( + byte[]? decryptResult = Managed.DecryptAsymmetric( Utils.Base64StringToByteArray("DQwCAAIAAgB1u62xYeyppWf83QdWwbwGUt5QuiAFZr+hIiFEvMRbXiNCE3RMBNbmgQkLr/vME0BeQa+uUTXZARvJcyNXHyAE4tSdw6o/psU/kw/Z/FbsPw=="), - Utils.Base64StringToByteArray("DQwBAAEAAQC9qf9UY1ovL/48ALGHL9SLVpVozbdjYsw0EPerUl3zYA=="), + Utils.Base64StringToByteArray("DQwBAAEAAQC9qf9UY1ovL/48ALGHL9SLVpVozbdjYsw0EPerUl3zYA==")!, aad: Utils.StringToUtf8ByteArray("this is some public data")); Assert.IsTrue(Utils.ByteArrayToUtf8String(decryptResult) == "testdata"); @@ -39,11 +39,11 @@ public void DecryptAsymmetricAadV2() public void DecryptV1() { byte[] encryptedData = Utils.Base64StringToByteArray( - "DQwCAAAAAQCK1twEut+TeJfFbTWCRgHjyS6bOPOZUEQAeBtSFFRl2jHggM/34n68zIZWGbsZHkufVzU6mTN5N2Dx9bTplrycv5eNVevT4P9FdVHJ751D+A=="); - byte[] encryptKey = Utils.Base64StringToByteArray("ozJVEme4+5e/4NG3C+Rl26GQbGWAqGc0QPX8/1xvaFM="); + "DQwCAAAAAQCK1twEut+TeJfFbTWCRgHjyS6bOPOZUEQAeBtSFFRl2jHggM/34n68zIZWGbsZHkufVzU6mTN5N2Dx9bTplrycv5eNVevT4P9FdVHJ751D+A==")!; + byte[] encryptKey = Utils.Base64StringToByteArray("ozJVEme4+5e/4NG3C+Rl26GQbGWAqGc0QPX8/1xvaFM=")!; - byte[] decryptResult = Managed.Decrypt(encryptedData, encryptKey); - string decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? decryptResult = Managed.Decrypt(encryptedData, encryptKey); + string? decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultAsUtf8String, "test Ciph3rtext~"); } @@ -51,23 +51,23 @@ public void DecryptV1() public void DecryptAadV1() { byte[] encryptedData = Utils.Base64StringToByteArray( - "DQwCAAEAAQCeKfbTqYjfVCEPEiAJjiypBstPmZz0AnpliZKoR+WXTKdj2f/4ops0++dDBVZ+XdyE1KfqxViWVc9djy/HSCcPR4nDehtNI69heGCIFudXfQ=="); - byte[] encryptKey = Utils.Base64StringToByteArray("ozJVEme4+5e/4NG3C+Rl26GQbGWAqGc0QPX8/1xvaFM="); + "DQwCAAEAAQCeKfbTqYjfVCEPEiAJjiypBstPmZz0AnpliZKoR+WXTKdj2f/4ops0++dDBVZ+XdyE1KfqxViWVc9djy/HSCcPR4nDehtNI69heGCIFudXfQ==")!; + byte[] encryptKey = Utils.Base64StringToByteArray("ozJVEme4+5e/4NG3C+Rl26GQbGWAqGc0QPX8/1xvaFM=")!; byte[] aad = Utils.StringToUtf8ByteArray("this is some public data"); - byte[] decryptResult = Managed.Decrypt(encryptedData, encryptKey, aad: aad); - string decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? decryptResult = Managed.Decrypt(encryptedData, encryptKey, aad: aad); + string? decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultAsUtf8String, "test Ciph3rtext~"); } [TestMethod] public void DecryptV2() { - byte[] encryptedData = Utils.Base64StringToByteArray("DQwCAAAAAgAA0iPpI4IEzcrWAQiy6tqDqLbRYduGvlMC32mVH7tpIN2CXDUu5QHF91I7pMrmjt/61pm5CeR/IcU="); - byte[] encryptKey = Utils.Base64StringToByteArray("ozJVEme4+5e/4NG3C+Rl26GQbGWAqGc0QPX8/1xvaFM="); + byte[] encryptedData = Utils.Base64StringToByteArray("DQwCAAAAAgAA0iPpI4IEzcrWAQiy6tqDqLbRYduGvlMC32mVH7tpIN2CXDUu5QHF91I7pMrmjt/61pm5CeR/IcU=")!; + byte[] encryptKey = Utils.Base64StringToByteArray("ozJVEme4+5e/4NG3C+Rl26GQbGWAqGc0QPX8/1xvaFM=")!; - byte[] decryptResult = Managed.Decrypt(encryptedData, encryptKey); - string decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? decryptResult = Managed.Decrypt(encryptedData, encryptKey); + string? decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultAsUtf8String, "test Ciph3rtext~2"); } @@ -75,24 +75,24 @@ public void DecryptV2() public void DecryptAadV2() { byte[] encryptedData = Utils.Base64StringToByteArray( - "DQwCAAEAAgA9bh989dao0Pvaz1NpJTI5m7M4br2qVjZtFwXXoXZOlkCjtqU/uif4pbNCcpEodzeP4YG1QvfKVQ=="); - byte[] encryptKey = Utils.Base64StringToByteArray("ozJVEme4+5e/4NG3C+Rl26GQbGWAqGc0QPX8/1xvaFM="); + "DQwCAAEAAgA9bh989dao0Pvaz1NpJTI5m7M4br2qVjZtFwXXoXZOlkCjtqU/uif4pbNCcpEodzeP4YG1QvfKVQ==")!; + byte[] encryptKey = Utils.Base64StringToByteArray("ozJVEme4+5e/4NG3C+Rl26GQbGWAqGc0QPX8/1xvaFM=")!; byte[] aad = Utils.StringToUtf8ByteArray("this is some public data"); - byte[] decryptResult = Managed.Decrypt(encryptedData, encryptKey, aad: aad); - string decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? decryptResult = Managed.Decrypt(encryptedData, encryptKey, aad: aad); + string? decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultAsUtf8String, "test Ciph3rtext~"); } [TestMethod] public void DeriveKeyArgon2_Default() { - Argon2Parameters parameters = Argon2Parameters.FromByteArray(Utils.Base64StringToByteArray("AQAAACAAAAABAAAAIAAAAAEAAAACEwAAAAAQAAAAimFBkm3f8+f+YfLRnF5OoQ==")); + Argon2Parameters? parameters = Argon2Parameters.FromByteArray(Utils.Base64StringToByteArray("AQAAACAAAAABAAAAIAAAAAEAAAACEwAAAAAQAAAAimFBkm3f8+f+YfLRnF5OoQ==")!); + Assert.IsNotNull(parameters); byte[] password = Utils.StringToUtf8ByteArray("password"); - byte[] derivedPassword = Managed.DeriveKeyArgon2(password, parameters); - string derivedPasswordAsBase64String = Utils.EncodeToBase64String(derivedPassword); + string? derivedPasswordAsBase64String = Utils.EncodeToBase64String(derivedPassword); Assert.AreEqual(derivedPasswordAsBase64String, "AcEN6Cb1Om6tomZScAM725qiXMzaxaHlj3iMiT/Ukq0="); } @@ -101,7 +101,7 @@ public void DeriveKey_Default() { byte[] encodedPassword = Utils.StringToUtf8ByteArray("testpassword"); byte[] derivedPassword = Managed.DeriveKey(encodedPassword); - string derivedPasswordAsBase64String = Utils.EncodeToBase64String(derivedPassword); + string derivedPasswordAsBase64String = Utils.EncodeToBase64String(derivedPassword)!; Assert.AreEqual(derivedPasswordAsBase64String, "ImfGCyv6PwMYaJShGxR4MfVrjuUrsI0CSarJgOApwf8="); } @@ -110,7 +110,7 @@ public void DeriveKey_Iterations() { byte[] encodedPassword = Utils.StringToUtf8ByteArray("testPa$$"); byte[] derivedPassword = Managed.DeriveKey(encodedPassword, null, 100); - string derivedPasswordAsBase64String = Utils.EncodeToBase64String(derivedPassword); + string derivedPasswordAsBase64String = Utils.EncodeToBase64String(derivedPassword)!; Assert.AreEqual(derivedPasswordAsBase64String, "ev/GiJLvOgIkkWrnIrHSi2fdZE5qJBIrW+DLeMLIXK4="); } @@ -118,9 +118,9 @@ public void DeriveKey_Iterations() public void DeriveKey_Salt() { byte[] encodedPassword = Utils.StringToUtf8ByteArray("testPa$$"); - byte[] saltBytes = Utils.DecodeFromBase64("tdTt5wgeqQYLvkiXKkFirqy2hMbzadBtL+jekVeNCRA="); + byte[] saltBytes = Utils.DecodeFromBase64("tdTt5wgeqQYLvkiXKkFirqy2hMbzadBtL+jekVeNCRA=")!; byte[] derivedPassword = Managed.DeriveKey(encodedPassword, saltBytes, 100); - string derivedPasswordAsBase64String = Utils.EncodeToBase64String(derivedPassword); + string derivedPasswordAsBase64String = Utils.EncodeToBase64String(derivedPassword)!; Assert.AreEqual(derivedPasswordAsBase64String, "ZaYRZeQiIPJ+Jl511AgHZjv4/HbCFq4eUP9yNa3gowI="); } @@ -129,7 +129,7 @@ public void VerifyPasswordV1_Default() { bool result = Managed.VerifyPassword( Utils.StringToUtf8ByteArray("password1"), - Utils.DecodeFromBase64("DQwDAAAAAQAQJwAAXCzLFoyeZhFSDYBAPiIWhCk04aoP/lalOoCl7D+skIY/i+3WT7dn6L8WvnfEq6flCd7i+IcKb3GEK4rCpzhDlw==")); + Utils.DecodeFromBase64("DQwDAAAAAQAQJwAAXCzLFoyeZhFSDYBAPiIWhCk04aoP/lalOoCl7D+skIY/i+3WT7dn6L8WvnfEq6flCd7i+IcKb3GEK4rCpzhDlw==")!); Assert.IsTrue(result); } @@ -139,7 +139,7 @@ public void VerifyPasswordV1_Iterations() { bool result = Managed.VerifyPassword( Utils.StringToUtf8ByteArray("password1"), - Utils.DecodeFromBase64("DQwDAAAAAQAKAAAAmH1BBckBJYDD0xfiwkAk1xwKgw8a57YQT0Igm+Faa9LFamTeEJgqn/qHc2R/8XEyK2iLPkVy+IErdGLLtLKJ2g==")); + Utils.DecodeFromBase64("DQwDAAAAAQAKAAAAmH1BBckBJYDD0xfiwkAk1xwKgw8a57YQT0Igm+Faa9LFamTeEJgqn/qHc2R/8XEyK2iLPkVy+IErdGLLtLKJ2g==")!); Assert.IsTrue(result); } @@ -148,14 +148,14 @@ public void VerifyPasswordV1_Iterations() public void SignatureV1() { byte[] data = Encoding.UTF8.GetBytes("this is a test"); - byte[] wrong_data = Encoding.UTF8.GetBytes("this is wrong"); + byte[] wrongData = Encoding.UTF8.GetBytes("this is wrong"); SigningPublicKey publicKey = SigningPublicKey.FromByteArray(Convert.FromBase64String("DQwFAAIAAQDeEvwlEigK5AXoTorhmlKP6+mbiUU2rYrVQ25JQ5xang==")); byte[] signature = Convert.FromBase64String("DQwGAAAAAQD82uRk4sFC8vEni6pDNw/vOdN1IEDg9cAVfprWJZ/JBls9Gi61cUt5u6uBJtseNGZFT7qKLvp4NUZrAOL8FH0K"); Assert.IsTrue(Managed.VerifySignature(data, publicKey, signature)); - Assert.IsFalse(Managed.VerifySignature(wrong_data, publicKey, signature)); + Assert.IsFalse(Managed.VerifySignature(wrongData, publicKey, signature)); } } } diff --git a/wrappers/csharp/tests/unit-tests/TestArgon2Parameters.cs b/wrappers/csharp/tests/unit-tests/TestArgon2Parameters.cs index 3082af3f..dcc19c14 100644 --- a/wrappers/csharp/tests/unit-tests/TestArgon2Parameters.cs +++ b/wrappers/csharp/tests/unit-tests/TestArgon2Parameters.cs @@ -15,7 +15,8 @@ public class TestArgon2Parameters [TestMethod] public void FromByteArray() { - Argon2Parameters parameters = Argon2Parameters.FromByteArray(Convert.FromBase64String(TestData.Argon2DefaultParametersb64)); + Argon2Parameters? parameters = Argon2Parameters.FromByteArray(Convert.FromBase64String(TestData.Argon2DefaultParametersb64)); + Assert.IsNotNull(parameters); Assert.IsTrue(parameters.Iterations == 2); Assert.IsTrue(parameters.Lanes == 1); Assert.IsTrue(parameters.Length == 32); diff --git a/wrappers/csharp/tests/unit-tests/TestLegacy.cs b/wrappers/csharp/tests/unit-tests/TestLegacy.cs index c537346e..db7a997d 100644 --- a/wrappers/csharp/tests/unit-tests/TestLegacy.cs +++ b/wrappers/csharp/tests/unit-tests/TestLegacy.cs @@ -3,7 +3,6 @@ namespace Devolutions.Crypto.Tests { using System; - using System.IO; using System.Security.Cryptography; using System.Text; @@ -17,11 +16,11 @@ public class TestLegacy [TestMethod] public void TestLegacyDecryptor() { - TestDecryptor legacy = new TestDecryptor(Utils.DecodeFromBase64("TErOq+UuM6AjF8SAUVmOWg==")); + TestDecryptor legacy = new TestDecryptor(Utils.DecodeFromBase64("TErOq+UuM6AjF8SAUVmOWg==")!); string base64Data = "TuOx/rB+iwGrxpdbhaRyc3kphNIb4feEWLzHZZF0+21OE7hnTcLf1JiuUxLoRR1+"; - byte[] data = Utils.DecodeFromBase64(base64Data); - byte[] key = Utils.DecodeFromBase64("5toYYi+R4MH/ZV1W0dCQ2C8xRYtgwFrmIR2qfEQRP6k="); + byte[] data = Utils.DecodeFromBase64(base64Data)!; + byte[] key = Utils.DecodeFromBase64("5toYYi+R4MH/ZV1W0dCQ2C8xRYtgwFrmIR2qfEQRP6k=")!; Assert.IsTrue(CompareArrays(Encoding.UTF8.GetBytes("A test Ciphertext!"), Managed.Decrypt(data, key, legacyDecryptor: legacy))); Assert.IsTrue(CompareArrays(Encoding.UTF8.GetBytes("A test Ciphertext!"), Managed.DecryptWithKey(data, key, legacyDecryptor: legacy))); @@ -34,11 +33,11 @@ public void TestLegacyDecryptor() [TestMethod] public void TestDecryptionWithLegacyDecryptor() { - TestDecryptor legacy = new TestDecryptor(Utils.DecodeFromBase64("TErOq+UuM6AjF8SAUVmOWg==")); + TestDecryptor legacy = new TestDecryptor(Utils.DecodeFromBase64("TErOq+UuM6AjF8SAUVmOWg==")!); string base64Data = "DQwCAAEAAgDiTIrZApcji3I3pDfBADJ6sMa+iXfpdxRwIf7RHot0XNqOCLv5BlMi5RzezdHl+5NYBwvm//SDomwk"; - byte[] data = Utils.DecodeFromBase64(base64Data); - byte[] key = Utils.DecodeFromBase64("XCF4aJBny9LHFmUBt8zha5O2oOVttykWKrmUl4ujlVg="); + byte[] data = Utils.DecodeFromBase64(base64Data)!; + byte[] key = Utils.DecodeFromBase64("XCF4aJBny9LHFmUBt8zha5O2oOVttykWKrmUl4ujlVg=")!; Assert.IsTrue(CompareArrays(Encoding.UTF8.GetBytes("A test Ciphertext!"), Managed.Decrypt(data, key, legacyDecryptor: legacy))); Assert.IsTrue(CompareArrays(Encoding.UTF8.GetBytes("A test Ciphertext!"), Managed.DecryptWithKey(data, key, legacyDecryptor: legacy))); @@ -51,8 +50,8 @@ public void TestDecryptionWithLegacyDecryptor() [TestMethod] public void TestLegacyHasher() { - byte[] hash = Utils.DecodeFromBase64("fjpYZE/4RowWaTEIwZ7VODtsmMfMvCtqWUSVq7N6NFPsT/dbl3sjhnBmUELhiNfdfX3CZNLbg8NwiWy3cWgLeQ=="); - byte[] salt = Utils.DecodeFromBase64("JOF9bCSdcNWf6mBZbZ0Vulw7+huwUXVC1rulPMT4XQy/riMI6UbHSDJR11LWokHoctPueavXQfRlD1Xfn0sdwQ=="); + byte[] hash = Utils.DecodeFromBase64("fjpYZE/4RowWaTEIwZ7VODtsmMfMvCtqWUSVq7N6NFPsT/dbl3sjhnBmUELhiNfdfX3CZNLbg8NwiWy3cWgLeQ==")!; + byte[] salt = Utils.DecodeFromBase64("JOF9bCSdcNWf6mBZbZ0Vulw7+huwUXVC1rulPMT4XQy/riMI6UbHSDJR11LWokHoctPueavXQfRlD1Xfn0sdwQ==")!; TestHasher legacy = new TestHasher(salt); @@ -63,8 +62,8 @@ public void TestLegacyHasher() [TestMethod] public void TestVerifyPasswordWithLegacyHasher() { - byte[] hash = Utils.DecodeFromBase64("DQwDAAAAAQAQJwAAhCyuG8U5NLG7Ik6jj1CiiiRXapGS6wBpbGXNJYrQQIVEqBRYhJbOj2MPB90LX1GKuYU4jWQkbof1nErbmcRGag=="); - byte[] salt = Utils.DecodeFromBase64("JOF9bCSdcNWf6mBZbZ0Vulw7+huwUXVC1rulPMT4XQy/riMI6UbHSDJR11LWokHoctPueavXQfRlD1Xfn0sdwQ=="); + byte[] hash = Utils.DecodeFromBase64("DQwDAAAAAQAQJwAAhCyuG8U5NLG7Ik6jj1CiiiRXapGS6wBpbGXNJYrQQIVEqBRYhJbOj2MPB90LX1GKuYU4jWQkbof1nErbmcRGag==")!; + byte[] salt = Utils.DecodeFromBase64("JOF9bCSdcNWf6mBZbZ0Vulw7+huwUXVC1rulPMT4XQy/riMI6UbHSDJR11LWokHoctPueavXQfRlD1Xfn0sdwQ==")!; TestHasher legacy = new TestHasher(salt); @@ -72,7 +71,7 @@ public void TestVerifyPasswordWithLegacyHasher() Assert.IsFalse(Managed.VerifyPassword(Encoding.UTF8.GetBytes("Wrong password!"), hash, legacy)); } - private static bool CompareArrays(byte[] a1, byte[] a2) + private static bool CompareArrays(byte[]? a1, byte[]? a2) { if (a1 == null || a2 == null || a1.Length != a2.Length) { diff --git a/wrappers/csharp/tests/unit-tests/TestManaged.cs b/wrappers/csharp/tests/unit-tests/TestManaged.cs index 1902b19f..2624523b 100644 --- a/wrappers/csharp/tests/unit-tests/TestManaged.cs +++ b/wrappers/csharp/tests/unit-tests/TestManaged.cs @@ -16,22 +16,22 @@ public class TestManaged [TestMethod] public void ByteArrayToString() { - string conversionResult = Utils.ByteArrayToUtf8String(TestData.BytesTestData); + string? conversionResult = Utils.ByteArrayToUtf8String(TestData.BytesTestData); Assert.AreEqual(conversionResult, TestData.StringTestData); } [TestMethod] public void Decode() { - byte[] data = Utils.DecodeFromBase64(TestData.Base64TestData); + byte[] data = Utils.DecodeFromBase64(TestData.Base64TestData)!; Assert.AreEqual(Convert.ToBase64String(data), Convert.ToBase64String(TestData.BytesTestData)); } [TestMethod] public void Decrypt() { - byte[] decryptResult = Managed.Decrypt(TestData.EncryptedData, TestData.BytesTestKey); - string encodedByteArrayToUtf8String = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? decryptResult = Managed.Decrypt(TestData.EncryptedData, TestData.BytesTestKey); + string? encodedByteArrayToUtf8String = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(encodedByteArrayToUtf8String, TestData.Base64TestData); } @@ -39,35 +39,34 @@ public void Decrypt() public void DecryptAsymmetric_Test() { byte[] encryptedData = Convert.FromBase64String("DQwCAAIAAgD5rUXkPQO55rzI69WSxtVTA43lDXougn6BxJ7evqf+Yq+SEGXZxpE49874fz/aEk39LTnh1yWnY2VNoAAqKVB5CWZryd6SSld8Sx8v"); + byte[]? decryptedData = Managed.DecryptAsymmetric(encryptedData, TestData.AlicePrivateKey); - byte[] decryptedData = Managed.DecryptAsymmetric(encryptedData, TestData.AlicePrivateKey); - - Assert.IsTrue(decryptedData != null); - Assert.IsTrue(Encoding.UTF8.GetString(decryptedData) == "test"); + Assert.IsNotNull(decryptedData); + Assert.AreEqual(Encoding.UTF8.GetString(decryptedData), "test"); } [TestMethod] public void DecryptWithKey() { - byte[] decryptResult = Managed.DecryptWithKey(TestData.EncryptedData, TestData.BytesTestKey); - string decryptResultString = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? decryptResult = Managed.DecryptWithKey(TestData.EncryptedData, TestData.BytesTestKey); + string? decryptResultString = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultString, TestData.Base64TestData); } [TestMethod] public void DecryptWithKeyAsUtf8String() { - string decryptResultString = Managed.DecryptWithKeyAsUtf8String(TestData.EncryptedData, TestData.BytesTestKey); + string? decryptResultString = Managed.DecryptWithKeyAsUtf8String(TestData.EncryptedData, TestData.BytesTestKey); Assert.AreEqual(decryptResultString, TestData.Base64TestData); } [TestMethod] public void DecryptWithPassword2() { - string encrytedDataAsBase64 = "DQwCAAAAAgDsQkLRs1I3054gNOYP7ifVSpOMFEV8vTfoMuZOWAzbMR2b1QLyIe0/NFNKr8rniijd8PxHv29N"; + string encryptedDataAsBase64 = "DQwCAAAAAgDsQkLRs1I3054gNOYP7ifVSpOMFEV8vTfoMuZOWAzbMR2b1QLyIe0/NFNKr8rniijd8PxHv29N"; string password = "testPa$$"; - byte[] decryptResult = Managed.DecryptWithPassword(encrytedDataAsBase64, password); - string decryptResultString = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? decryptResult = Managed.DecryptWithPassword(encryptedDataAsBase64, password); + string? decryptResultString = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultString, "test Ciph3rtext"); } @@ -76,10 +75,10 @@ public void DecryptWithPassword2_5() { try { - string encrytedDataAsBase64 = "DQwCAAAAAgDutPWBLPHG0+ocNw+Yzs6xygGOeOlNPOAjbYDdbJKjPRnEP8HuDN7Y3h3dCoH81Szf3tCf3mNf"; + string encryptedDataAsBase64 = "DQwCAAAAAgDutPWBLPHG0+ocNw+Yzs6xygGOeOlNPOAjbYDdbJKjPRnEP8HuDN7Y3h3dCoH81Szf3tCf3mNf"; string password = "testPa$$"; - byte[] decryptResult = Managed.DecryptWithPassword(encrytedDataAsBase64, password); - string decryptResultString = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? decryptResult = Managed.DecryptWithPassword(encryptedDataAsBase64, password); + string? decryptResultString = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultString, "test Ciph3rtext"); } catch (Exception ex) @@ -93,7 +92,7 @@ public void DecryptWithPassword2_5() public void DecryptWithPasswordAsUtf8String() { string encryptedDataAsBase64 = "DQwCAAAAAgCoE9Y3m06QaPSAiL2qegthcm0+zZWt4fXbdqcefkzD6y8pnWsMzLkx/32t"; - string decryptResultString = Managed.DecryptWithPasswordAsUtf8String(encryptedDataAsBase64, TestData.TestPassword); + string? decryptResultString = Managed.DecryptWithPasswordAsUtf8String(encryptedDataAsBase64, TestData.TestPassword); Assert.AreEqual(decryptResultString, TestData.StringTestData); } @@ -107,7 +106,7 @@ public void DerivePassword() [TestMethod] public void Encode() { - string encodedArrayToBase64String = Utils.EncodeToBase64String(TestData.BytesTestData); + string? encodedArrayToBase64String = Utils.EncodeToBase64String(TestData.BytesTestData); Assert.AreEqual(encodedArrayToBase64String, TestData.Base64TestData); } @@ -115,11 +114,11 @@ public void Encode() public void Encrypt() { byte[] base64DataAsUtf8ByteArray = Utils.StringToUtf8ByteArray(TestData.Base64TestData2); - byte[] encryptResult = Managed.Encrypt(base64DataAsUtf8ByteArray, TestData.BytesTestKey); + byte[]? encryptResult = Managed.Encrypt(base64DataAsUtf8ByteArray, TestData.BytesTestKey); Assert.IsTrue(Utils.ValidateHeader(encryptResult, DataType.Cipher)); - byte[] decryptResult = Managed.Decrypt(encryptResult, TestData.BytesTestKey); - var decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? decryptResult = Managed.Decrypt(encryptResult, TestData.BytesTestKey); + string? decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultAsUtf8String, TestData.Base64TestData2); } @@ -127,28 +126,28 @@ public void Encrypt() public void EncryptAsymmetric_Test() { byte[] dataToEncrypt = Encoding.UTF8.GetBytes("test"); + byte[]? encryptedData = Managed.EncryptAsymmetric(dataToEncrypt, TestData.AlicePublicKey); - byte[] encryptedData = Managed.EncryptAsymmetric(dataToEncrypt, TestData.AlicePublicKey); - - Assert.IsTrue(encryptedData != null); - Assert.IsTrue(encryptedData.Length == 84); + Assert.IsNotNull(encryptedData); + Assert.AreEqual(encryptedData.Length, 84); } [TestMethod] public void EncryptBase64WithPassword() { - byte[] encryptedData = Managed.EncryptBase64WithPassword(TestData.Base64TestData, TestData.TestPassword); + byte[]? encryptedData = Managed.EncryptBase64WithPassword(TestData.Base64TestData, TestData.TestPassword); Assert.IsTrue(Utils.ValidateHeader(encryptedData, DataType.Cipher)); - byte[] decryptResult = Managed.DecryptWithPassword(encryptedData, TestData.TestPassword); - string decryptResultString = Utils.ByteArrayToUtf8String(decryptResult); + + byte[]? decryptResult = Managed.DecryptWithPassword(encryptedData, TestData.TestPassword); + string? decryptResultString = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultString, TestData.StringTestData); } [TestMethod] public void EncryptBase64WithPasswordAsString() { - string encryptResultString = Managed.EncryptBase64WithPasswordAsString(TestData.Base64TestData, TestData.TestPassword); - string decryptResultAsUtf8String = Managed.DecryptWithPasswordAsUtf8String(encryptResultString, TestData.TestPassword); + string? encryptResultString = Managed.EncryptBase64WithPasswordAsString(TestData.Base64TestData, TestData.TestPassword); + string? decryptResultAsUtf8String = Managed.DecryptWithPasswordAsUtf8String(encryptResultString, TestData.TestPassword); Assert.AreEqual(decryptResultAsUtf8String, TestData.StringTestData); } @@ -157,8 +156,10 @@ public void EncryptDecryptWithKeyAsBase64String() { byte[] encodedData = Utils.StringToUtf8ByteArray(TestData.StringTestData); byte[] encodedPassword = Utils.StringToUtf8ByteArray(TestData.TestPassword); - string encryptResultAsBase64String = Managed.EncryptWithKeyAsBase64String(encodedData, encodedPassword); - string decryptResult = Managed.DecryptWithKeyAsUtf8String(encryptResultAsBase64String, encodedPassword); + string? encryptResultAsBase64String = Managed.EncryptWithKeyAsBase64String(encodedData, encodedPassword); + Assert.IsNotNull(encryptResultAsBase64String); + + string? decryptResult = Managed.DecryptWithKeyAsUtf8String(encryptResultAsBase64String, encodedPassword); Assert.AreEqual(decryptResult, TestData.StringTestData); } @@ -167,9 +168,11 @@ public void EncryptWithKeyDecryptWithKey() { byte[] encodedData = Utils.StringToUtf8ByteArray(TestData.StringTestData); byte[] encodedPassword = Utils.StringToUtf8ByteArray(TestData.TestPassword); - byte[] encryptResultArray = Managed.EncryptWithKey(encodedData, encodedPassword); - byte[] decryptResult = Managed.DecryptWithKey(encryptResultArray, encodedPassword); - string decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); + byte[]? encryptResultArray = Managed.EncryptWithKey(encodedData, encodedPassword); + Assert.IsNotNull(encryptResultArray); + + byte[]? decryptResult = Managed.DecryptWithKey(encryptResultArray, encodedPassword); + string? decryptResultAsUtf8String = Utils.ByteArrayToUtf8String(decryptResult); Assert.AreEqual(decryptResultAsUtf8String, TestData.StringTestData); } @@ -177,8 +180,8 @@ public void EncryptWithKeyDecryptWithKey() public void EncryptWithPasswordAsBase64String() { byte[] encodedDataAsUtf8ByteArray = Utils.StringToUtf8ByteArray(TestData.StringTestData); - string encryptResultAsBase64String = Managed.EncryptWithPasswordAsBase64String(encodedDataAsUtf8ByteArray, TestData.TestPassword); - string decryptResultAsUtf8String = Managed.DecryptWithPasswordAsUtf8String(encryptResultAsBase64String, TestData.TestPassword); + string? encryptResultAsBase64String = Managed.EncryptWithPasswordAsBase64String(encodedDataAsUtf8ByteArray, TestData.TestPassword); + string? decryptResultAsUtf8String = Managed.DecryptWithPasswordAsUtf8String(encryptResultAsBase64String, TestData.TestPassword); Assert.AreEqual(decryptResultAsUtf8String, TestData.StringTestData); } @@ -187,8 +190,8 @@ public void EncryptDecryptWithPasswordAsBase64String() { byte[] base64EncodedToUtf8ByteArray = Utils.StringToUtf8ByteArray(TestData.Base64TestData); string password = "pwd"; - string encryptResultAsBase64String = Managed.EncryptWithPasswordAsBase64String(base64EncodedToUtf8ByteArray, password, 100); - string decryptionResultAsUtf8String = Managed.DecryptWithPasswordAsUtf8String(encryptResultAsBase64String, password, 100); + string? encryptResultAsBase64String = Managed.EncryptWithPasswordAsBase64String(base64EncodedToUtf8ByteArray, password, 100); + string? decryptionResultAsUtf8String = Managed.DecryptWithPasswordAsUtf8String(encryptResultAsBase64String, password, 100); Assert.AreEqual(decryptionResultAsUtf8String, TestData.Base64TestData); } @@ -224,10 +227,10 @@ public void GenerateSigningKeyPair() public void Sign() { SigningKeyPair keypair = SigningKeyPair.FromByteArray(Convert.FromBase64String(TestData.SigningKeyPairb64)); - byte[] data = System.Text.Encoding.UTF8.GetBytes(TestData.SignTesting); - - byte[] signature = Managed.Sign(data, keypair); + byte[] data = Encoding.UTF8.GetBytes(TestData.SignTesting); + byte[]? signature = Managed.Sign(data, keypair); + Assert.IsNotNull(signature); Assert.AreEqual(Convert.ToBase64String(signature), TestData.SignedTestingb64); } @@ -239,7 +242,7 @@ public void VerifySignature() SigningKeyPair keypair = SigningKeyPair.FromByteArray(Convert.FromBase64String(TestData.SigningKeyPairb64)); SigningPublicKey pubkey = SigningPublicKey.FromByteArray(Convert.FromBase64String(TestData.SigningPublicKeyb64)); - bool res = Managed.VerifySignature(System.Text.Encoding.UTF8.GetBytes(TestData.SignTesting), pubkey, signature); + bool res = Managed.VerifySignature(Encoding.UTF8.GetBytes(TestData.SignTesting), pubkey, signature); Assert.AreEqual(res, true); } @@ -252,7 +255,7 @@ public void VerifySignature_FailBadData() SigningKeyPair keypair = SigningKeyPair.FromByteArray(Convert.FromBase64String(TestData.SigningKeyPairb64)); SigningPublicKey pubkey = SigningPublicKey.FromByteArray(Convert.FromBase64String(TestData.SigningPublicKeyb64)); - bool res = Managed.VerifySignature(System.Text.Encoding.UTF8.GetBytes("bad data"), pubkey, signature); + bool res = Managed.VerifySignature(Encoding.UTF8.GetBytes("bad data"), pubkey, signature); Assert.AreEqual(res, false); } @@ -265,7 +268,7 @@ public void VerifySignature_FailBadKey() SigningKeyPair keypair = Managed.GenerateSigningKeyPair(); SigningPublicKey pubkey = keypair.GetPublicKey(); - bool res = Managed.VerifySignature(System.Text.Encoding.UTF8.GetBytes(TestData.SignTesting), pubkey, signature); + bool res = Managed.VerifySignature(Encoding.UTF8.GetBytes(TestData.SignTesting), pubkey, signature); Assert.AreEqual(res, false); } @@ -274,11 +277,12 @@ public void VerifySignature_FailBadKey() public void VerifySignature_FailBadSignature() { SigningKeyPair keypair = Managed.GenerateSigningKeyPair(); - byte[] signature = Managed.Sign(System.Text.Encoding.UTF8.GetBytes(TestData.SignTesting), keypair); + byte[]? signature = Managed.Sign(Encoding.UTF8.GetBytes(TestData.SignTesting), keypair); + Assert.IsNotNull(signature); SigningPublicKey pubkey = SigningPublicKey.FromByteArray(Convert.FromBase64String(TestData.SigningPublicKeyb64)); - bool res = Managed.VerifySignature(System.Text.Encoding.UTF8.GetBytes(TestData.SignTesting), pubkey, signature); + bool res = Managed.VerifySignature(Encoding.UTF8.GetBytes(TestData.SignTesting), pubkey, signature); Assert.AreEqual(res, false); } @@ -290,7 +294,7 @@ public void GenerateSharedKey() const int secretLength = 10; const int threshold = 3; var result = Managed.GenerateSharedKey(nbShares, threshold, secretLength); - Assert.IsTrue(result != null && result.Length == 5 && result[0].Length == 20); + Assert.IsTrue(result is { Length: 5 } && result[0].Length == 20); } [TestMethod] @@ -332,13 +336,13 @@ public void JoinShares() var shares = GetSharesKeys(); var result = Managed.JoinShares(shares); var val = Utils.ByteArrayToUtf8String(result); - Assert.IsTrue(result != null && result.Length == 10); + Assert.IsTrue(result is { Length: 10 }); var shares2 = GetSharesKeys2(); var result2 = Managed.JoinShares(shares2); var val2 = Utils.ByteArrayToUtf8String(result2); - Assert.IsTrue(result2 != null && result2.Length == 10); + Assert.IsTrue(result2 is { Length: 10 }); Assert.AreEqual(val, val2); } @@ -360,7 +364,7 @@ public void StringToByteArray() [TestMethod] public void ToBase64String() { - string dataEncodedToBase64String = Utils.EncodeToBase64String(TestData.BytesTestData); + string? dataEncodedToBase64String = Utils.EncodeToBase64String(TestData.BytesTestData); Assert.AreEqual(dataEncodedToBase64String, TestData.Base64TestData); } diff --git a/wrappers/csharp/tests/unit-tests/TestStreams.cs b/wrappers/csharp/tests/unit-tests/TestStreams.cs index 88d40edd..9f0d1b1b 100644 --- a/wrappers/csharp/tests/unit-tests/TestStreams.cs +++ b/wrappers/csharp/tests/unit-tests/TestStreams.cs @@ -12,10 +12,10 @@ public class TestStreams [TestMethod] public void EncryptStream() { - byte[] base64DataAsUtf8ByteArray = Utils.Base64StringToByteArray(TestData.Base64TestDataStream); + byte[] base64DataAsUtf8ByteArray = Utils.Base64StringToByteArray(TestData.Base64TestDataStream)!; using MemoryStream ms = new MemoryStream(); - using (EncryptionStream ec = new EncryptionStream(TestData.BytesTestKey, Array.Empty(), 1000, false, 0, ms)) + using (EncryptionStream ec = new EncryptionStream(TestData.BytesTestKey, [], 1000, false, 0, ms)) { byte[] header = ec.GetHeader(); @@ -35,13 +35,13 @@ public void EncryptStream() [TestMethod] public void DecryptStream() { - byte[] base64DataAsUtf8ByteArray = Utils.Base64StringToByteArray(TestData.Base64TestDataStreamEncrypted); + byte[] base64DataAsUtf8ByteArray = Utils.Base64StringToByteArray(TestData.Base64TestDataStreamEncrypted)!; using MemoryStream ms = new MemoryStream(); - byte[] header = Utils.Base64StringToByteArray((TestData.Base64HeaderDataStreamEncrypted)); + byte[] header = Utils.Base64StringToByteArray(TestData.Base64HeaderDataStreamEncrypted)!; using (DecryptionStream ec = - new DecryptionStream(TestData.BytesTestKey, Array.Empty(), header, false, ms, false)) + new DecryptionStream(TestData.BytesTestKey, [], header, false, ms, false)) { Assert.IsTrue(Utils.ValidateHeader(header, DataType.OnlineCiphertext)); diff --git a/wrappers/csharp/tests/unit-tests/TestUtils.cs b/wrappers/csharp/tests/unit-tests/TestUtils.cs index 578f71f6..125e3404 100644 --- a/wrappers/csharp/tests/unit-tests/TestUtils.cs +++ b/wrappers/csharp/tests/unit-tests/TestUtils.cs @@ -16,7 +16,8 @@ public class TestUtils [TestMethod] public void Base64StringToByteArray() { - byte[] data = Utils.Base64StringToByteArray(TestData.Base64TestData); + byte[]? data = Utils.Base64StringToByteArray(TestData.Base64TestData); + Assert.IsNotNull(data); Assert.AreEqual(Convert.ToBase64String(data), Convert.ToBase64String(TestData.BytesTestData)); } @@ -24,50 +25,55 @@ public void Base64StringToByteArray() public void ByteArrayToString() { byte[] data = new byte[] { 0x51, 0x55, 0x4a, 0x44 }; - string dataToUtf8String = Utils.ByteArrayToUtf8String(data); + string? dataToUtf8String = Utils.ByteArrayToUtf8String(data); Assert.AreEqual(dataToUtf8String, TestData.Base64TestData); } [TestMethod] public void ConstantTimeEqual() { - byte[] x = { 0, 1, 2 }; - byte[] y = { 4, 5, 6 }; - byte[] z = { 0, 1, 2, 3 }; + byte[] x = [0, 1, 2]; + byte[] y = [4, 5, 6]; + byte[] z = [0, 1, 2, 3]; Assert.IsTrue(Utils.ConstantTimeEquals(x, x)); - Assert.IsTrue(!Utils.ConstantTimeEquals(x, y)); - Assert.IsTrue(!Utils.ConstantTimeEquals(x, z)); - Assert.IsTrue(!Utils.ConstantTimeEquals(y, x)); + Assert.IsFalse(Utils.ConstantTimeEquals(x, y)); + Assert.IsFalse(Utils.ConstantTimeEquals(x, z)); + Assert.IsFalse(Utils.ConstantTimeEquals(y, x)); Assert.IsTrue(Utils.ConstantTimeEquals(y, y)); - Assert.IsTrue(!Utils.ConstantTimeEquals(y, z)); - Assert.IsTrue(!Utils.ConstantTimeEquals(z, x)); - Assert.IsTrue(!Utils.ConstantTimeEquals(z, y)); + Assert.IsFalse(Utils.ConstantTimeEquals(y, z)); + Assert.IsFalse(Utils.ConstantTimeEquals(z, x)); + Assert.IsFalse(Utils.ConstantTimeEquals(z, y)); Assert.IsTrue(Utils.ConstantTimeEquals(z, z)); } [TestMethod] public void Decode() { - byte[] decodedData = Utils.DecodeFromBase64(TestData.Base64TestData); + byte[]? decodedData = Utils.DecodeFromBase64(TestData.Base64TestData); + Assert.IsNotNull(decodedData); Assert.AreEqual(Convert.ToBase64String(decodedData), Convert.ToBase64String(TestData.BytesTestData)); - byte[] decodeDataNoPad = Utils.DecodeFromBase64(TestData.Base64TestDataNoPad); + byte[]? decodeDataNoPad = Utils.DecodeFromBase64(TestData.Base64TestDataNoPad); + Assert.IsNotNull(decodeDataNoPad); Assert.AreEqual(Encoding.UTF8.GetString(decodeDataNoPad), TestData.StringTestDataNoPad); - byte[] decodeDataInvalid = Utils.DecodeFromBase64("===="); + byte[]? decodeDataInvalid = Utils.DecodeFromBase64("===="); Assert.IsNull(decodeDataInvalid); - byte[] decodeDataInvalid2 = Utils.DecodeFromBase64("="); + byte[]? decodeDataInvalid2 = Utils.DecodeFromBase64("="); Assert.IsNull(decodeDataInvalid2); } [TestMethod] public void DecodeUrl() { - byte[] d1 = Utils.DecodeFromBase64Url(TestData.Base64Url1); - byte[] d2 = Utils.DecodeFromBase64Url(TestData.Base64Url2); - byte[] d3 = Utils.DecodeFromBase64Url(TestData.Base64Url3); + byte[]? d1 = Utils.DecodeFromBase64Url(TestData.Base64Url1); + byte[]? d2 = Utils.DecodeFromBase64Url(TestData.Base64Url2); + byte[]? d3 = Utils.DecodeFromBase64Url(TestData.Base64Url3); + Assert.IsNotNull(d1); + Assert.IsNotNull(d2); + Assert.IsNotNull(d3); Assert.AreEqual(Convert.ToBase64String(d1), Convert.ToBase64String(TestData.Base64UrlBytes1)); Assert.AreEqual(Convert.ToBase64String(d2), Convert.ToBase64String(TestData.Base64UrlBytes2)); Assert.AreEqual(Convert.ToBase64String(d3), Convert.ToBase64String(TestData.Base64UrlBytes3)); @@ -76,16 +82,16 @@ public void DecodeUrl() [TestMethod] public void Encode() { - string y = Utils.EncodeToBase64String(TestData.BytesTestData); + string? y = Utils.EncodeToBase64String(TestData.BytesTestData); Assert.AreEqual(y, TestData.Base64TestData); } [TestMethod] public void EncodeUrl() { - string e1 = Utils.EncodeToBase64UrlString(TestData.Base64UrlBytes1); - string e2 = Utils.EncodeToBase64UrlString(TestData.Base64UrlBytes2); - string e3 = Utils.EncodeToBase64UrlString(TestData.Base64UrlBytes3); + string? e1 = Utils.EncodeToBase64UrlString(TestData.Base64UrlBytes1); + string? e2 = Utils.EncodeToBase64UrlString(TestData.Base64UrlBytes2); + string? e3 = Utils.EncodeToBase64UrlString(TestData.Base64UrlBytes3); Assert.AreEqual(e1, TestData.Base64Url1); Assert.AreEqual(e2, TestData.Base64Url2); Assert.AreEqual(e3, TestData.Base64Url3); @@ -102,8 +108,13 @@ public void GetDecodedLength() Assert.IsTrue(Utils.GetDecodedBase64StringLength(null) == this.GetDotNetBase64Length(null)); } - public int GetDotNetBase64Length(string base64) + public int GetDotNetBase64Length(string? base64) { + if (base64 == null) + { + return 0; + } + try { byte[] test = Convert.FromBase64String(base64); @@ -129,7 +140,7 @@ public void ScryptSimple() byte[] password = Utils.StringToUtf8ByteArray(TestData.TestPassword); byte[] salt = TestData.Salt; - string hash = Utils.ScryptSimple(password, salt, 10, 8, 1); + string? hash = Utils.ScryptSimple(password, salt, 10, 8, 1); Assert.AreEqual(TestData.ScryptHash, hash); } @@ -153,7 +164,7 @@ public void ValidateSignature() byte[] dataToEncrypt = Utils.StringToUtf8ByteArray(TestData.Base64TestData); byte[] password = Utils.StringToUtf8ByteArray(TestData.TestPassword); - byte[] encryptResult = Managed.Encrypt(dataToEncrypt, password); + byte[]? encryptResult = Managed.Encrypt(dataToEncrypt, password); Assert.IsFalse(Utils.ValidateHeader(dataToEncrypt, DataType.Cipher)); Assert.IsTrue(Utils.ValidateHeader(encryptResult, DataType.Cipher)); @@ -176,7 +187,7 @@ public void ValidateSignatureFromStream_ClosedStream() stream.Close(); - DevolutionsCryptoException exception = null; + DevolutionsCryptoException? exception = null; try { @@ -187,6 +198,9 @@ public void ValidateSignatureFromStream_ClosedStream() exception = ex; } + Assert.IsNotNull(exception); + Assert.IsNotNull(exception.ManagedException); + // Xamarin has a space between Cannot (Can not) bool validException = exception.ManagedException.Message.Contains("Cannot access a closed Stream.") || exception.ManagedException.Message.Contains("Can not access a closed Stream."); @@ -212,7 +226,7 @@ public void ValidateSignatureFromStream_UnReadableStream() { Stream stream = new UnReadableStream(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); - DevolutionsCryptoException exception = null; + DevolutionsCryptoException? exception = null; try { @@ -223,6 +237,7 @@ public void ValidateSignatureFromStream_UnReadableStream() exception = ex; } + Assert.IsNotNull(exception); Assert.AreEqual(exception.ManagedError, ManagedError.CanNotReadStream); } @@ -231,7 +246,7 @@ public void ValidateSignatureFromStream_UnseekableStream() { Stream stream = new UnSeekableStream(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); - DevolutionsCryptoException exception = null; + DevolutionsCryptoException? exception = null; try { @@ -242,6 +257,7 @@ public void ValidateSignatureFromStream_UnseekableStream() exception = ex; } + Assert.IsNotNull(exception); Assert.AreEqual(exception.ManagedError, ManagedError.CanNotSeekStream); } diff --git a/wrappers/csharp/tests/unit-tests/local/devolutions-crypto-tests/devolutions-crypto-tests.csproj b/wrappers/csharp/tests/unit-tests/local/devolutions-crypto-tests/devolutions-crypto-tests.csproj index 4667dbcd..5856e817 100644 --- a/wrappers/csharp/tests/unit-tests/local/devolutions-crypto-tests/devolutions-crypto-tests.csproj +++ b/wrappers/csharp/tests/unit-tests/local/devolutions-crypto-tests/devolutions-crypto-tests.csproj @@ -2,6 +2,7 @@ net9.0 true + enable