1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-06-26 10:36:21 +02:00
bitwarden-mobile/src/App/Utilities/PasswordFormatter.cs

95 lines
3.0 KiB
C#
Raw Normal View History

2019-04-27 05:58:15 +02:00
using System;
using Xamarin.Forms;
namespace Bit.App.Utilities
{
/**
* Helper class to format a password with numeric encoding to separate
* normal text from numbers and special characters.
*/
class PasswordFormatter
{
/**
* This enum is used for the state machine when building the colorized
* password string.
*/
private enum CharType
{
None,
Normal,
Number,
Special
}
public static string FormatPassword(string password)
2019-04-27 05:58:15 +02:00
{
2019-06-07 16:31:41 +02:00
if(password == null)
{
return string.Empty;
2019-06-07 16:31:41 +02:00
}
// First two digits of returned hex code contains the alpha,
// which is not supported in HTML color, so we need to cut those out.
var numberColor = $"<span style=\"color:#{((Color)Application.Current.Resources["PasswordNumberColor"]).ToHex().Substring(2)}\">";
var specialColor = $"<span style=\"color:#{((Color)Application.Current.Resources["PasswordSpecialColor"]).ToHex().Substring(2)}\">";
var result = string.Empty;
2019-04-27 05:58:15 +02:00
// Start with an otherwise uncovered case so we will definitely enter the "something changed"
// state.
var currentType = CharType.None;
foreach(var c in password)
{
// First, identify what the current char is.
CharType charType;
if(char.IsLetter(c))
{
charType = CharType.Normal;
}
else if(char.IsDigit(c))
{
charType = CharType.Number;
}
else
{
charType = CharType.Special;
}
// If the char type changed, build a new span to append the text to.
if(charType != currentType)
{
// Close off previous span.
if (currentType != CharType.None && currentType != CharType.Normal)
{
result += "</span>";
}
2019-04-27 05:58:15 +02:00
currentType = charType;
// Switch the color if it is not a normal text. Otherwise leave the
// default value.
switch(currentType)
{
// Apply color style to span.
2019-04-27 05:58:15 +02:00
case CharType.Number:
result += numberColor;
2019-04-27 05:58:15 +02:00
break;
case CharType.Special:
result += specialColor;
2019-04-27 05:58:15 +02:00
break;
}
}
result += c;
2019-04-27 05:58:15 +02:00
}
// Close off last span.
if (currentType != CharType.None && currentType != CharType.Normal)
{
result += "</span>";
}
2019-04-27 05:58:15 +02:00
return result;
}
}
}