Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion cas-core-lib
Submodule cas-core-lib updated 2 files
+5 −0 Cargo.toml
+58 −0 build.rs
18 changes: 0 additions & 18 deletions cas-dotnet-sdk/Asymmetric/Linux/RSALinuxWrapper.cs

This file was deleted.

75 changes: 22 additions & 53 deletions cas-dotnet-sdk/Asymmetric/RSAWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,41 @@
using CasDotnetSdk.Asymmetric.Linux;
using CasCoreLib;
using CasDotnetSdk.Asymmetric.Types;
using CasDotnetSdk.Asymmetric.Windows;

using CasDotnetSdk.Helpers;
using CasDotnetSdk.Helpers.Types;

using System;
using System.Runtime.InteropServices;

namespace CasDotnetSdk.Asymmetric
{
public class RSAWrapper : BaseWrapper
public unsafe class RSAWrapper : BaseWrapper
{
/// <summary>
/// A wrapper class for RSA key creation, encryption, decryption, signing, and verification.
/// </summary>
public RSAWrapper()
{

}

/// <summary>
/// Signs data with an RSA private key.
/// </summary>
/// <param name="privateKey"></param>
/// <param name="dataToSign"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
///

public byte[] Sign(string privateKey, byte[] dataToSign)
{
if (dataToSign == null || dataToSign.Length == 0)
{
throw new Exception("You must provide allocated data to sign with RSA");
}

RsaSignBytesResults signResult = (this._platform == OSPlatform.Linux) ?
RSALinuxWrapper.rsa_sign_with_key_bytes(privateKey, dataToSign, dataToSign.Length) :
RSAWindowsWrapper.rsa_sign_with_key_bytes(privateKey, dataToSign, dataToSign.Length); ;
CasErrorHandler.ThrowIfError(signResult.error_code, "RSA sign");
byte[] result = new byte[signResult.length];
Marshal.Copy(signResult.signature_raw_ptr, result, 0, (int)signResult.length);
FreeMemoryHelper.FreeBytesMemory(signResult.signature_raw_ptr);


return result;
fixed (byte* privateKeyPtr = NativeString.ToCString(privateKey))
fixed (byte* dataPtr = NativePin.Of(dataToSign))
{
RsaSignBytesResults signResult = NativeMethods.rsa_sign_with_key_bytes(privateKeyPtr, dataPtr, (nuint)dataToSign.Length);
CasErrorHandler.ThrowIfError(signResult.error_code, "RSA sign");
return NativeByteBuffer.CopyAndFree(signResult.signature_raw_ptr, signResult.length);
}
}


/// <summary>
/// Verifies data with an RSA public key.
/// </summary>
/// <param name="publicKey"></param>
/// <param name="dataToVerify"></param>
/// <param name="signature"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
///

public bool Verify(string publicKey, byte[] dataToVerify, byte[] signature)
{
if (dataToVerify == null || dataToVerify.Length == 0)
Expand All @@ -70,41 +47,33 @@ public bool Verify(string publicKey, byte[] dataToVerify, byte[] signature)
throw new Exception("You must provide an allocated signature to verify with RSA");
}

CasVerifyResult result = (this._platform == OSPlatform.Linux) ?
RSALinuxWrapper.rsa_verify_bytes(publicKey, dataToVerify, dataToVerify.Length, signature, signature.Length) :
RSAWindowsWrapper.rsa_verify_bytes(publicKey, dataToVerify, dataToVerify.Length, signature, signature.Length);
CasErrorHandler.ThrowIfError(result.error_code, "RSA verify");


return result.is_valid;
fixed (byte* publicKeyPtr = NativeString.ToCString(publicKey))
fixed (byte* dataPtr = NativePin.Of(dataToVerify))
fixed (byte* signaturePtr = NativePin.Of(signature))
{
CasVerifyResult result = NativeMethods.rsa_verify_bytes(publicKeyPtr, dataPtr, (nuint)dataToVerify.Length, signaturePtr, (nuint)signature.Length);
CasErrorHandler.ThrowIfError(result.error_code, "RSA verify");
return result.is_valid;
}
}

/// <summary>
/// Generates an RSA key based on the key size provided. (1024, 2048, 4096)
/// </summary>
/// <param name="keySize"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>

public RsaKeyPairResult GetKeyPair(int keySize)
{
if (keySize != 1024 && keySize != 2048 && keySize != 4096)
{
throw new Exception("Please pass in a valid key size.");
}

RsaKeyPairStruct keyPairStruct = (this._platform == OSPlatform.Linux) ?
RSALinuxWrapper.get_key_pair(keySize) :
RSAWindowsWrapper.get_key_pair(keySize);
CasErrorHandler.ThrowIfError(keyPairStruct.error_code, "RSA key pair generation");
RsaKeyPairResult result = new RsaKeyPairResult()
RsaKeyPair keyPair = NativeMethods.get_key_pair((nuint)keySize);
CasErrorHandler.ThrowIfError(keyPair.error_code, "RSA key pair generation");
return new RsaKeyPairResult()
{
PrivateKey = Marshal.PtrToStringAnsi(keyPairStruct.priv_key),
PublicKey = Marshal.PtrToStringAnsi(keyPairStruct.pub_key)
PrivateKey = NativeString.ReadAndFree(keyPair.priv_key),
PublicKey = NativeString.ReadAndFree(keyPair.pub_key)
};
FreeMemoryHelper.FreeCStringMemory(keyPairStruct.pub_key);
FreeMemoryHelper.FreeCStringMemory(keyPairStruct.priv_key);
return result;
}
}
}
10 changes: 0 additions & 10 deletions cas-dotnet-sdk/Asymmetric/Types/RsaDecryptBytesResult.cs

