Skip to content

Instantly share code, notes, and snippets.

@sar
Forked from ayende/IdentifierMasking.cs
Created April 15, 2021 22:51
Show Gist options
  • Save sar/c55b72e1321744df633cdc71e6987e03 to your computer and use it in GitHub Desktop.
Save sar/c55b72e1321744df633cdc71e6987e03 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