Código
' *********************************************************************** ' Author : ElektroStudios ' Modified : 12-July-2023 ' *********************************************************************** #Region " Public Members Summary " #Region " Functions " ' MaskSecureString(String, Opt: Char) As SecureString ' MaskSecureString(String, Char(), Opt: Char) As SecureString ' MaskSecureString(String, Integer, Boolean, Opt: Char) As SecureString ' MaskSecureString(String, Integer, Boolean, Char(), Opt: Char) As SecureString ' MaskSecureString(SecureString, Opt: Char) As SecureString ' MaskSecureString(SecureString, Char(), Opt: Char) As SecureString ' MaskSecureString(SecureString, Integer, Boolean, Opt: Char) As SecureString ' MaskSecureString(SecureString, Integer, Boolean, Char(), Opt: Char) As SecureString #End Region #End Region #Region " Option Statements " Option Strict On Option Explicit On Option Infer Off #End Region #Region " Imports " Imports System.Text Imports System.Security Imports System.ComponentModel Imports System.Runtime.InteropServices Imports System.Collections.Generic Imports System.Linq #End Region #Region " UtilPasswords " ' ReSharper disable once CheckNamespace Namespace DevCase.Core.Security.Passwords Public NotInheritable Class UtilPasswords #Region " Public Methods " ''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Masks the source string with a specific character. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code language="VB.NET"> ''' Dim password As String = "This is a password" ''' Dim maskChar As Char = "*"c ''' ''' Dim masked As SecureString = MaskSecureString(password, maskChar) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="input"> ''' The string to mask. ''' </param> ''' ''' <param name="maskCharacter"> ''' Optional. The character used for masking (default: "*"). ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' The masked string. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepperBoundary> Public Shared Function MaskSecureString(input As String, Optional maskCharacter As Char = "*"c) As SecureString Return MaskSecureString(input, maskLength:=input.Length, leftToRight:=True, allowedChars:=Nothing, maskCharacter) End Function ''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Masks the source string with a specific character, ''' allowing certain characters to remain unmasked. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code language="VB.NET"> ''' Dim serialKey As String = "123-456-789" ''' Dim allowedChars As Char() = "-".ToCharArray() ''' Dim maskChar As Char = "*"c ''' ''' Dim masked As SecureString = MaskSecureString(serialKey, allowedChars, maskChar) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="input"> ''' The string to mask. ''' </param> ''' ''' <param name="allowedChars"> ''' An array of characters that are allowed to remain unmasked. ''' </param> ''' ''' <param name="maskCharacter"> ''' The character used for masking (default: "*"). ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' The masked string. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepperBoundary> Public Shared Function MaskSecureString(input As String, allowedChars As Char(), Optional maskCharacter As Char = "*"c) As SecureString Return MaskSecureString(input, maskLength:=input.Length, leftToRight:=True, allowedChars:=allowedChars, maskCharacter) End Function ''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Partially masks the source string with a specific character. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code language="VB.NET"> ''' Dim serialKey As String = "123-456-789" ''' Dim maskLength As Integer = 7 ''' Dim leftToRight As Boolean = True ''' Dim maskChar As Char = "*"c ''' ''' Dim masked As SecureString = MaskSecureString(serialKey, maskLength, leftToRight, maskChar) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="input"> ''' The string to mask. ''' </param> ''' ''' <param name="maskLength"> ''' The length of the mask. ''' </param> ''' ''' <param name="leftToRight"> ''' Indicates the direction of the mask (left to right or right to left). ''' </param> ''' ''' <param name="maskCharacter"> ''' The character used for masking (default: "*"). ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' The masked string. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepperBoundary> Public Shared Function MaskSecureString(input As String, maskLength As Integer, leftToRight As Boolean, Optional maskCharacter As Char = "*"c) As SecureString Return MaskSecureString(input, maskLength:=maskLength, leftToRight:=leftToRight, allowedChars:=Nothing, maskCharacter) End Function ''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Partially masks the source string with a specific character, ''' allowing certain characters to remain unmasked. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code language="VB.NET"> ''' Dim serialKey As String = "123-456-789" ''' Dim maskLength As Integer = 7 ''' Dim leftToRight As Boolean = True ''' Dim allowedChars As Char() = "-".ToCharArray() ''' Dim maskChar As Char = "*"c ''' ''' Dim masked As SecureString = MaskSecureString(serialKey, maskLength, leftToRight, allowedChars, maskChar) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="input"> ''' The string to mask. ''' </param> ''' ''' <param name="maskLength"> ''' The length of the mask. ''' </param> ''' ''' <param name="leftToRight"> ''' Indicates the direction of the mask (left to right or right to left). ''' </param> ''' ''' <param name="allowedChars"> ''' An array of characters that are allowed to remain unmasked. ''' </param> ''' ''' <param name="maskCharacter"> ''' The character used for masking (default: "*"). ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' The masked string. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepperBoundary> Public Shared Function MaskSecureString(input As String, maskLength As Integer, leftToRight As Boolean, allowedChars As Char(), Optional maskCharacter As Char = "*"c) As SecureString If String.IsNullOrEmpty(input) Then Throw New ArgumentNullException(paramName:=NameOf(input)) End If If String.IsNullOrEmpty(maskCharacter) Then Throw New ArgumentNullException(paramName:=NameOf(maskCharacter)) End If If maskLength <= 0 Then Throw New ArgumentException($"maskLength must be greather than zero.", paramName:=NameOf(maskLength)) End If Dim valueLength As Integer = input.Length If maskLength > valueLength Then Throw New ArgumentException($"maskLength can't be greather than the source string length.", paramName:=NameOf(maskLength)) End If Dim allowedCharIndices As IDictionary(Of Integer, Char) = Nothing If allowedChars IsNot Nothing AndAlso allowedChars.Length > 0 Then Dim startPos As Integer Dim endPos As Integer If maskLength = valueLength Then ' Full mask. startPos = 0 endPos = valueLength - 1 Else If leftToRight Then ' Left to right mask. startPos = 0 endPos = maskLength - 1 Else ' Right to left mask. startPos = valueLength - maskLength endPos = valueLength - 1 End If End If For i As Integer = startPos To endPos Dim c As Char = input(i) If allowedChars.Contains(c) Then allowedCharIndices.Add(i, c) End If Next End If Dim sec As New SecureString() If maskLength = valueLength Then ' Full mask. For i As Integer = 0 To valueLength Dim dictValue As Char = Nothing If allowedCharIndices IsNot Nothing AndAlso allowedCharIndices.TryGetValue(i, dictValue) Then sec.AppendChar(dictValue) Continue For End If sec.AppendChar(maskCharacter) Next Else If leftToRight Then ' Left to right mask. For i As Integer = 0 To maskLength - 1 Dim dictValue As Char = Nothing If allowedCharIndices IsNot Nothing AndAlso allowedCharIndices.TryGetValue(i, dictValue) Then sec.AppendChar(dictValue) Continue For End If sec.AppendChar(maskCharacter) Next For i As Integer = maskLength To valueLength - 1 sec.AppendChar(input(i)) Next Else ' Right to left mask. For i As Integer = 0 To valueLength - maskLength - 1 sec.AppendChar(input(i)) Next For i As Integer = valueLength - maskLength To valueLength - 1 Dim dictValue As Char = Nothing If allowedCharIndices IsNot Nothing AndAlso allowedCharIndices.TryGetValue(i, dictValue) Then sec.AppendChar(dictValue) Continue For End If sec.AppendChar(maskCharacter) Next End If End If Return sec End Function ''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Masks the source string with a specific character. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code language="VB.NET"> ''' Dim secureStr As New SecureString() ''' With secureStr ''' .AppendChar("p"c) ''' .AppendChar("a"c) ''' .AppendChar("s"c) ''' .AppendChar("s"c) ''' .AppendChar("w"c) ''' .AppendChar("o"c) ''' .AppendChar("r"c) ''' .AppendChar("d"c) ''' End With ''' ''' Dim maskChar As Char = "*"c ''' Dim masked As SecureString = MaskSecureString(secureStr, maskChar) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="value"> ''' The string to mask. ''' </param> ''' ''' <param name="maskCharacter"> ''' Optional. The character used for masking (default: "*"). ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' The masked string. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepperBoundary> Public Shared Function MaskSecureString(value As SecureString, Optional maskCharacter As Char = "*"c) As SecureString Return MaskSecureString(value, maskLength:=value.Length, leftToRight:=True, allowedChars:=Nothing, maskCharacter) End Function ''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Masks the source string with a specific character, ''' allowing certain characters to remain unmasked. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code language="VB.NET"> ''' Dim serialKey As New SecureString() ''' With serialKey ''' .AppendChar("1"c) ''' .AppendChar("2"c) ''' .AppendChar("3"c) ''' .AppendChar("-"c) ''' .AppendChar("4"c) ''' .AppendChar("5"c) ''' .AppendChar("6"c) ''' .AppendChar("-"c) ''' .AppendChar("7"c) ''' .AppendChar("8"c) ''' .AppendChar("9"c) ''' End With ''' ''' Dim allowedChars As Char() = "-".ToCharArray() ''' Dim maskChar As Char = "*"c ''' ''' Dim masked As SecureString = MaskSecureString(serialKey, allowedChars, maskChar) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="value"> ''' The string to mask. ''' </param> ''' ''' <param name="allowedChars"> ''' An array of characters that are allowed to remain unmasked. ''' </param> ''' ''' <param name="maskCharacter"> ''' The character used for masking (default: "*"). ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' The masked string. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepperBoundary> Public Shared Function MaskSecureString(value As SecureString, allowedChars As Char(), Optional maskCharacter As Char = "*"c) As SecureString Return MaskSecureString(value, maskLength:=value.Length, leftToRight:=True, allowedChars:=allowedChars, maskCharacter) End Function ''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Partially masks the source string with a specific character. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code language="VB.NET"> ''' Dim serialKey As New SecureString() ''' With serialKey ''' .AppendChar("1"c) ''' .AppendChar("2"c) ''' .AppendChar("3"c) ''' .AppendChar("-"c) ''' .AppendChar("4"c) ''' .AppendChar("5"c) ''' .AppendChar("6"c) ''' .AppendChar("-"c) ''' .AppendChar("7"c) ''' .AppendChar("8"c) ''' .AppendChar("9"c) ''' End With ''' ''' Dim maskLength As Integer = 7 ''' Dim leftToRight As Boolean = True ''' Dim maskChar As Char = "*"c ''' ''' Dim masked As SecureString = MaskSecureString(serialKey, maskLength, leftToRight, maskChar) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="value"> ''' The string to mask. ''' </param> ''' ''' <param name="maskLength"> ''' The length of the mask. ''' </param> ''' ''' <param name="leftToRight"> ''' Indicates the direction of the mask (left to right or right to left). ''' </param> ''' ''' <param name="maskCharacter"> ''' The character used for masking (default: "*"). ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' The masked string. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepperBoundary> Public Shared Function MaskSecureString(value As SecureString, maskLength As Integer, leftToRight As Boolean, Optional maskCharacter As Char = "*"c) As SecureString Return MaskSecureString(value, maskLength:=maskLength, leftToRight:=leftToRight, allowedChars:=Nothing, maskCharacter) End Function ''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Partially masks the source string with a specific character, ''' allowing certain characters to remain unmasked. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code language="VB.NET"> ''' Dim serialKey As New SecureString() ''' With serialKey ''' .AppendChar("1"c) ''' .AppendChar("2"c) ''' .AppendChar("3"c) ''' .AppendChar("-"c) ''' .AppendChar("4"c) ''' .AppendChar("5"c) ''' .AppendChar("6"c) ''' .AppendChar("-"c) ''' .AppendChar("7"c) ''' .AppendChar("8"c) ''' .AppendChar("9"c) ''' End With ''' ''' Dim maskLength As Integer = 7 ''' Dim leftToRight As Boolean = True ''' Dim allowedChars As Char() = "-".ToCharArray() ''' Dim maskChar As Char = "*"c ''' ''' Dim masked As SecureString = MaskSecureString(serialKey, maskLength, leftToRight, allowedChars, maskChar) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="value"> ''' The string to mask. ''' </param> ''' ''' <param name="maskLength"> ''' The length of the mask. ''' </param> ''' ''' <param name="leftToRight"> ''' Indicates the direction of the mask (left to right or right to left). ''' </param> ''' ''' <param name="allowedChars"> ''' An array of characters that are allowed to remain unmasked. ''' </param> ''' ''' <param name="maskCharacter"> ''' The character used for masking (default: "*"). ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' The masked string. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepperBoundary> Public Shared Function MaskSecureString(value As SecureString, maskLength As Integer, leftToRight As Boolean, allowedChars As Char(), Optional maskCharacter As Char = "*"c) As SecureString Dim managedString As String = ToManagedString(value) Return MaskSecureString(managedString, maskLength:=maskLength, leftToRight:=leftToRight, allowedChars:=allowedChars, maskCharacter:=maskCharacter) End Function #End Region #Region " Private Methods " ''' ---------------------------------------------------------------------------------------------------- ''' <summary> ''' Converts the source <see cref="Global.System.Security.SecureString"/> to a managed <see cref="String"/>. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <example> This is a code example. ''' <code language="VB.NET"> ''' Dim secStr As New SecureString() ''' With secStr ''' .AppendChar("q"c) ''' .AppendChar("w"c) ''' .AppendChar("e"c) ''' .AppendChar("r"c) ''' .AppendChar("t"c) ''' .AppendChar("y"c) ''' End With ''' ''' MessageBox.Show(secStr.ToManagedString()) ''' </code> ''' </example> ''' ---------------------------------------------------------------------------------------------------- ''' <param name="secureString"> ''' The source <see cref="Global.System.Security.SecureString"/>. ''' </param> ''' ---------------------------------------------------------------------------------------------------- ''' <returns> ''' The resulting <see cref="String"/>. ''' </returns> ''' ---------------------------------------------------------------------------------------------------- <DebuggerStepThrough> <EditorBrowsable(EditorBrowsableState.Never)> Private Shared Function ToManagedString(secureString As Global.System.Security.SecureString) As String If secureString Is Nothing Then Throw New ArgumentNullException(NameOf(secureString)) End If If secureString.Length = 0 Then Return "" Else Dim ptr As System.IntPtr = Global.System.IntPtr.Zero Try ptr = Marshal.SecureStringToGlobalAllocUnicode(secureString) Return Marshal.PtrToStringUni(ptr) Finally If ptr <> IntPtr.Zero Then Marshal.ZeroFreeGlobalAllocUnicode(ptr) End If End Try End If End Function #End Region End Class End Namespace #End Region