Skip to content

Instantly share code, notes, and snippets.

@ayende
Created June 21, 2020 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ayende/4729c8e7be8039023c206c63c5d83a24 to your computer and use it in GitHub Desktop.
Save ayende/4729c8e7be8039023c206c63c5d83a24 to your computer and use it in GitHub Desktop.
public class IdentifierMasking
{
private static byte[] _key;
public IdentifierMasking(byte[] key = null)
{
_key = key ?? Sodium.SecretBox.GenerateKey();
}
public string RevealIdentifier(string hidden)
{
Span<byte> data = SimpleBase.Base58.Bitcoin.Decode(hidden);
byte[] nonce = data.Slice(0, 12).ToArray();
byte[] encrypted = data.Slice(12).ToArray();
byte[] plain = Sodium.SecretAeadAes.Decrypt(encrypted, nonce, _key);
return Encoding.UTF8.GetString(plain);
}
public string HideIdentifier(string id)
{
byte[] nonce = Sodium.SecretAeadAes.GenerateNonce();
byte[] encrypted = Sodium.SecretAeadAes.Encrypt(Encoding.UTF8.GetBytes(id), nonce, _key);
return SimpleBase.Base58.Bitcoin.Encode(nonce.Concat(encrypted).ToArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment