Last active
July 27, 2020 20:32
-
-
Save phillippelevidad/b8b5bb097d6292223e542845e079a4c5 to your computer and use it in GitHub Desktop.
Replace letters and numbers with RANDOM letters and numbers, respectively. Good for obfuscating senstive data before logging.
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 static class StringExtensions | |
{ | |
// Consecutive runs for "Foo Bar 0123456789" generated: | |
// xuc iih 3156679205 | |
// lzr xai 1153091860 | |
// xxm uoy 5532413549 | |
public static string Obfuscate(this string input) | |
{ | |
if (input == null) return null; | |
var obfuscated = new char[input.Length]; | |
var random = new Random(); | |
for (var i = 0; i < input.Length; i++) | |
{ | |
if (Char.IsLetter(input[i])) | |
{ | |
obfuscated[i] = (char)random.Next(97, 123); | |
continue; | |
} | |
if (Char.IsNumber(input[i])) | |
{ | |
obfuscated[i] = (char)random.Next(48, 58); | |
continue; | |
} | |
obfuscated[i] = input[i]; | |
} | |
return new String(obfuscated); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment