143 lines
5.5 KiB
C#
143 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Security;
|
|
using Sarand.Net.Standard.Dbas.Data;
|
|
|
|
namespace Sarand.Net.Standard.Dbas.Security
|
|
{
|
|
//=======================================================================
|
|
internal partial class SaSecurityMgr_Dbas : SaSecurityMgr
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
internal SaSecurityMgr_Dbas(SaSecurityConsole_Dbas securityConsole) : base()
|
|
{
|
|
SecurityConsole = securityConsole;
|
|
DbInfo = new DbDataItem.DatabaseInfo(securityConsole.Database);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
internal SaSecurityConsole_Dbas SecurityConsole { get; }
|
|
internal ISacsDatabase Database { get { return SecurityConsole.Database; } }
|
|
private DbDataItem.DatabaseInfo DbInfo { get; }
|
|
public String TableName_SecItem { get { return SaSecurityDbDfn.GetTableName(SaSecurityDbDfn.TableIdx.SaSecItem); } }
|
|
public String TableName_SecLink { get { return SaSecurityDbDfn.GetTableName(SaSecurityDbDfn.TableIdx.SaSecLink); } }
|
|
public override bool DatabaseIsValid { get { return (Database != null && Database.GetTableNames().Contains(TableName_SecItem) && Database.GetTableNames().Contains(TableName_SecLink)); } }
|
|
public override bool CanUpdateDatabase { get { return DatabaseIsValid; } }
|
|
|
|
// Database utilities.
|
|
public override bool ColumnExists(SaSecurityDbDfn.ColumnIdx columnIdx)
|
|
{
|
|
String tableName = SaSecurityDbDfn.GetTableName(SaSecurityDbDfn.GetTableForColumn(columnIdx), false);
|
|
String columnName = SaSecurityDbDfn.GetColumnName(columnIdx, false);
|
|
return Database.ColumnExists(tableName, columnName);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Password utilities.
|
|
|
|
public override bool PasswordIsBlank(SaSecurityItem securityItemUser)
|
|
{
|
|
if (securityItemUser == null) return false;
|
|
SaSecurity.PasswordMethod method = securityItemUser.GetPasswordMethod();
|
|
// MsSql method.
|
|
if (method == SaSecurity.PasswordMethod.MsSql)
|
|
{
|
|
//...
|
|
}
|
|
// Legacy.
|
|
#pragma warning disable CS0618 // Deprecated property
|
|
return EncryptPassword_Legacy("").Equals(securityItemUser.PasswordEncrypted);
|
|
#pragma warning restore CS0618 // Deprecated property
|
|
}
|
|
|
|
public override bool PasswordMatches(SaSecurityItem securityItemUser, String password)
|
|
{
|
|
if (securityItemUser == null) return false;
|
|
SaSecurity.PasswordMethod method = securityItemUser.GetPasswordMethod();
|
|
// MsSql method.
|
|
if (method == SaSecurity.PasswordMethod.MsSql)
|
|
{
|
|
//...
|
|
}
|
|
// Legacy.
|
|
#pragma warning disable CS0618 // Deprecated property
|
|
return (EncryptPassword_Legacy(password).Equals(securityItemUser.PasswordEncrypted));
|
|
#pragma warning restore CS0618 // Deprecated property
|
|
}
|
|
|
|
/// <summary>Store the password against against the user.</summary>
|
|
public override bool SetPassword(SaSecurityItem securityItemUser, String password)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
internal static String EncryptPassword_Legacy(String password)
|
|
{
|
|
// We know this is not very water-tight, but it at least
|
|
// hides the contents of the password from prying eyes.
|
|
String toBeEncrypted = password;
|
|
// Password length goes in as first character:
|
|
toBeEncrypted = ByteToString((byte)(128 - password.Length)) + toBeEncrypted;
|
|
toBeEncrypted = string.Concat(toBeEncrypted.Reverse());
|
|
// Pad out to fixed length (minimum 16, multiples of 8):
|
|
int fixedLength = 16;
|
|
if (password.Length / 8 > 2) fixedLength = 8 * (int)(password.Length / 8);
|
|
while (toBeEncrypted.Length < fixedLength)
|
|
{
|
|
char charToAdd = (char)(password.Length > 0 ? password[(toBeEncrypted.Length % password.Length)] : 'A' + (toBeEncrypted.Length * 13) % 23);
|
|
if (toBeEncrypted.Length % 2 != 0)
|
|
toBeEncrypted += charToAdd;
|
|
else toBeEncrypted = charToAdd + toBeEncrypted;
|
|
}
|
|
|
|
// Now perform a simple algorythm on each character to hide its true value:
|
|
byte[] encryptedChars = new byte[toBeEncrypted.Length];
|
|
for (int i = 0; i < toBeEncrypted.Length; i++)
|
|
{
|
|
int iData = toBeEncrypted[i];
|
|
iData += 17 + i;
|
|
if (iData > 255) iData -= (255 + 32);
|
|
encryptedChars[i] = (byte)iData;
|
|
}
|
|
String result = Encoding_Ansi1252.GetString(encryptedChars);
|
|
#if DEBUG
|
|
// Password: mgbgt
|
|
// To encrypt: tbmggtgbgm{btggm
|
|
// Encrypted: 133, 116, 128, 123, 124, 138, 126, 122, 128, 135, 150, 126, 145, 133, 134, 141
|
|
//Logger.Info(fn + "\n\tPassword: " + password + "\n\tTo encrypt: " + toBeEncrypted + "\n\tEncrypted: " + encryptedChars.Select(i => i.ToString()).ToList().Concat(", "));
|
|
//Logger.Debug(password + ": " + result);
|
|
#endif
|
|
return result;
|
|
}
|
|
private static byte[] PasswordStringToBytes(String value)
|
|
{
|
|
byte[] values = new byte[value.Length];
|
|
for (int idx = 0; idx < value.Length; idx++)
|
|
{
|
|
char c = value[idx];
|
|
UInt32 ui = Convert.ToUInt32(c);
|
|
SaLogger.Info(idx + ". " + ui.ToString("x8"));
|
|
values[idx] = (byte)(c);
|
|
}
|
|
return values;
|
|
}
|
|
private static readonly Encoding Encoding_Ansi1252 = Encoding.GetEncoding(1252);
|
|
private static String ByteToString(byte value)
|
|
{
|
|
return Encoding_Ansi1252.GetString(new[] { value });
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|