Skip to content

Instantly share code, notes, and snippets.

@mjs3339
mjs3339 / BigRational.cs
Last active August 11, 2022 22:38
C# Arbitrary Precision Signed Big Rational Numbers
using System;
using System.Diagnostics;
using System.Globalization;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
[DebuggerDisplay("{" + nameof(DDisplay) + "}")]
[Serializable]
public struct BigRational : IComparable, IComparable<BigRational>, IEquatable<BigRational>
{
@mjs3339
mjs3339 / SecureStringHelper.cs
Created January 12, 2021 14:11
Secure String Helper Class
public class SecureStringHelper : IDisposable
{
private readonly Encoding _encoding;
private readonly SecureString _secureString;
private byte[] _bytes;
private bool _disposed;
public SecureStringHelper(SecureString secureString) : this(secureString, Encoding.Default)
{
}
public SecureStringHelper(SecureString secureString, Encoding encoding)
@mjs3339
mjs3339 / xIntX.cs
Last active June 15, 2021 18:21
Adjustable Bit Width Big Unsigned or Signed Integer 32,64,128,256,512,1024,2048…
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
[Serializable]
@mjs3339
mjs3339 / Mapping256BitTo32BitHash.cs
Created January 12, 2021 14:09
Mapping 256Bit Hash to 32Bit Indexed Hash
using System.Security.Cryptography;
public class Mapping256BitTo32BitHash : HashAlgorithm
{
private readonly SHA256Managed hasher = new SHA256Managed();
public readonly TinyDictionary<byte[], byte[]> map = new TinyDictionary<byte[], byte[]>(101, new ArrayComparer());
private byte[] h160;
public override int HashSize => 32;
public override void Initialize()
{
}
@mjs3339
mjs3339 / MapComparer.cs
Created January 12, 2021 14:08
Uses 64bit to 32bit hash Mapping for primitive, Value, and Reference types
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security;
[Serializable]
public class MapComparer<T> : IEqualityComparer<T>
{
@mjs3339
mjs3339 / MonitorActionFuncWrapper.cs
Created January 12, 2021 14:08
Monitor Lock Wrapper
using System;
using System.Threading;
public class MonitorActionFuncWrapper : ConcurrencyCheck
{
public void Lock(object localObject, Action action)
{
if (action == null)
throw new Exception("Action argument cannot be null");
if (CheckState())
{
@mjs3339
mjs3339 / Sha16512.cs
Created January 12, 2021 14:07
Variable 16Bit to 512Bit Hashing Algorithm
using System;
using System.Security.Cryptography;
[Serializable]
public class Sha16512 : HashAlgorithm
{
private int _bitWidth;
private SHA512Managed _hash = new SHA512Managed();
public Sha16512(int bitWidth)
{
if (bitWidth < 16 || bitWidth > 512)
@mjs3339
mjs3339 / GetBytesClass.cs
Created January 12, 2021 14:06
GetBytes Primitive, Serializable, Non-Serializable Arrays
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security;
using System.Security.Cryptography;
@mjs3339
mjs3339 / FNV1a128b.cs
Created January 12, 2021 14:06
FNV1a Patterned 128Bit Hashing Algorithm using BigInteger
using System.Numerics;
using System.Security.Cryptography;
public class FNV1a128b : HashAlgorithm
{
private readonly BigInteger K = "309485009821345068724781371".BigIntegerBase10();
private readonly BigInteger M = "340282366920938463463374607431768211456".BigIntegerBase10();
private BigInteger _hash;
private readonly BigInteger Seed = "144066263297769815596495629667062367629".BigIntegerBase10();
public FNV1a128b()
{
@mjs3339
mjs3339 / RandomX.cs
Created January 12, 2021 14:05
A Fast Random Number Generator Based on BigInteger
using System;
using System.Numerics;
using System.Security.Cryptography;
public struct RandomX
{
private readonly RNGCryptoServiceProvider _crng;
private int _maxByteWidth;
private int _bitWidth;
public RandomX(int bitWidth)
{