Created
April 21, 2018 05:29
-
-
Save mjs3339/13269a33fa0d93520622ba9b99b76b63 to your computer and use it in GitHub Desktop.
C# Entropy of Primitive Array Types Plus String, String Array, SecureString, Biginteger, and Bigrational
This file contains 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 Entropy | |
{ | |
private static readonly int[] _map = new int[256]; | |
private static bool _mapStatus; | |
static Entropy() | |
{ | |
_map.Fill(0); | |
_mapStatus = false; | |
} | |
public static double GetEntropy(this char[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this sbyte[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this byte[] value) | |
{ | |
return EntropyBase(value); | |
} | |
public static double GetEntropy(this short[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this ushort[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this int[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this uint[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this long[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this ulong[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this float[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this double[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this string value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this string[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this SecureString value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this SecureString[] value) | |
{ | |
return EntropyBase(value.GetBytes()); | |
} | |
public static double GetEntropy(this BigInteger value) | |
{ | |
return EntropyBase(value.ToByteArray()); | |
} | |
public static double GetEntropy(this BigInteger[] value) | |
{ | |
var ba = new byte[0]; | |
foreach (var a in value) | |
ba = ba.Add(a.ToByteArray()); | |
return EntropyBase(ba); | |
} | |
public static double GetEntropy(this BigRational value) | |
{ | |
var ba = new byte[0]; | |
ba = ba.Add(value.Numerator.ToByteArray().Add(value.Denominator.ToByteArray())); | |
return EntropyBase(ba); | |
} | |
public static double GetEntropy(this BigRational[] value) | |
{ | |
var ba = new byte[0]; | |
foreach (var a in value) | |
ba = ba.Add(a.Numerator.ToByteArray().Add(a.Denominator.ToByteArray())); | |
return EntropyBase(ba); | |
} | |
public static double GetEntropyAsPercent(this char[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this sbyte[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this byte[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this short[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this ushort[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this int[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this uint[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this long[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this ulong[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this float[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this double[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this string value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this string[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this SecureString value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this SecureString[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this BigInteger value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this BigInteger[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this BigRational value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
public static double GetEntropyAsPercent(this BigRational[] value) | |
{ | |
return GetEntropy(value) / 8.00D * 100D; | |
} | |
/// <summary> | |
/// The central function that calculates the entropy of the byte array. | |
/// </summary> | |
/// <param name="s">The byte array being analyzed.</param> | |
/// <returns>Entropy-0 being perfect order, 8 being pure randomness.</returns> | |
private static double EntropyBase(byte[] s) | |
{ | |
double Len = s.Length; | |
var result = 0d; | |
if (_mapStatus) | |
{ | |
_map.Fill(0); | |
_mapStatus = false; | |
} | |
for (var i = 0; i < (int) Len; i++) | |
_map[s[i]]++; | |
_mapStatus = true; | |
foreach (var item in _map) | |
{ | |
var frequency = item / Len; | |
result -= frequency * Math.Log(frequency, 2); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment