Skip to content

Instantly share code, notes, and snippets.

View savaged's full-sized avatar
418

David Savage savaged

418
View GitHub Profile
@savaged
savaged / totp-fun.sh
Last active October 9, 2023 14:08
Fun with a very basic implementation of TOTP
#!/bin/sh
# generate shared secret for TOTP
secret='never gonna give you up'
# take only the last 16 characters less padding
# apply standard formatting (see https://github.com/google/google-authenticator/wiki/Key-Uri-Format )
# generate the QR code (remember to apply percent encoding for the url)
echo -n $secret | base32 | sed 's/=//g' | tail -c 16 | xargs -I $ echo 'otpauth://totp/savaged%20info:alice@test.com?secret=$&issuer=Savaged' | xargs qrencode -o ~/Downloads/savaged-qr-code.png
#finally, generate TOTP
@savaged
savaged / sofd.sh
Last active September 30, 2023 21:08
SOTD (scripture of the day)
#!/bin/sh
# dependencies: curl, grep, sed, html-xml-utils, lolcat and an internet connection
curl -s "https://wol.jw.org/en/wol/h/r1/lp-e/$(date +"%Y/%m/%d")" | grep -E -A5 "data-date=\"$(date +"%Y-%m-%d")" | grep -m 1 '\<themeScrp\>' | hxselect -c 'em' | sed 's/$/\n/g' | lolcat
@savaged
savaged / TEA.cs
Last active September 24, 2023 18:22
TEA implementation
// Credit to Page Brooks https://www.codeproject.com/Articles/6137/Tiny-Encryption-Algorithm-TEA-for-the-Compact-Fram
var key = "mykey";
var encrypted = Encrypt("testing", key);
var decrypted = Decrypt(encrypted, key);
Console.WriteLine(decrypted);
static string Encrypt(string data, string key)
{
if (data.Length == 0)
throw new ArgumentOutOfRangeException("data must be at least 1 character in length.");
@savaged
savaged / DiffieHellman
Last active September 24, 2023 16:58
High-level Diffie-Hellman key exchange
// public (primitive root) base¹, known to Alice, Bob, and Eve
const long G = 5;
// public (prime) modulus², known to Alice, Bob, and Eve
const long P = 23;
var aliceCalc = new Calc(6, G, P);
var bobCalc = new Calc(5, G, P);
// Alice's public key, known to Alice, Bob, and Eve. A = g^a mod p
long A = aliceCalc.GetPublicKey(bobCalc.OutgoingSharedKey);
@savaged
savaged / OcrPostcodeCleaningEx.cs
Created March 31, 2023 14:04
Some regex to clean OCR 'O' for zero mistakes in UK postcodes
using System.Text.RegularExpressions;
foreach (var postcode in args)
Console.WriteLine(postcode.ReplaceAnyMisplacedO());
static class OcrPostCodeCleaningExtensions
{
public static string ReplaceAnyMisplacedO(this string postcode) =>
postcode.ReplaceAnyFrontO().ReplaceAnyEndO();
@savaged
savaged / YCombinator.cs
Created March 25, 2023 19:00
Trying to get my head around the y combinator
// Y = λf.(λx.f(x x))(λx.f(x x))
var factorial = YCombinator.Y<int, int>(recursiveFunc => num => num == 0 ? 1 : num * recursiveFunc(num - 1));
var fibonacci = YCombinator.Y<int, int>(recursiveFunc => num => num <= 1 ? 1 : recursiveFunc(num - 1) + recursiveFunc(num - 2));
// Test with sample inputs
Console.WriteLine(factorial(3));
Console.WriteLine(fibonacci(5));
// Implementation of Y combinator
@savaged
savaged / CurryConcept.cs
Created March 25, 2023 17:18
Fun tasting curry in C#
// See Computerphile: https://youtu.be/psmu_VAuiag
// add x y = x + y
Func<int, int, int> AddTwoParameters = (x, y) => x + y;
// map (add 100) [1..10]
Enumerable.Range(1, 10)
.Select(i => AddTwoParameters.AddLater()(100)(i))
.ToList()
.ForEach(i => Console.WriteLine(i));
@savaged
savaged / ChmodNum.cs
Last active March 22, 2023 07:01
Fun with C# and a command line tool for calculating the UNIX file permission number from the text listing
using System.Text.RegularExpressions;
if (args.Length == 0 || !args[0].IsUnixFileModeAndPermissionsString())
{
Console.WriteLine(
"Argument must be in the form of a UNIX file permission string, " +
"i.e. -rwxrwxrwx or -rwxr-xr-x etc.");
return;
}
Console.WriteLine(args[0].ToClasses().ToPermissionNumber());
@savaged
savaged / LinqGroupByMethodSyntax.cs
Created March 17, 2023 13:22
Simple example of Linq GroupBy in C# using method syntax
var customers = new List<(string Name, string Town)>
{
("Isaac", "London"),
("Sara", "Birmingham"),
("Tim", "London"),
("Michelle", "Manchester")
};
var customersByTownQuery = customers
.GroupBy(c => c.Town)
@savaged
savaged / funwithla.tex
Created March 15, 2023 15:20
Fun with LaTeX
\documentclass[12pt]{beamer}
\begin{document}
\title{Fun With \LaTeX}
\author{David Savage}
\maketitle
\begin{frame}
\frametitle{Net Algorythm}
Function to calculate net, given the gross amount\textsuperscript{a} and the percentage rate used\textsuperscript{b}.
\begin{displaymath}
f(a, r) = \frac{a}{(100+r)}\times100