Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
HenJigg committed Oct 25, 2021
1 parent d1e9f6a commit b99fd3c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
21 changes: 21 additions & 0 deletions MyToDo/MyToDo/MyToDo.Shared/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MyToDo.Shared
{
public static class StringExtensions
{
public static string GetMD5(this string data)
{
if (string.IsNullOrWhiteSpace(data))
throw new ArgumentNullException(nameof(data));

var hash = MD5.Create().ComputeHash(Encoding.Default.GetBytes(data));
return Convert.ToBase64String(hash);//将加密后的字节数组转换为加密字符串
}
}
}
55 changes: 55 additions & 0 deletions MyToDo/MyToDo/MyToDo/Common/PassWordExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.Xaml.Behaviors;
using System.Windows;
using System.Windows.Controls;

namespace MyToDo.Common
{
public class PassWordExtensions
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached("Password",
typeof(string), typeof(PassWordExtensions),
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));

private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
string password = (string)e.NewValue;
if (passwordBox != null && passwordBox.Password != password)
passwordBox.Password = password;
}

public static string GetPassword(DependencyObject dp)
{
return (string)dp.GetValue(PasswordProperty);
}

public static void SetPassword(DependencyObject dp, string value)
{
dp.SetValue(PasswordProperty, value);
}
}

public class PasswordBoxBehavior : Behavior<PasswordBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PasswordChanged += OnPasswordChanged;
}

private static void OnPasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
string password = PassWordExtensions.GetPassword(passwordBox);
if (passwordBox != null && passwordBox.Password != password)
PassWordExtensions.SetPassword(passwordBox, passwordBox.Password);
}

protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PasswordChanged -= OnPasswordChanged;
}
}
}

0 comments on commit b99fd3c

Please sign in to comment.