Skip to content

Instantly share code, notes, and snippets.

View AdamWhiteHat's full-sized avatar
🏠
Working from home

Adam White AdamWhiteHat

🏠
Working from home
View GitHub Profile
@AdamWhiteHat
AdamWhiteHat / ObservableDictionary.cs
Last active September 14, 2023 23:50
ObservableDictionary - Like an ObservableCollection, but its a Dictionary. Fires events for: Add, Remove, Replace, and Clear.
using System;
using System.Linq;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
@AdamWhiteHat
AdamWhiteHat / GenericArithmeticFactory.cs
Last active January 25, 2023 17:56
Generic arithmetic operations with an example
public static class GenericArithmeticFactory<T>
{
private static Dictionary<ExpressionType, Func<T, T, T>> _binaryOperationDictionary;
private static Dictionary<ExpressionType, Func<T, T, bool>> _comparisonOperationDictionary;
private static Func<T, T> _sqrtOperation = null;
static GenericArithmeticFactory()
{
_binaryOperationDictionary = new Dictionary<ExpressionType, Func<T, T, T>>();
_comparisonOperationDictionary = new Dictionary<ExpressionType, Func<T, T, bool>>();
@AdamWhiteHat
AdamWhiteHat / PasswordHashing.cs
Created December 22, 2020 13:17
Don't use SHA256 to hash your passwords! Hash passwords the correct way, using BCrypt.
/// <summary>
/// Requires the BCrypt nuget package. Available here: https://www.nuget.org/packages/BCrypt.Net-Next
/// </summary>
public static class PasswordHashing
{
public static string HashPassword(string password, int workFactor)
{
return BCrypt.Net.BCrypt.HashPassword(password, workFactor);
}
@AdamWhiteHat
AdamWhiteHat / FastFactorial.cs
Created December 22, 2020 12:48
A fast factorial implementation using the divide and conquer method.
public static class FastFactorial
{
public static BigInteger Factorial(BigInteger n)
{
return MultiplyRange(2, n);
}
private static BigInteger MultiplyRange(BigInteger from, BigInteger to)
{
var diff = to - from;
@AdamWhiteHat
AdamWhiteHat / NumberTheory.Maths.cs
Last active June 19, 2022 02:38
Number Theory functions: Legendre Symbol, Eulers Criterion, Tonelli-Shanks, Chinese Remainder Theorem, Modular Multiplicative Inverse, Primitive Root, Multiplicative Order, Discrete Logarithm, Discrete Root, Eulers Totient Phi, Carmichael Lambda, Exponentiation by Squaring, Factorial, Complex Gamma Function, Nth Root, Prime Factorization, LCM, GCD.
using System;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
public static class Maths
{
/// <summary>
/// Legendre Symbol returns 1 for a (nonzero) quadratic residue mod p, -1 for a non-quadratic residue (non-residue), or 0 on zero.
/// </summary>
@AdamWhiteHat
AdamWhiteHat / CryptoRandomSingleton.cs
Last active December 22, 2020 12:42
Cryptographically secure random number generator. Safe, secure, & correct implementation. Numbers are evenly distributed across entire range; avoids modulo bias. Internal buffers always cleared after every value returned--leaves nothing in memory. Singleton intended to be instantiated only once. Remember to call Dispose.
/// <summary>
/// Cryptographically secure random number generator. Features a safe, secure, and correct implementation.
/// Numbers are evenly distributed across the whole range; avoids modulo bias.
/// Internal buffers always cleared after every value returned--leaves nothing in memory.
/// Singleton intended to be instantiated only once for the lifetime of the application.
/// Remember to call Dispose when no longer needed.
/// </summary>
public static class CryptoRandomSingleton
{
@AdamWhiteHat
AdamWhiteHat / ANS1PrivateKey.cs
Created December 22, 2020 10:44
ANS1 parser for a RSA PrivateKey. Can also create a private key from just a P and Q by calculating the D, DP, DQ, and InverseQ. Create real private keys from small primes for CTFs, factoring challenges or the lulz.
public class ANS1PrivateKey : IDisposable
{
public BigInteger Modulus = BigInteger.MinusOne;
public BigInteger Exponent = BigInteger.MinusOne;
public BigInteger P = BigInteger.MinusOne;
public BigInteger Q = BigInteger.MinusOne;
public BigInteger D = BigInteger.MinusOne;
public BigInteger DP = BigInteger.MinusOne;
public BigInteger DQ = BigInteger.MinusOne;
public BigInteger InverseQ = BigInteger.MinusOne;
@AdamWhiteHat
AdamWhiteHat / Factorization.cs
Last active May 22, 2022 17:51
Prime Factorization class.
using System.Numerics;
public static class Factorization
{
private static BigInteger _cacheCeiling;
private static List<BigInteger> _primeCache;
private static BigInteger _cacheLargestPrimeCurrently;
private static BigInteger[] _probablePrimeCheckBases;
static Factorization()
@AdamWhiteHat
AdamWhiteHat / PrimeFactory.cs
Last active December 22, 2020 10:29
PrimeFactory class (with caching). Required by Factorization class.
public static class PrimeFactory
{
private static BigInteger _cacheLargestPrimeCurrently;
private static BigInteger _cacheCeiling;
internal static BigInteger[] _primeCache;
static PrimeFactory()
{
_cacheCeiling = BigInteger.Pow(11, 7);
_primeCache = new BigInteger[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
@AdamWhiteHat
AdamWhiteHat / Eratosthenes.cs
Last active December 22, 2020 10:28
Sieve of Eratosthenes.
public static class Eratosthenes
{
public static IEnumerable<BigInteger> Sieve(Int64 maxValue)
{
if (maxValue < 10)
{
if (maxValue == 9 || maxValue == 8 || maxValue == 7)
{
return new List<BigInteger>() { 2, 3, 5, 7 };
}