Skip to content

Instantly share code, notes, and snippets.

@mjs3339
mjs3339 / CPUTotalPC.cs
Created December 30, 2017 17:17
C# Get Processor Time using PerformanceCounter
public static class CPUTotalPC
{
private static PerformanceCounter _CPUsage;
public static double CPULoad
{
get
{
if (_CPUsage == null)
{
try
@mjs3339
mjs3339 / GArray.cs
Created December 30, 2017 17:20
C# Small Generic Collection Class
[Serializable]
public class GArray<T>
{
private readonly int _capacity = 4096;
private int _count;
private int _position;
private T[] Thing;
public GArray()
{
_capacity = 4096;
@mjs3339
mjs3339 / CryptoGetBytes.cs
Last active January 8, 2018 16:32
C# Implements a Cryptographically Strong GetBytes
public class CryptoGetBytes
{
private readonly SHA512Managed _Hasher = new SHA512Managed();
private readonly SeedBytes _SeedBytes;
private int _AvailableCacheBytes;
private byte[] _Buffer;
private byte[] _Cache;
private int _Ptr;
public CryptoGetBytes()
{
@mjs3339
mjs3339 / brmsc.cs
Created January 1, 2018 01:43
C# Add Functionallity to BigRational AsDecimal AsScientific
public BigInteger Remainder => Numerator % Denominator;
private static ulong HashHold;
private static string DecimalString = "0";
private static readonly FNV1a64 _Hasher = new FNV1a64();
public static int DisplayPrecision = 100;
public string AsDecimal(bool rounding = false, int toDigits = 100)
{
var lh = _Hasher.Hash64(Numerator.ToByteArray()) ^ _Hasher.Hash64(Denominator.ToByteArray());
if (HashHold == lh) return DecimalString;
if (DisplayPrecision < 0)
@mjs3339
mjs3339 / CryptoRandomNumberGenerator.cs
Created January 2, 2018 20:36
C# Generic Cryptographically Strong Random Number Generator
public class CryptoRandomNumberGenerator<T> where T : struct
{
private static BigRational _maxValue;
private readonly byte[] _buffer;
private readonly BigRational _mult;
private readonly CryptoGetBytes _ncsp;
private int _availableCacheEntries;
private BigRational[] _cache;
private int _cacheptr;
private int _cacheSize = 1000000;
@mjs3339
mjs3339 / BigIntegerHelper.cs
Created January 2, 2018 21:00
C# BigIntegerHelper Class
public static class BigIntegerHelper
{
private static readonly BigInteger Ten = new BigInteger(10);
public static BigInteger ToBigInteger(this ulong ul)
{
return new BigInteger(ul);
}
public static BigInteger ToBigInteger(this long ul)
{
return new BigInteger((ulong) ul);
@mjs3339
mjs3339 / LimitedStack.cs
Created January 4, 2018 06:34
C# Limited Size Stack Class
[Serializable]
public class LimitedStack<T> : IEnumerable<T>
{
private T[] _array;
public LimitedStack(int maxsize)
{
if (maxsize == 0)
throw new Exception("The MaxSize argument must be greater than 0");
MaxSize = maxsize;
_array = new T[MaxSize];
@mjs3339
mjs3339 / CryproSeedBytes.cs
Last active January 11, 2023 03:43
C# CPU Randomness Extractor Based on CPU Noise, Jitter, Drift
/// <summary>
/// Randomness extractor for Small and Large seed buffers based on CPU noise/jitter.
/// </summary>
public class CryptoSeedBytes
{
private readonly object _lock = new object();
public CryptoSeedBytes()
{
TSCSupport = Tsc_supported();
AdjustAdditionLoopLimits();
@mjs3339
mjs3339 / Zobristhashing32.cs
Last active April 24, 2018 21:03
C# 32 Bit Implementation of the Zobrist Hashing Algorithm.
/// <summary>
/// https://en.wikipedia.org/wiki/Zobrist_hashing
/// Faster then GetHashCode, 6 collisions / 435,000 English words, ~20 Collisions / 400,000
/// random byte patterns.
/// </summary>
public class ZOB64 : HashAlgorithm
{
private ulong[] _table;
private ulong hash;
private uint hash32;
@mjs3339
mjs3339 / Zobristhashing64.cs
Last active March 14, 2023 01:40
C# 64 Bit Implementation of the Zobrist Hashing Algorithm.
public class ZOB64 : HashAlgorithm
{
private ulong[] _table;
private ulong hash;
private uint hash32;
private readonly ulong starthash = 0x391615744853B307;
private ZOB32State zhs;
public ZOB64()
{
hash = starthash;