Created
January 4, 2013 13:47
-
-
Save ChrisMcKee/4452710 to your computer and use it in GitHub Desktop.
Security type methods (helpers)
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
| using System; | |
| using System.Configuration; | |
| using System.Security.Cryptography; | |
| using System.Text; | |
| using System.Web; | |
| using BCrypt.Net; | |
| public static class Helper | |
| { | |
| internal static T GetAppSetting<T>(string key, T Default) | |
| { | |
| if (key != null) | |
| { | |
| var value = ConfigurationManager.AppSettings[key]; | |
| if (string.IsNullOrEmpty(value)) | |
| return Default; | |
| if (typeof (T).BaseType == typeof (Enum)) | |
| return (T) Enum.Parse(typeof (T), value); | |
| return (T) Convert.ChangeType(value, typeof (T)); | |
| } | |
| return Default; | |
| } | |
| internal static bool CheckPassword(string password, string dbpassword, string salt) | |
| { | |
| return BCrypt.Verify(String.Concat(password, salt), dbpassword); | |
| } | |
| public static string EncodePassword(string password, string salt, int workFactor) | |
| { | |
| var pass = String.Concat(password, salt); | |
| string hash = BCrypt.HashPassword(pass, workFactor); | |
| return hash; | |
| } | |
| internal static string GetConfigValue(string configValue, string defaultValue) | |
| { | |
| if (String.IsNullOrEmpty(configValue)) | |
| return defaultValue; | |
| return configValue; | |
| } | |
| /// <summary> | |
| /// Returns a random 64 character hex string (256 bits) | |
| /// </summary> | |
| public static string CreateSalt() | |
| { | |
| var random = new RNGCryptoServiceProvider(); | |
| var salt = new byte[32]; //256 bits | |
| random.GetBytes(salt); | |
| return BytesToHex(salt); | |
| } | |
| /// <summary> | |
| /// Converts a byte array to a hex string | |
| /// </summary> | |
| internal static string BytesToHex(byte[] toConvert) | |
| { | |
| var s = new StringBuilder(toConvert.Length*2); | |
| foreach (byte b in toConvert) | |
| { | |
| s.Append(b.ToString("x2")); | |
| } | |
| return s.ToString(); | |
| } | |
| /// <summary> | |
| /// returns the SHA512 hash of a string, formatted in hex | |
| /// </summary> | |
| public static string SHA512Hex(string plainText) | |
| { | |
| var hash = new SHA512Managed(); | |
| var utf8 = Encoding.UTF8.GetBytes(plainText); | |
| return BytesToHex(hash.ComputeHash(utf8)); | |
| } | |
| public static string GetMD5Hash(string input) | |
| { | |
| var x = new MD5CryptoServiceProvider(); | |
| byte[] bs = Encoding.UTF8.GetBytes(input); | |
| bs = x.ComputeHash(bs); | |
| var s = new StringBuilder(); | |
| foreach (byte b in bs) | |
| { | |
| s.Append(b.ToString("x2").ToLower()); | |
| } | |
| return s.ToString(); | |
| } | |
| public static string GetClientIP() | |
| { | |
| HttpContext context = HttpContext.Current; | |
| string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; | |
| if (!string.IsNullOrEmpty(ipAddress)) | |
| { | |
| string[] addresses = ipAddress.Split(','); | |
| if (addresses.Length != 0) | |
| { | |
| return addresses[0]; | |
| } | |
| } | |
| return context.Request.ServerVariables["REMOTE_ADDR"]; | |
| } | |
| public static string PHPMd5Hash(string pass) | |
| { | |
| using (MD5 md5 = MD5.Create()) | |
| { | |
| byte[] input = Encoding.UTF8.GetBytes(pass); | |
| byte[] hash = md5.ComputeHash(input); | |
| return BitConverter.ToString(hash).Replace("-", ""); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment