This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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 | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |