Created
June 21, 2020 08:23
-
-
Save ayende/4729c8e7be8039023c206c63c5d83a24 to your computer and use it in GitHub Desktop.
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 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