Skip to content

Instantly share code, notes, and snippets.

View cj-bytes's full-sized avatar

cj-bytes cj-bytes

View GitHub Profile
@cj-bytes
cj-bytes / Program.cs
Last active August 25, 2023 01:29
RulesEngine Demo
//install this nuget: https://www.nuget.org/packages/RulesEngine
using RulesEngine.Extensions;
using RulesEngine.Models;
var rules = new List<Rule>();
rules.Add(new Rule()
{
RuleName = "Weekend Rule",
SuccessEvent = "Weekend Schedule",
@cj-bytes
cj-bytes / levenshtein.js
Last active September 12, 2023 04:18
Levenshtein Distance in javascript
function levenshteinDistance(arr1, arr2) {
const m = arr1.length;
const n = arr2.length;
const maxLength = Math.max(m, n);
// Create a matrix to store the distances
const dp = new Array(m + 1).fill(null).map(() => new Array(n + 1).fill(0));
// Initialize the matrix
for (let i = 0; i <= m; i++) {
@cj-bytes
cj-bytes / Program.cs
Created September 28, 2023 06:08
Create and Validate a JWT using a self signed certificate in .NET 6
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
/*
Use the following command to generate a proper self signed certificate
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365 -nodes
-nodes removes the password requirement
@cj-bytes
cj-bytes / gist:b459a72ebb1ede0066ded4577b1a34bb
Created January 9, 2024 18:59
Generate HMAC SHA-256 Digest in ruby
require 'openssl'
def generate_hmac_sha256(secret_key, payload)
OpenSSL::HMAC.hexdigest('SHA256', secret_key, payload)
end
# Example usage
secret_key = 'your-secret-key'
payload = 'your-payload'
digest = generate_hmac_sha256(secret_key, payload)
@cj-bytes
cj-bytes / program.cs
Created January 24, 2024 20:09
Multi JWT provider on .NET 8
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
@cj-bytes
cj-bytes / IsLocked.cs
Last active November 19, 2024 08:08
User32.dll to check user connection
//first version
using System.Runtime.InteropServices;
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags, bool fInherit, uint dwDesiredAccess);
[DllImport("user32.dll", SetLastError = true)]
static extern bool CloseDesktop(IntPtr hDesktop);
[DllImport("user32.dll", SetLastError = true)]