} // func PasswordCompare public static bool PasswordCompare(string testPassword, byte[] passwordHash) { if (passwordHash == null) { return(String.IsNullOrEmpty(testPassword)); } if (passwordHash.Length < 6) { throw new ArgumentException("invalid hash-length", nameof(passwordHash)); } if (BitConverter.ToInt16(passwordHash, 0) != 2) { throw new ArgumentException("invalid hash-version", nameof(passwordHash)); } var testPasswordBytes = Encoding.Unicode.GetBytes(testPassword); // create the SHA256 hash (Password + Salt) var sha = SHA512Managed.Create(); sha.TransformBlock(testPasswordBytes, 0, testPasswordBytes.Length, testPasswordBytes, 0); sha.TransformFinalBlock(passwordHash, 2, 4); return(Procs.CompareBytes(sha.Hash, 0, passwordHash, 6, sha.HashSize / 8)); } // func PasswordCompare
} // func TryGetProperty /// <summary>Versucht einen Paremter zurückzugeben.</summary> /// <typeparam name="T">Rückgabewert.</typeparam> /// <param name="name">Parametername.</param> /// <param name="value">Wert der abgelegt wurde.</param> /// <returns><c>true</c>, wenn ein Wert gefunden wurde.</returns> public static bool TryGetProperty <T>(this IPropertyReadOnlyDictionary propertyDictionary, string name, out T value) { object ret; if (propertyDictionary.TryGetProperty(name, out ret) && ret != null) { try { if (ret is T) { value = (T)ret; } else { value = (T)Procs.ChangeType(ret, typeof(T)); } return(true); } catch (FormatException) { } } value = default(T); return(false); } // func TryGetProperty
} // func EncodeWindowsPassword public static SecureString DecodePassword(string passwordValue) { if (String.IsNullOrEmpty(passwordValue)) { return(null); } if (passwordValue.Length > 5 && passwordValue[5] == ':') { var pwdType = passwordValue.Substring(0, 5); switch (pwdType) { case "win0x": return(DecodeWindowsPassword(Procs.ConvertToBytes(passwordValue, 6, passwordValue.Length - 6), true)); case "win64": return(DecodeWindowsPassword(Convert.FromBase64String(passwordValue.Substring(6, passwordValue.Length - 6)), true)); case "usr0x": return(DecodeWindowsPassword(Procs.ConvertToBytes(passwordValue, 6, passwordValue.Length - 6), false)); case "usr64": return(DecodeWindowsPassword(Convert.FromBase64String(passwordValue.Substring(6, passwordValue.Length - 6)), false)); case "plain": return(passwordValue.CreateSecureString(6, passwordValue.Length - 6)); default: throw new ArgumentOutOfRangeException("passwordType", pwdType, "Invalid password type."); } } else { return(passwordValue.CreateSecureString()); } } // func DecodePassword
} // ctor public PropertyValue(string name, Type type, object value) { if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } this.name = name; this.type = type ?? typeof(object); this.value = Procs.ChangeType(value, this.type); } // ctor
} // func ToXml private static object GetValue(XElement x) { var type = LuaType.GetType(x.Attribute("t")?.Value ?? "string", lateAllowed: false).Type; if (type == typeof(LuaTable)) { return(CreateLuaTable(x)); } else { return(Procs.ChangeType(x.Value, type)); } } // func GetValue
} // func EncodePassword #endregion #region -- Password Hash ------------------------------------------------------ public static byte[] ParsePasswordHash(string passwordHash) { if (String.IsNullOrEmpty(passwordHash)) { return(null); } if (passwordHash.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { return(Procs.ConvertToBytes(passwordHash, 2, passwordHash.Length - 2)); } else { return(Convert.FromBase64String(passwordHash)); } } // func ParsePasswordHash
} // func GetAttribute /// <summary>Gibt den Inhalt eines Attributes typiriesiert zurück.</summary> /// <typeparam name="T">Datentyp der erwartet wird.</typeparam> /// <param name="x">XElement, an dem das Attribut erwartet wird.</param> /// <param name="attributeName">Name des Attributes.</param> /// <param name="default">Defaultwert, falls das Attribut nicht vorhanden ist oder der Wert nicht in den Typ konvertiert werden konnte.</param> /// <returns>Wert oder der default-Wert.</returns> public static T GetAttribute <T>(this XElement x, XName attributeName, T @default) { try { string sValue = GetAttribute(x, attributeName, (string)null); if (sValue == null) { return(@default); } return(Procs.ChangeType <T>(sValue)); } catch { return(@default); } } // func GetAttribute
} // func DecodePassword public static string EncodePassword(SecureString password, string passwordType = null) { if (passwordType == null) { passwordType = "win64"; } var passwordPtr = Marshal.SecureStringToGlobalAllocUnicode(password); var passwordSize = password.Length * 2; try { switch (passwordType) { case "win0x": return("win0x:" + Procs.ConvertToString(EncodeWindowsPassword(passwordPtr, passwordSize, true))); case "win64": return("win64:" + Convert.ToBase64String(EncodeWindowsPassword(passwordPtr, passwordSize, true))); case "usr0x": return("usr0x:" + Procs.ConvertToString(EncodeWindowsPassword(passwordPtr, passwordSize, false))); case "usr64": return("usr64:" + Convert.ToBase64String(EncodeWindowsPassword(passwordPtr, passwordSize, false))); case "plain": return("plain:" + Marshal.PtrToStringUni(passwordPtr, password.Length)); default: throw new ArgumentOutOfRangeException(nameof(passwordType), passwordType, "Invalid password type."); } } finally { Marshal.ZeroFreeGlobalAllocUnicode(passwordPtr); } } // func EncodePassword
} // ctor /// <summary>Erzeugt ein Dictionary.</summary> /// <param name="sParameterList">Parameterliste als Zeichenfolge konvertiert.</param> public PropertyDictionary(string parameterList) { AddRange(Procs.SplitPropertyList(parameterList)); } // ctor
} // func ChangeType /// <summary>Generische Implementierung von <c>ChangeType</c>.</summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <returns></returns> public static T ChangeType <T>(this object value) => (T)Procs.ChangeType(value, typeof(T));
public void Except(string message, Exception e) => Procs.LogMsg(logger, LogMsgType.Error, message, e);
public void Except(Exception e) => Procs.LogMsg(logger, LogMsgType.Error, e);
public void Warn(string message, Exception e) => Procs.LogMsg(logger, LogMsgType.Warning, message, e);
public void Warn(Exception e) => Procs.LogMsg(logger, LogMsgType.Warning, e);