Last active
September 12, 2017 13:56
-
-
Save andresmoschini/fb57f64a2ab6a7655ffbd819402ce050 to your computer and use it in GitHub Desktop.
EmailUrlObfuscation
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
using System; | |
using System.Text; | |
using System.Linq; | |
public static class EmailUrlObfuscation | |
{ | |
private static string AtSymbolEncode(this string email) => email | |
.Replace("]", "-]") | |
.Replace("@", "[a]"); | |
private static string AtSymbolDecode(this string customizedEmail) => customizedEmail | |
.Replace("[a]", "@") | |
.Replace("-]", "]"); | |
private static string Base64Encode(this string plain) => | |
Convert.ToBase64String(Encoding.UTF8.GetBytes(plain)); | |
private static string Base64Decode(this string base64) => | |
Encoding.UTF8.GetString(Convert.FromBase64String(base64)); | |
private static string Base64ForUrlEncode(this string base64) => base64 | |
.Replace('+', '-') | |
.Replace('/', '_') | |
.TrimEnd(new[] { '=' }); | |
private static string Base64ForUrlDecode(this string base64ForUrl) => base64ForUrl | |
.Replace('-', '+') | |
.Replace('_', '/') | |
+ string.Join("", Enumerable.Repeat("=", (4 - base64ForUrl.Length % 4) % 4)); | |
public static string ObfuscateEmailForUrl(string email) => email | |
.AtSymbolEncode() | |
.Base64Encode() | |
.Base64ForUrlEncode(); | |
public static string DeobfuscateEmailForUrl(string obfuscatedEmail) => obfuscatedEmail | |
.Base64ForUrlDecode() | |
.Base64Decode() | |
.AtSymbolDecode(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment