Last active
August 29, 2015 14:03
-
-
Save abenedykt/69be1b0b9b3b055bdfeb to your computer and use it in GitHub Desktop.
"wisemans" reFUCKtroing
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
// version before refactoring | |
using (var key = Registry.CurrentUser.OpenSubKey(RegistryKey, true)) | |
{ | |
var obfuscator = new ViewNameObfuscator("whatever"); | |
key.SetValue("password", obfuscator.Obfuscate(value)); | |
key.Close(); | |
} | |
// version after refactoring - the functionality remained the same - exacly the same | |
return ReadValueFromRegistry(PasswordKeyName, Deobfuscate); | |
private string ReadValueFromRegistry(string keyName, Func<string, string> transformation = null) | |
{ | |
using (var key = OpenKeyForRead()) | |
{ | |
if (key == null) | |
return null; | |
var value = key.GetValue(keyName) as string; | |
return transformation != null ? transformation.Invoke(value) : value; | |
} | |
} | |
private static string Deobfuscate(string value) | |
{ | |
return TransformByObfuscator(value, (x, y) => x.Deobfuscate(y)); | |
} | |
private static string TransformByObfuscator(string value, Func<ViewNameObfuscator, string, string> transformation) | |
{ | |
var obfuscator = new ViewNameObfuscator("D0B64B7A6099"); | |
return transformation.Invoke(obfuscator, value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment