public void RecalculateHashes(Action <int> progress) { int count = imagesDict.Count * 2; int index = 0; foreach (Image image in imagesDict.Select(x => x.Value)) { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(image.path); image.hash = DHash.Calculate(bmp); bmp.Dispose(); ++index; progress((int)(100.0f * index / count)); } foreach (Image image in imagesDict.Select(x => x.Value)) { foreach (Image image2 in imagesDict.Select(x => x.Value)) { if (image != image2) { int dist = DHash.Distance(image.hash, image2.hash); if (dist < DHash.Margin && !duplicates.Any(x => x.image1 == image2 && x.image2 == image)) { duplicates.Add(new Duplicate { image1 = image, image2 = image2, dist = dist }); } } } ++index; progress((int)(100.0f * index / count)); } }
/// <summary> /// Hàm lưu mảng giá trị config /// </summary> /// <param name="strGroup">Key nhóm cần lưu</param> /// <param name="dic">Mảng giá trị config</param> /// <returns></returns> static public bool SetGroupValue(string strGroup, Dictionary <string, string> dic) { if (dic == null) { return(false); } if (dic.Count == 0) { return(false); } Xmlconfig xmlcg = new Xmlconfig(DDefault.PathFileConfig, true); foreach (var d in dic) { xmlcg.Settings[strGroup][d.Key].Value = DHash.Encrypt(d.Value, KeyPass); } try { xmlcg.Save(DDefault.PathFileConfig); return(true); } catch { return(false); } }
public ActionResult DecryptPass(string typepass, string content, string keypass) { if (typepass == "d") { ViewBag.StrEncrypt = content; if (string.IsNullOrEmpty(keypass)) { ViewBag.StrDecrypt = DHash.Decrypt(content); } else { ViewBag.StrDePass = keypass; ViewBag.StrDecrypt = DHash.Decrypt(content, keypass); } } else if (typepass == "e") { ViewBag.StrDecrypt = content; if (string.IsNullOrEmpty(keypass)) { ViewBag.StrEncrypt = DHash.Encrypt(content); } else { ViewBag.StrEnPass = keypass; ViewBag.StrEncrypt = DHash.Encrypt(content, keypass); } } return(View("__Cms/Config/DecryptPass")); }
public static string GetCookie(string key) { HttpCookie getCookie = HttpContext.Current.Request.Cookies[key]; if (getCookie != null) { return(DHash.Decrypt(getCookie.Value)); } else { return(null); } }
public static void SetCookie(string key, object value, int timehour = 5) { // create a cookie HttpCookie newCookie = new HttpCookie(key, DHash.Encrypt(value.ToString())); if (timehour == 5) { timehour = DDefault.HourCookiesSession; } newCookie.Expires = DateTime.Now.AddHours(timehour); newCookie.HttpOnly = true; HttpContext.Current.Response.Cookies.Add(newCookie); }
/// <summary> /// Hàm lưu giá trị config /// </summary> /// <param name="strKey">Key thuộc tính</param> /// <param name="strValue">Giá trị lưu (string)</param> /// <returns></returns> static public bool SetValue(string strKey, string strValue) { Xmlconfig xmlcg = new Xmlconfig(DDefault.PathFileConfig, true); xmlcg.Settings[strKey].Value = DHash.Encrypt(strValue, KeyPass); try { xmlcg.Save(DDefault.PathFileConfig); return(true); } catch { return(false); } }
/// <summary> /// Hàm lấy mảng các giá trị config /// <para>VD: "Roles/PermissionsAdmin"</para> /// </summary> /// <param name="strGroup">Key nhóm cần lấy</param> /// <returns></returns> static public Dictionary <string, string> GetGroupValue(string strGroup) { Xmlconfig xmlcg = new Xmlconfig(DDefault.PathFileConfig, false); if (xmlcg.Settings[strGroup].ChildCount(true) > 0) { Dictionary <string, string> arr = new Dictionary <string, string>(); foreach (ConfigSetting cs in xmlcg.Settings[strGroup].Children()) { arr[cs.Name] = DHash.Decrypt(cs.Value, KeyPass); } return(arr); } return(null); }
/// <summary> /// Hàm kiểm tra dành cho đăng nhập /// <para>Được sử riêng cho trang Login</para> /// </summary> /// <param name="username">Tài khoản</param> /// <param name="password">Mật khẩu</param> /// <param name="remember">Ghi nhớ tài khoản bằng Cookie</param> /// <returns>bool</returns> public static bool CheckLogin(string username, string password, bool remember = false) { if (username == DDefault.SAdminID && password == DDefault.SAdminPW) { //Setup quyền cho Supper Admin SetCookie("DUser", DDefault.SAdminID); ReloadInfoUser(); if (remember) { SetCookie(DDefault.NameCookieRemember, username, 24 * DDefault.DayCookiesLogin); } else { ClearCookie(DDefault.NameCookieRemember); } return(true); } //Check tài khoàn đăng nhập = database thông thường else { DBAdmin db = new DBAdmin(); password = DHash.Encrypt(password); tbUser tbUser = db.tbUsers.Where(p => p.Username == username && p.Password == password).FirstOrDefault(); if (tbUser == null) { return(false); } SetCookie("DUser", tbUser.Username); ReloadInfoUser(); if (remember) { SetCookie(DDefault.NameCookieRemember, username); } else { ClearCookie(DDefault.NameCookieRemember); } return(true); } }
private void dataGridView1_SelectionChanged(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count == 0) { return; } int index = dataGridView1.SelectedRows[0].Index, pos = 0; DuplicateItem item = items[index]; foreach (DuplicateItem item2 in items) { if (item == item2) { item2.Similarity = "---"; } else { int dist = DHash.Distance(item.image.hash, item2.image.hash); item2.Similarity = $"{DHash.ToSimilarity(dist)}%"; } if (!inInit) { bindingList.ResetItem(pos); ++pos; } } if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); } try { pictureBox1.Image = ImageLoader.Load(item.image.path); } catch (Exception) { pictureBox1.Image = null; } }
/// <summary> /// Hàm lấy giá trị config /// </summary> /// <param name="strKey">Key thuộc tính</param> /// <returns></returns> static public string GetValue(string strKey) { Xmlconfig xmlcg = new Xmlconfig(DDefault.PathFileConfig, false); return(DHash.Decrypt(xmlcg.Settings[strKey].Value, KeyPass)); }
public void SetUp() { string json = @"{""duck"":""quack"",""isADuck"":true,""age"":5,""foo"":null,""bar"":{baz:1},""numbers"":[0,1,3],""strs"":[""c"",""d""]}"; d = DHash.FromJson(json); }
public DialogResult Show(Image image1, Image image2, int?dist = null, int?complex = null) { this.image1 = image1; this.image2 = image2; pictureBox1.Image = ImageLoader.Load(image1.path); pictureBox2.Image = ImageLoader.Load(image2.path); DialogResult = DialogResult.None; textBox1.Text = $"File:\u200B{image1.Filename.TrimExt(100)} Size:\u200B{Utility.BytesToString(image1.size)} Resolution:\u200B{image1.Resolution}"; if (image1.score > 0) { textBox2.Text += $" Score:\u200B{db.GetScore(image1)}"; } textBox2.Text = $"File:\u200B{image2.Filename.TrimExt(100)} Size:\u200B{Utility.BytesToString(image2.size)} Resolution:\u200B{image2.Resolution}"; if (image2.score > 0) { textBox2.Text += $" Score:\u200B{db.GetScore(image2)}"; } if (dist.HasValue) { btBoth.Text = $"Keep both\n({DHash.ToSimilarity(dist.Value)}% similarity)"; if (image2.size >= image1.size && image2.ResolutionValue >= image1.ResolutionValue && complex == null) { btRight.Font = boldFont; btLeft.Font = normalFont; } else if (image1.size >= image2.size && image1.ResolutionValue >= image2.ResolutionValue && complex == null) { btLeft.Font = boldFont; btRight.Font = normalFont; } else { btLeft.Font = normalFont; btRight.Font = normalFont; } if (complex.HasValue) { btComplex.Enabled = true; btComplex.Text = $"Multi compare ({complex.Value})"; btComplex.Font = boldFont; } else { btComplex.Enabled = false; btComplex.Text = "Multi compare"; btComplex.Font = normalFont; } } Show(); while (DialogResult == DialogResult.None) { System.Threading.Thread.Sleep(10); Application.DoEvents(); } pictureBox1.Image.Dispose(); pictureBox1.Image = null; pictureBox2.Image.Dispose(); pictureBox2.Image = null; return(DialogResult); }
public void Scan(Action <int> progress) { missing.Clear(); exactDuplicates.Clear(); foreach (KeyValuePair <string, Image> kvp in imagesDict) { kvp.Value.found = false; } // scan for new files, mark existing string[] files = Directory.GetFiles(path); List <string> newFiles = new List <string>(); foreach (string file in files) { string filename = Path.GetFileName(file); string ext = Path.GetExtension(file); if (!excludedExt.Contains(ext)) { if (imagesDict.TryGetValue(filename, out Image image)) { image.found = true; } else { newFiles.Add(filename); } } } // add new files, check for duplicates if (newFiles.Count != 0) { int index = 0; foreach (string newFile in newFiles) { Image image = new Image { Filename = newFile, path = $"{path}\\{newFile}", found = true }; System.Drawing.Bitmap bmp = ImageLoader.Load(image.path); image.hash = DHash.Calculate(bmp); image.width = bmp.Width; image.height = bmp.Height; image.size = new FileInfo(image.path).Length; bmp.Dispose(); foreach (Image image2 in imagesDict.Select(x => x.Value).Where(x => x.found)) { int dist = DHash.Distance(image.hash, image2.hash); if (dist == 0 && image.size == image2.size && image.ResolutionValue == image2.ResolutionValue) { // exact duplicate, autodelete exactDuplicates.Add(image.path); image = null; break; } else if (dist < DHash.Margin && !duplicates.Any(x => x.image1 == image2 && x.image2 == image)) { duplicates.Add(new Duplicate { image1 = image, image2 = image2, dist = dist }); } } if (image != null) { imagesDict[newFile] = image; newImages.Add(image); } ++index; progress(100 * index / newFiles.Count); } } missing.AddRange(imagesDict.Select(x => x.Value).Where(x => !x.found)); }
public override void OnActionExecuting(ActionExecutingContext filter) { string ActionName = filter.RouteData.Values["action"].ToString().ToLower(); string ControllerName = filter.RouteData.Values["controller"].ToString().ToLower(); string AreaName = filter.RouteData.DataTokens["area"] != null ? filter.RouteData.DataTokens["area"].ToString().ToLower() : ""; string MethodName = filter.HttpContext.Request.HttpMethod; // GET AREAS string Areas = ""; string[] arrT = filter.Controller.GetType().ToString().Split('.'); if (arrT.Length >= 3) //DreamCMS.Areas.Admin.Controllers. ... { if (arrT[1] == "Areas") { Areas = arrT[2]; } } HttpCookie getCookie = HttpContext.Current.Request.Cookies[DDefault.NameCookieRemember]; if (getCookie != null) { if (Auth.GetCookie("DUser") == null) { Auth.SetCookie("DUser", DHash.Decrypt(getCookie.Value)); } if (filter.HttpContext.Session["DUserName"] == null) { ReloadInfoUser(); } } string DUser = Auth.GetCookie("DUser"); /// Kiểm tra các lỗi xảy ra /// Nếu không đúng phân vùng Area: Thông báo lỗi 404 if (Areas != "" && AreaName != Areas.ToLower()) { filter.HttpContext.Session["ErrorHttp"] = filter.HttpContext.Request.RawUrl; filter.Result = new RedirectResult(DDefault.Path404); return; } /// Nếu chưa đăng nhập /// Chuyển về trang đăng nhập if (DUser == null) { filter.HttpContext.Session["returnUrl"] = filter.HttpContext.Request.RawUrl; filter.Result = new RedirectResult(DDefault.PathLogin); return; } /// Đặc quyền cho SUPERADMIN if (DUser == DDefault.SAdminID) { filter.HttpContext.Session["ErrorHttp"] = ""; filter.HttpContext.Session["returnUrl"] = ""; if (!IsNotLayout) { IsNotLayout = false; LoadBreadcrumb(filter); LoadMenuLeft(filter); } base.OnActionExecuting(filter); return; } DBAdmin db = new DBAdmin(); int? GroupId = int.Parse(Auth.GetCookie("DGroupId")) as int?; tbMenuInGroup tbMenuInGroup = new tbMenuInGroup(db.tbMenus.Where(p => p.IsDisable == false && p.Controller.ToLower() == ControllerName && p.Action.ToLower() == ActionName).FirstOrDefault(), GroupId.Value); if (tbMenuInGroup.IsIn) { /// Trường hợp thỏa các điều kiện filter.HttpContext.Session["ErrorHttp"] = ""; filter.HttpContext.Session["returnUrl"] = ""; if (!IsNotLayout) { IsNotLayout = false; LoadBreadcrumb(filter); LoadMenuLeft(filter); } base.OnActionExecuting(filter); return; } else { /// Nếu tài khoản không đủ quyền /// Chuyển về trang thông báo quyền truy cập hoặc 404 HttpContext.Current.Session["ErrorHttp"] = filter.HttpContext.Request.RawUrl; filter.Result = new RedirectResult(DDefault.PathAccessDenied); return; } }
/// <summary> /// Hàm check quyền của control Admin /// </summary> /// <param name="ct">Control hiện tại (this)</param> /// <returns>int</returns> public static int IsUse(Controller ct, string area = "") { string ActionName = ct.ControllerContext.RouteData.Values["action"].ToString().ToLower(); string ControllerName = ct.ControllerContext.RouteData.Values["controller"].ToString().ToLower(); string AreaName = ct.ControllerContext.RouteData.DataTokens["area"] != null ? ct.ControllerContext.RouteData.DataTokens["area"].ToString().ToLower() : ""; string MethodName = ct.ControllerContext.HttpContext.Request.HttpMethod; //string Ip = IPAddress; HttpCookie getCookie = HttpContext.Current.Request.Cookies[DDefault.NameCookieRemember]; if (getCookie != null) { if (GetCookie("DUser") == null) { SetCookie("DUser", DHash.Decrypt(getCookie.Value)); if (HttpContext.Current.Session["DUserName"] == null) { ReloadInfoUser(); } } } string DUser = GetCookie("DUser"); /// Kiểm tra các lỗi xảy ra /// Nếu không đúng phân vùng Area: Thông báo lỗi 404 if (area != "" && AreaName != area.ToLower()) { HttpContext.Current.Session["ErrorHttp"] = ct.Request.RawUrl; _Auth = -1; return(-1); } /// Nếu chưa đăng nhập /// Chuyển về trang đăng nhập if (DUser == null) { HttpContext.Current.Session["returnUrl"] = ct.Request.RawUrl; _Auth = -2; return(-2); } /// Đặc quyền cho SUPERADMIN if (DUser == DDefault.SAdminID) { HttpContext.Current.Session["ErrorHttp"] = ""; HttpContext.Current.Session["returnUrl"] = ""; _Auth = 1; LoadBreadcrumb(ct); LoadMenuLeft(ct); return(1); } DBAdmin db = new DBAdmin(); int? GroupId = int.Parse(GetCookie("DGroupId")) as int?; tbMenuInGroup tbMenuInGroup = new tbMenuInGroup(db.tbMenus.Where(p => p.IsDisable == false && p.Controller.ToLower() == ControllerName && p.Action.ToLower() == ActionName).FirstOrDefault(), GroupId.Value); if (tbMenuInGroup.IsIn) { /// Trường hợp thỏa các điều kiện HttpContext.Current.Session["ErrorHttp"] = ""; HttpContext.Current.Session["returnUrl"] = ""; _Auth = 1; LoadBreadcrumb(ct); LoadMenuLeft(ct); return(1); } else { /// Nếu tài khoản không đủ quyền /// Chuyển về trang thông báo quyền truy cập hoặc 404 HttpContext.Current.Session["ErrorHttp"] = ct.Request.RawUrl; _Auth = 0; return(0); } }
public void FromJson_WihDoubleValue() { d = DHash.FromJson("{\"thing\":1.2345678912345679E+26}"); Assert.That((double)d.thing, Is.EqualTo(123456789123456789123456789.123456789)); }