Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
Created April 18, 2020 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numberoverzero/910f8abd90cb2f0a3eba576932576416 to your computer and use it in GitHub Desktop.
Save numberoverzero/910f8abd90cb2f0a3eba576932576416 to your computer and use it in GitHub Desktop.
utility functions for filename nonces
byte[] Sha256(string s)
{
using (var hash = SHA256.Create())
{
return hash.ComputeHash(Encoding.UTF8.GetBytes(s));
}
}
byte[] Fold(byte[] input)
{
if (input.Length == 0) return new byte[0];
if (input.Length % 2 == 1)
{
throw Exceptions.IllegalArgument("input", "an even length");
}
int length = input.Length / 2;
byte[] output = new byte[length];
for (int i=0; i<length; i++)
{
output[i] = (byte)(input[i] ^ input[i + length]);
}
return output;
}
string Base64Encode(byte[] b)
{
return System.Convert.ToBase64String(b);
}
void usage() {
var hash = Sha256("hello");
var shortened = Fold(hash);
var encoded = Base64Encode(shortened);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment