/// <summary> /// Base64 Encode /// </summary> /// <param name="input">입력값</param> /// <param name="encodingType">Encoding Type</param> /// <returns>Base64 Encode 문자열</returns> public static string base64_encode(string input, HxEncodingType encodingType = HxEncodingType.ASCII) { //byte[] inputStringAsBytes = Encoding.ASCII.GetBytes(input); byte[] inputStringAsBytes = GetString2Bytes(input, encodingType); string Result = Convert.ToBase64String(inputStringAsBytes); return(Result); }
/// <summary> /// Base64 Decode /// </summary> /// <param name="input">입력값</param> /// <param name="encodingType">Encoding Type</param> /// <returns>Base64 Decode 문자열</returns> public static string base64_decode(string input, HxEncodingType encodingType = HxEncodingType.ASCII) { byte[] inputStringAsBytes = Convert.FromBase64String(input); //string Result = Encoding.ASCII.GetString(inputStringAsBytes); string Result = GetBytes2String(inputStringAsBytes, encodingType); return(Result); }
public static string Sha1(string inputValue, HxEncodingType encodingType = HxEncodingType.None) { string Result = null; using (System.Security.Cryptography.SHA1 sha = new System.Security.Cryptography.SHA1CryptoServiceProvider()) { byte[] data = sha.ComputeHash(GetString2Bytes(inputValue, encodingType)); Result = GetBytes2String(data); } return(Result); }
/// <summary> /// MD5 /// </summary> /// <param name="inputValue"></param> /// <param name="encodingType"></param> /// <returns></returns> public static string Md5(string inputValue, HxEncodingType encodingType = HxEncodingType.None) { string Result = null; using (System.Security.Cryptography.MD5 md5Hash = System.Security.Cryptography.MD5.Create()) { Result = GetMd5Hash(md5Hash, inputValue, encodingType); } //String.IsNullOrWhiteSpace return(Result); }
/// <summary> /// Byte형을 Encoding Type 문자열로 변환 /// </summary> /// <param name="input">입력값</param> /// <param name="encodingType">Encoding Type</param> /// <returns>변환 값</returns> public static string GetBytes2String(byte[] input, HxEncodingType encodingType) { string Result; switch (encodingType) { case HxEncodingType.UTF7: Result = Encoding.UTF7.GetString(input); break; case HxEncodingType.UTF32: Result = Encoding.UTF32.GetString(input); break; case HxEncodingType.Unicode: Result = Encoding.Unicode.GetString(input); break; case HxEncodingType.BigEndianUnicode: Result = Encoding.BigEndianUnicode.GetString(input); break; case HxEncodingType.ASCII: Result = Encoding.ASCII.GetString(input); break; case HxEncodingType.Default: Result = Encoding.Default.GetString(input); break; case HxEncodingType.UTF8: case HxEncodingType.None: default: Result = Encoding.UTF8.GetString(input); break; } return(Result); }
// Verify a hash against a string. private static bool VerifyMd5Hash(System.Security.Cryptography.MD5 md5Hash, string input, string hash, HxEncodingType encodingType = HxEncodingType.None) { // Hash the input. string hashOfInput = GetMd5Hash(md5Hash, input, encodingType); // Create a StringComparer an compare the hashes. StringComparer comparer = StringComparer.OrdinalIgnoreCase; if (0 == comparer.Compare(hashOfInput, hash)) { return(true); } else { return(false); } }
private static string GetMd5Hash(System.Security.Cryptography.MD5 md5Hash, string input, HxEncodingType encodingType = HxEncodingType.None) { // Convert the input string to a byte array and compute the hash. //byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); //byte[] data = md5Hash.ComputeHash(Encoder.Default.GetBytes(input)); byte[] data = md5Hash.ComputeHash(GetString2Bytes(input, encodingType)); return(GetBytes2String(data, "x2")); //byte[] dataEA = md5Hash.ComputeHash(Encoding.ASCII.GetBytes(input)); //byte[] dataE7 = md5Hash.ComputeHash(Encoding.UTF7.GetBytes(input)); //byte[] dataE8 = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); //byte[] dataEU = md5Hash.ComputeHash(Encoding.Unicode.GetBytes(input)); //byte[] dataE32 = md5Hash.ComputeHash(Encoding.UTF32.GetBytes(input)); //byte[] dataED = md5Hash.ComputeHash(Encoding.Default.GetBytes(input)); //byte[] dataAA = md5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(input)); //byte[] dataA7 = md5Hash.ComputeHash(ASCIIEncoding.UTF7.GetBytes(input)); //byte[] dataA8 = md5Hash.ComputeHash(ASCIIEncoding.UTF8.GetBytes(input)); //byte[] dataAU = md5Hash.ComputeHash(ASCIIEncoding.Unicode.GetBytes(input)); //byte[] dataA32 = md5Hash.ComputeHash(ASCIIEncoding.UTF32.GetBytes(input)); //byte[] dataAD = md5Hash.ComputeHash(ASCIIEncoding.Default.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. /* * StringBuilder sBuilder = new StringBuilder(); * * // Loop through each byte of the hashed data * // and format each one as a hexadecimal string. * for (int i = 0; i < data.Length; i++) * { * sBuilder.Append(data[i].ToString("x2")); * } * * // Return the hexadecimal string. * return sBuilder.ToString(); */ }