This file was deleted.

10 changes: 0 additions & 10 deletions cas-dotnet-sdk/Asymmetric/Types/RsaEncryptBytesResult.cs

This file was deleted.

11 changes: 0 additions & 11 deletions cas-dotnet-sdk/Asymmetric/Types/RsaKeyPairStruct.cs

This file was deleted.

11 changes: 0 additions & 11 deletions cas-dotnet-sdk/Asymmetric/Types/RsaSignBytesResults.cs

This file was deleted.

10 changes: 0 additions & 10 deletions cas-dotnet-sdk/Asymmetric/Types/RsaSignResultStruct.cs

This file was deleted.

18 changes: 0 additions & 18 deletions cas-dotnet-sdk/Asymmetric/Windows/RSAWindowsWrapper.cs

This file was deleted.

14 changes: 0 additions & 14 deletions cas-dotnet-sdk/Compression/Linux/ZSTDLinuxWrapper.cs

This file was deleted.

11 changes: 0 additions & 11 deletions cas-dotnet-sdk/Compression/Types/ZSTDResult.cs

This file was deleted.

14 changes: 0 additions & 14 deletions cas-dotnet-sdk/Compression/Windows/ZSTDWindowsWrapper.cs

This file was deleted.

54 changes: 15 additions & 39 deletions cas-dotnet-sdk/Compression/ZSTDWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
using CasDotnetSdk.Compression.Linux;
using CasDotnetSdk.Compression.Types;
using CasDotnetSdk.Compression.Windows;

using CasCoreLib;
using CasDotnetSdk.Helpers;
using System;
using System.Runtime.InteropServices;

namespace CasDotnetSdk.Compression
{
public class ZSTDWrapper : BaseWrapper
public unsafe class ZSTDWrapper : BaseWrapper
{
public ZSTDWrapper()
{

}

/// <summary>
/// Datas to the byte array to compress and the level of encryption.
/// Zstandard (zstd) supports 22 compression levels, ranging from -22 to 22. Lower levels, such as 1–9,
/// Zstandard (zstd) supports 22 compression levels, ranging from -22 to 22. Lower levels, such as 1–9,
/// are faster but result in larger file sizes, while higher levels, such as 10–22, provide better compression ratios.
/// </summary>
/// <param name="data"></param>
/// <param name="level"></param>
/// <returns></returns>
///

public byte[] Compress(byte[] data, int level)
{
if (data == null || data.Length == 0)
Expand All @@ -36,45 +26,31 @@ public byte[] Compress(byte[] data, int level)
throw new Exception("Please pass in a valid level for ZSTD Compression");
}


ZSTDResult compressResult = (this._platform == OSPlatform.Linux) ?
ZSTDLinuxWrapper.compress(data, data.Length, level) :
ZSTDWindowsWrapper.compress(data, data.Length, level);
CasErrorHandler.ThrowIfError(compressResult.error_code, "ZSTD compress");
byte[] result = new byte[compressResult.length];
Marshal.Copy(compressResult.data, result, 0, (int)compressResult.length);
FreeMemoryHelper.FreeBytesMemory(compressResult.data);


return result;
fixed (byte* dataPtr = NativePin.Of(data))
{
ZstdCompressResult compressResult = NativeMethods.compress(dataPtr, (nuint)data.Length, (nuint)level);
CasErrorHandler.ThrowIfError(compressResult.error_code, "ZSTD compress");
return NativeByteBuffer.CopyAndFree(compressResult.data, compressResult.length);
}
}

/// <summary>
/// Decompresses and previosuly compressed byte array with ZSTD.
/// No level is required to decompress.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
///

public byte[] Decompress(byte[] data)
{
if (data == null || data.Length == 0)
{
throw new Exception("Must pass in an allocated array of data to decompress");
}


ZSTDResult decompressResult = (this._platform == OSPlatform.Linux) ?
ZSTDLinuxWrapper.decompress(data, data.Length) :
ZSTDWindowsWrapper.decompress(data, data.Length);
CasErrorHandler.ThrowIfError(decompressResult.error_code, "ZSTD decompress");
byte[] result = new byte[decompressResult.length];
Marshal.Copy(decompressResult.data, result, 0, (int)decompressResult.length);
FreeMemoryHelper.FreeBytesMemory(decompressResult.data);


return result;
fixed (byte* dataPtr = NativePin.Of(data))
{
ZstdCompressResult decompressResult = NativeMethods.decompress(dataPtr, (nuint)data.Length);
CasErrorHandler.ThrowIfError(decompressResult.error_code, "ZSTD decompress");
return NativeByteBuffer.CopyAndFree(decompressResult.data, decompressResult.length);
}
}
}
}
Loading
Loading