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 / prime-sieve.svg
Created December 22, 2016 21:21
Javascript in SVG example (prime circles)
View prime-sieve.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@AdamWhiteHat
AdamWhiteHat / Eratosthenes.cs
Last active December 22, 2020 10:28
Sieve of Eratosthenes.
View Eratosthenes.cs
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 };
}
@AdamWhiteHat
AdamWhiteHat / HashCodeHelper.cs
Last active December 22, 2020 10:28
Combine two hashcodes
View HashCodeHelper.cs
public static class HashCodeHelper
{
public static int Combine(int h1, int h2)
{
uint num = (uint)((h1 << 5) | (int)((uint)h1 >> 27));
return ((int)num + h1) ^ h2;
}
}
@AdamWhiteHat
AdamWhiteHat / PrimeFactory.cs
Last active December 22, 2020 10:29
PrimeFactory class (with caching). Required by Factorization class.
View PrimeFactory.cs
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 / 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.
View CryptoRandomSingleton.cs
/// <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 / FastFactorial.cs
Created December 22, 2020 12:48
A fast factorial implementation using the divide and conquer method.
View FastFactorial.cs
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 / IsValidIPv6Address.cs
Last active January 31, 2021 05:37
IsValidIPv6Address
View IsValidIPv6Address.cs
public static bool IPv6Check(string input)
{
if (string.IsNullOrWhiteSpace(input))
{
return false;
}
if (input.IndexOf("::") != input.LastIndexOf("::")) // 'The "::" can only appear once in an address.'
{
return false;
@AdamWhiteHat
AdamWhiteHat / BigIntegerExtensionMethods.cs
Last active April 27, 2022 13:03
BigInteger ExtensionMethods
View BigIntegerExtensionMethods.cs
public static class BigIntegerExtensionMethods
{
public static BigInteger Sum(this IEnumerable<BigInteger> source)
{
return source.Aggregate((accumulator, current) => accumulator + current);
}
public static BigInteger Product(this IEnumerable<BigInteger> source)
{
return source.Aggregate((accumulator, current) => accumulator * current);
@AdamWhiteHat
AdamWhiteHat / INotifyPropertyChangedEx.cs
Created March 20, 2022 01:00
INotifyPropertyChangedEx
View INotifyPropertyChangedEx.cs
namespace ComponentModelEx
{
public delegate void PropertyChangedExEventHandler(object sender, PropertyChangedExEventArgs e);
public interface INotifyPropertyChangedEx
{
event PropertyChangedExEventHandler PropertyChangedEx;
}
}
@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.
View PasswordHashing.cs
/// <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);
}