Skip to content

Commit

Permalink
Feature/#104 documentation (#122)
Browse files Browse the repository at this point in the history
* #104 adding documentation to core cryptonet

* #104 GenerateDocumentationFile

* #104 cryptonetinfo get doc

* #104 move old docs to _site\web

* #104 rename docs to web

* #104 intoducing docfx documentation to generate docs from xml and manule markdown rendering

* #104 docfx config

* #104 exlude unwanted folders

* #104 docfx generating command

* #104 adding missing xml docs

* #104 remove unused warning

* #104 fix null warning

* #104 coverage test for X509Certificate2

* #104 improve run docs

* #104 doc publish

* #104 temp disable pipelines

* #104 fix site path

* #104 finalize docs publishing pipeline

* #104 re-enable pipelines

* #104 gitingore update, remove old docs
  • Loading branch information
maythamfahmi authored Nov 3, 2024
1 parent ac26268 commit 690a5b5
Show file tree
Hide file tree
Showing 65 changed files with 686 additions and 45,105 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages

on:
# Runs on pushes targeting the default branch
push:
branches: ["main", "version3"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

jobs:
# Single deploy job since we're just deploying
deploy:
runs-on: ubuntu-latest

environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.0.x"

- name: Install DocFX
run: dotnet tool install -g docfx

- name: Build DocFX Documentation
run: |
docfx metadata
docfx build docfx.json -o _site
- name: Setup Pages
uses: actions/configure-pages@v5

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
# Upload entire repository
path: "./_site"

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,6 @@ CoverageReport
*.vpwhist
*.vtg
/Resources/RsaKeys/encrypted.txt
/api
/_site
/archive
40 changes: 33 additions & 7 deletions CryptoNet.Models/CryptoNetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace CryptoNet.Models;

Expand All @@ -24,6 +25,29 @@ public class RsaDetail
public RSA? Rsa { get; set; }
public byte[] PublicKey { get; set; }
public byte[] PrivateKey { get; set; }

public RsaDetail(RSA rsa)
{
Rsa = rsa ?? throw new ArgumentNullException(nameof(rsa));
PublicKey = Array.Empty<byte>();
PrivateKey = Array.Empty<byte>();
}

public RsaDetail(byte[] publicKey, byte[] privateKey)
{
if (publicKey == null || publicKey.Length <= 0)
{
throw new ArgumentNullException(nameof(publicKey));
}

if (privateKey == null || privateKey.Length <= 0)
{
throw new ArgumentNullException(nameof(privateKey));
}

PublicKey = publicKey;
PrivateKey = privateKey;
}
}

public class AesDetail
Expand All @@ -40,11 +64,7 @@ public AesDetail(byte[] key, byte[] iv)
throw new ArgumentNullException(nameof(iv));
}

AesKeyValue = new AesKeyValue()
{
Key = key,
Iv = iv
};
AesKeyValue = new AesKeyValue(key, iv);
}

public Aes? Aes { get; set; }
Expand All @@ -53,8 +73,14 @@ public AesDetail(byte[] key, byte[] iv)

public class AesKeyValue
{
public byte[] Key { get; set; }
public byte[] Iv { get; set; }
public byte[] Key { get; }
public byte[] Iv { get; }

public AesKeyValue(byte[] key, byte[] iv)
{
Key = key ?? throw new ArgumentNullException(nameof(key));
Iv = iv ?? throw new ArgumentNullException(nameof(iv));
}
}

public enum KeyType
Expand Down
48 changes: 39 additions & 9 deletions CryptoNet.UnitTests/CryptoNetRsaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@ public void Encrypt_With_PublicKey_Decrypt_With_PrivateKey_Of_Content_Test()
Common.ConfidentialDummyData.ShouldBe(decryptedData);
}

