This Repository contains esoteric archive/compression formats.
HashZip boasts an incredible infinite compression ratio - all inputs are compressed to only 64 bytes!
Compression performance is very fast. Decompression may take a while for inputs larger than a few bytes, so make sure you have an inheritance plan to keep the process running to completion.
The SHA-512 and length of the decompressed data will always match the SHA-512 and length of the original data.
A hash is taken of the content to be compressed.
public static byte[] Compress(byte[] content)
{
var hasher = SHA512.Create();
return hasher.ComputeHash(content);
}
The hash is reversed.
public static byte[]? Decompress(byte[] digest, int fileLength)
{
var cts = new CancellationTokenSource();
byte[]? result = null;
Parallel.For((long)0, byte.MaxValue,
new ParallelOptions()
{ CancellationToken = cts.Token, MaxDegreeOfParallelism = Environment.ProcessorCount * 2 },
i =>
{
var sha512 = SHA512.Create();
var currentTest = Enumerable.Repeat((byte)0, fileLength).ToArray();
currentTest[0] = (byte)i;
var found = false;
while (!found)
{
if (sha512.ComputeHash(currentTest).SequenceEqual(digest))
{
found = true;
}
else
{
IncrementByteArray(currentTest);
}
}
if (found)
{
result = currentTest;
cts.Cancel();
}
});
return result;
}
The archive size is determined by the number of unique byte values in the original content. You can achieve smaller compressed files by using fewer unique bytes.
Compression performance is fast. Decompression is much faster than HashZip, but you'll still want to make sure your UPS is working.
The SHA-512 and count of occurences of bytes of the decompressed data will always match the SHA-512 and count of occurences of bytes of the original data.
A hash is taken of the content to be compressed and the number of occurences of each byte is counted.
public static HashZip2Archive Compress(byte[] content)
{
var hasher = SHA512.Create();
var hash = hasher.ComputeHash(content);
var dict = new Dictionary<byte, int>();
foreach (var byt in content)
{
if (!dict.ContainsKey(byt))
{
dict.Add(byt, 0);
}
dict[byt]++;
}
return new HashZip2Archive(hash, dict);
}
The hash is reversed by generating the set of possible permutations of the bytes.
public static byte[]? Decompress(HashZip2Archive archive)
{
var hasher = SHA512.Create();
byte[]? result = null;
var byteList = new List<byte>();
foreach (var byt in archive.ByteCounts)
{
var toWrite = Enumerable.Repeat(byt.Key, byt.Value).ToArray();
byteList.AddRange(toWrite);
}
var candidate = byteList.ToArray();
foreach (var permutation in byteList.Permutations())
{
permutation.CopyTo(candidate,0);
var hash = hasher.ComputeHash(candidate);
if (hash.SequenceEqual(archive.Digest))
{
return candidate;
}
}
return result;
}
MIT.