-
-
Save sar/c55b72e1321744df633cdc71e6987e03 to your computer and use it in GitHub Desktop.
This file contains hidden or 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