Skip to content

Instantly share code, notes, and snippets.

@mjs3339
mjs3339 / CryptoGetBytesExperimental.cs
Last active Mar 4, 2019
C# CryptoGetBytesExperimental
View CryptoGetBytesExperimental.cs
using System;
using System.Security.Cryptography;
/// <summary>
/// Murmur3 (128b): 16 MBps.
/// MD5 (128b): 7.5 MBps.
/// SHA1 (160b): 8.5MBps.
/// SHA256: 9.5 MBps.
/// *SHA512: 13.5 MBps.
/// SHA3 (1024b): 12.5 MBps.
/// Tested: I7-8700K @4.2Ghz
@mjs3339
mjs3339 / CryptoEntropySourceExperimental.cs
Created Mar 4, 2019
C# CryptoEntropySourceExperimental
View CryptoEntropySourceExperimental.cs
#define TESTING
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading;
public class CryptoEntropySourceExperimental
{
@mjs3339
mjs3339 / BigIntegerEx.cs
Created Mar 2, 2019
C# BigInteger Extension Class
View BigIntegerEx.cs
public static class BigIntegerEx
{
private static readonly BigInteger Ten = new BigInteger(10);
private static readonly BigInteger Three = new BigInteger(3);
public static BigInteger ToBigInteger(this char ul)
{
return new BigInteger(ul);
}
public static BigInteger ToBigInteger(this byte ul)
{
View CryptoRandomNumberGeneratorExperimental.cs
[Serializable]
public class CryptoRandomNumberGeneratorExperimental : RandomNumberGenerator
{
private const double DBi = 1.0 / ulong.MaxValue;
private readonly CryptoGetBytesExperimental crng = new CryptoGetBytesExperimental();
public bool NextBool()
{
return Sample() < .5;
}
/// <summary>
@mjs3339
mjs3339 / Mapping64BitToHash32Bit.cs
Last active Feb 19, 2019
C# 32 Bit Hashing Algorithm ~No Collisions in the Int Bit Space
View Mapping64BitToHash32Bit.cs
using System.Security.Cryptography;
/// <summary>
/// Pros: ~Very few to no Collisions within int bit space ~(2,147,483,647)
/// Cons: Maintains a TinyDictionary(byte[],byte[]), Decreased performance, non-deterministic across application or
/// method domains
/// Cannot Transform or reuse.
/// </summary>
public class Mapping64BitToHash32Bit : HashAlgorithm
{
private readonly FNV1a64 hasher = new FNV1a64();
@mjs3339
mjs3339 / ArrayComparer.cs
Created Feb 17, 2019
C# Generic IEqualityComparer
View ArrayComparer.cs
[Serializable]
private class ArrayComparer<T> : IEqualityComparer<T>
{
private static Type _type;
private static int _size;
public bool Equals(T x, T y)
{
if(x == null || y == null)
return false;
if(_type == null)
@mjs3339
mjs3339 / Converters.cs
Created Feb 17, 2019
C# Convert from Primitives to Byte Array
View Converters.cs
public static class Converters
{
public static bool IsValidPrimitive(Type type)
{
switch(Type.GetTypeCode(type))
{
case TypeCode.Boolean:
case TypeCode.Char:
case TypeCode.SByte:
case TypeCode.Byte:
@mjs3339
mjs3339 / FNV1a32xf.cs
Last active Feb 8, 2019
C# FNV1a32 XOR Folding Version
View FNV1a32xf.cs
/// <summary>
/// XOR folding version reduces collisions by greater than ~2/3's
/// </summary>
public class FNV1a32 : HashAlgorithm
{
private const uint K = 0x1000193;
private uint _hash;
public uint Hash;
public uint Seed = 0x811c9dc5;
public FNV1a32()
@mjs3339
mjs3339 / IEnumerableFileHelper.cs
Created Feb 6, 2019
C# IEnumerable File Helper Class, Load or Save Primitive, Class, Structure, IEnumerable To or From a File.
View IEnumerableFileHelper.cs
/// <summary>
/// IEnumerable File Helper Class.
/// </summary>
public static class IEnumerableFileHelper
{
/// <summary>
/// Save a IEnumerable to a file.
/// </summary>
public static void SaveToFileIEnum<T>(this IEnumerable<T> obj, string path, bool append = false)
{
@mjs3339
mjs3339 / Pathhelper.cs
Created Feb 1, 2019
C# Pathhelper, Analyze Path Construction Without Touching Path
View Pathhelper.cs
public static class Pathhelper
{
[Flags]
[Serializable]
public enum DeviceTypes
{
None = 0,
CDRom = 1,
InvalidCDRom = 2,
PhysicalDrive = 4,
You can’t perform that action at this time.