[Ignore("Private: This test works only on local Windows enviroment for debugging and testing. You can also put your own certifciate when debugging and testing")]
[Test]
public void Encrypt_Decrypt_Using_X509_Certificate_Test()
{
// Arrange
X509Certificate2? certificate = CryptoNetExtensions.GetCertificateFromStore("CN=Maytham");
// You can change to test real system certificate by using CryptoNetExtensions.GetCertificateFromStore("CN=MaythamCertificateName")
X509Certificate2 ? certificate = CreateSelfSignedCertificate();
var rsaPublicKey = new CryptoNetRsa(certificate, KeyType.PublicKey);
var rsaPrivateKey = new CryptoNetRsa(certificate, KeyType.PrivateKey);

Expand All @@ -218,11 +219,12 @@ public void Encrypt_Decrypt_Using_X509_Certificate_Test()

}

[Ignore("Private: This test works only on local Windows enviroment for debugging and testing. You can also put your own certifciate when debugging and testing")]
[Test]
public void Export_Public_Key_For_X509_Certificate_Test()
{
// Arrange
X509Certificate2? certificate = CryptoNetExtensions.GetCertificateFromStore("CN=Maytham");
// You can change to test real system certificate by using CryptoNetExtensions.GetCertificateFromStore("CN=MaythamCertificateName")
X509Certificate2? certificate = CreateSelfSignedCertificate();
var rsa = new CryptoNetRsa(certificate, KeyType.PublicKey);

// Act
Expand All @@ -233,16 +235,17 @@ public void Export_Public_Key_For_X509_Certificate_Test()
publicKey.ShouldNotBeEmpty();
}

[Ignore("Private: This test works only on local Windows enviroment for debugging and testing. You can also put your own certifciate when debugging and testing")]
[Test]
public void Customize_PEM_Key_Encryption_Decryption_Test()
{
// Arrange
X509Certificate2? cert = CryptoNetExtensions.GetCertificateFromStore("CN=Maytham");
// You can change to test real system certificate by using CryptoNetExtensions.GetCertificateFromStore("CN=MaythamCertificateName")
X509Certificate2? certificate = CreateSelfSignedCertificate();

var pubKeyPem = Common.ExportPemKey(cert!, false);
var priKeyPem = Common.ExportPemKey(cert!);
var pubKeyPem = Common.ExportPemKey(certificate!, false);
var priKeyPem = Common.ExportPemKey(certificate!);
var password = "password";
var encryptedPriKeyBytes = Common.ExportPemKeyWithPassword(cert!, password);
var encryptedPriKeyBytes = Common.ExportPemKeyWithPassword(certificate!, password);

// Act
ICryptoNetRsa cryptoNet1 = ImportPemKeyWithPassword(encryptedPriKeyBytes, password);
Expand Down Expand Up @@ -273,4 +276,31 @@ public static ICryptoNetRsa ImportPemKeyWithPassword(byte[] encryptedPrivateKey,
cryptoNet.Info.RsaDetail?.Rsa?.ImportEncryptedPkcs8PrivateKey(password, encryptedPrivateKey, out _);
return cryptoNet;
}

public static X509Certificate2 CreateSelfSignedCertificate()
{
using var rsa = RSA.Create(2048); // Generate a new RSA key pair for the certificate
var request = new CertificateRequest(
"CN=TestCertificate",
rsa,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1
);

// Add extensions (e.g., for key usage, if needed)
request.CertificateExtensions.Add(
new X509KeyUsageExtension(
X509KeyUsageFlags.DigitalSignature,
critical: true
)
);

// Create a self-signed certificate that is valid for one year
var certificate = request.CreateSelfSigned(
DateTimeOffset.Now.AddDays(-1),
DateTimeOffset.Now.AddYears(1)
);

return certificate;
}
}
1 change: 1 addition & 0 deletions CryptoNet/CryptoNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<_SkipUpgradeNetAnalyzersNuGetWarning>true</_SkipUpgradeNetAnalyzersNuGetWarning>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<Target Name="PrepareReleaseNotes" BeforeTargets="GenerateNuspec">
Expand Down
Loading

0 comments on commit 690a5b5

Please sign in to comment.