Skip to content

Instantly share code, notes, and snippets.

@jdmallen
jdmallen / .bashrc
Last active July 5, 2024 12:01
My .bashrc file that I use in Windows alongside the PowerShell profile, which loads a common SSH agent for git
# Starts an SSH agent for use with git.
# I designed this to share the same agent between PowerShell and Git Bash.
# Make sure you set $GIT_SSH at the User or System level to point to
# whichever ssh-agent.exe PowerShell is using, or else this won't work.
function start_agent {
/c/Windows/System32/OpenSSH/ssh-agent.exe > /dev/null
# Output agent's PID to file
ps -W | grep ssh-agent | awk '{print $4}' | head -n 1 > ~/.ssh/pid
/c/Windows/System32/OpenSSH/ssh-add.exe ~/.ssh/id_rsa.pri # or wherever your private keys are
@jdmallen
jdmallen / profile.ps1
Last active July 5, 2024 12:02
My personal PowerShell profile-- think of it as a `.bashrc` file for PS-- that loads a common SSH agent for git
# Gives me git info in the shell when in git directories
Import-Module posh-git
# Simple function to tell me if I'm in an admin window or not
Function Test-Elevated
{
$wid = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp = New-Object System.Security.Principal.WindowsPrincipal($wid)
$adm = [System.Security.Principal.WindowsBuiltInRole]::Administrator
$prp.IsInRole($adm)
@jdmallen
jdmallen / openssl_sample_config.cnf
Last active October 17, 2023 12:32
An anonymized version of the configuration file I often use with OpenSSL
# OpenSSL root CA configuration file.
[ ca ]
# `man ca`
default_ca = ICA_default
[ CA_default ]
# Directory and file locations.
dir = ./ca # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
@jdmallen
jdmallen / openssl_command_examples.md
Last active October 17, 2023 12:37
Sample commands for OpenSSL, assuming you have an external openssl.cfg file to use (see other gist)

Generate private RSA key with AES encryption

openssl genrsa -out .\path\to\my_cert_encrypted.key -aes256 4096

Generate private RSA key without encryption

openssl genrsa -out .\path\to\my_cert.key 4096
@jdmallen
jdmallen / check_admin_or_root.cs
Created May 15, 2021 18:03
Check if .NET Core/5+ app is running elevated
// To check if you are running as root on Unix:
// 1. Add a PackageReference to Mono.Posix.NETStandard
// 2. Change IsAdminstrator to the following:
public static bool IsAdministrator =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? new WindowsPrincipal(WindowsIdentity.GetCurrent())
.IsInRole(WindowsBuiltInRole.Administrator)
: Mono.Unix.Native.Syscall.geteuid() == 0;
@jdmallen
jdmallen / table_to_sql.sql
Last active January 9, 2021 20:02
Script to generate C# entities (POCOs, optionally) from a SQL Server table -- best run from SSMS
DECLARE @TableName varchar(50)
DECLARE @SchemaName varchar(50)
DECLARE @ClassNamespace varchar(512)
DECLARE @ClassPrefix varchar(50)
DECLARE @ClassExtends varchar(512)
DECLARE @EnableAnnotations bit
DECLARE @AddOnModelCreating bit
DECLARE @NL varchar(2) -- newline character(s)
SET @TableName = 'User'
@jdmallen
jdmallen / CustomFizzBuzz.cs
Created June 19, 2020 05:13
A custom FizzBuzz generator: you select your multiples and keywords, it'll do the rest.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace JDMallen.Gists
{
class Program
{
public static async Task Main(string[] args)
{
@jdmallen
jdmallen / convert_binary_file_to_base64.ps1
Last active April 12, 2024 10:42
Powershell script to convert a binary file to/from a base64 text file
Param(
$filePath,
[switch]$reverse = $false
)
## Usage
#
# From binary to UTF8 base-64 file with "b64" extension:
# .\convert_binary_file_to_base64.ps1 path\to\file.bin
#
@jdmallen
jdmallen / efCoreDropTables.cs
Last active July 23, 2018 18:53
I wrote this snippet to drop all my MariaDB tables and rebuild them using EnsureCreated each time I ran the app to ensure I got the tables and relationships I wanted from my modelBuilder settings in EF Core 2.1. For reference, `dbContext` is of EF type DbContext and I'm injecting it into the Configure method of the Startup class to call the below.
#if DEBUG
var conn = dbContext.Database.GetDbConnection();
conn.Open();
dbContext.Model.GetEntityTypes()
.ToList()
.ForEach( // For each of the entity types registered in the model...
et =>
{
bool tableExists;
// Fetch its table name, whether it's set in OnModelCreating
@jdmallen
jdmallen / urlParseRegex.js
Last active April 16, 2018 18:28
Regex pattern to capture the domain and params of a URL
// Regex:
// /(?:https?:\/\/)?(?:www\.)?([-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6})(?:[:]?[0-9]{0,5})([-a-zA-Z0-9@:%_\+.~#?&\/=]*)/i
function extractUrlParts(url) {
var matches =
/(?:https?:\/\/)?(?:www\.)?([-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6})(?:[:]?[0-9]{0,5})([-a-zA-Z0-9@:%_\+.~#?&\/=]*)/i
.exec(url);
return {
domain: matches && matches[1],
params: matches && matches[2]