(04-10-2023, 15:34)tincho escribió: la dureza de una contraseña
' Gambas class file
Public Sub Form_Open()
' Example usage:
CheckPasswordStrength("MyP@ssw0rd")
End
Public Sub CheckPasswordStrength(password As String)
Dim length As Integer = Len(password)
Dim hasLowercase As Boolean = False
Dim hasUppercase As Boolean = False
Dim hasDigit As Boolean = False
Dim hasSpecialChar As Boolean = False
Dim specialChars As String = "!@#$%^&*()_+-=[]{}|;:,.<>?~"
For i As Integer = 0 To length - 1
If IsLower(password[i]) Then
hasLowercase = True
Else If IsUpper(password[i]) Then
hasUppercase = True
Else If IsDigit(password[i]) Then
hasDigit = True
Else If InStr(specialChars, password[i]) Then
hasSpecialChar = True
End If
Next
If length < 8 Or Not hasLowercase Or Not hasUppercase Or Not hasDigit Or Not hasSpecialChar Then
Print "The password is weak."
Else If length < 12 Then
Print "The password is moderate."
Else
Print "The password is strong."
End If
End Sub
' Public Sub CheckPasswordStrength(password As String)
' Dim length As Integer = Len(password)
' Dim hasLowercase As Boolean = False
' Dim hasUppercase As Boolean = False
' Dim hasDigit As Boolean = False
' Dim hasSpecialChar As Boolean = False
' Dim specialChars As String = "!@#$%^&*()_+-=[]{}|;:,.<>?~"
'
' For i As Integer = 0 To length - 1
' If Char.IsLower(password(i)) Then
' hasLowercase = True
' ElseIf Char.IsUpper(password(i)) Then
' hasUppercase = True
' ElseIf Char.IsDigit(password(i)) Then
' hasDigit = True
' ElseIf specialChars.Contains(password(i)) Then
' hasSpecialChar = True
' End If
' Next
'
' If length < 8 Or Not hasLowercase Or Not hasUppercase Or Not hasDigit Or Not hasSpecialChar Then
' Print "The password is weak."
' Else If length < 12 Then
' Print "The password is moderate."
' Else
' Print "The password is strong."
' End If
' End Sub
'
' ' Example usage:
' CheckPasswordStrength("MyP@ssw0rd")
' This code checks if the given password meets the following criteria:
'
' Has at least one lowercase letter.
' Has at least one uppercase letter.
' Has at least one digit.
' Has at least one special character (e.g., !, @, #, $, %, ^, &, *, (, ), _, +, -, =, [, ], {, }, |, ;, :, , ., <, >, ?, ~).
' Is at least 8 characters long.
' If the password meets all of these criteria, it is considered strong. If it does not meet all of these criteria but is at least 8 characters long, it is considered moderate. Otherwise, it is considered weak.
'
' I hope this helps!
(04-10-2023, 16:45)vuott escribió: ¿Es decir: "Seguridad de la contraseña" ?
(04-10-2023, 16:59)cogier escribió: Le pregunté a Bing AI cómo hacer esto en Gambas. El código que me dio no funcionaba, pero con algunos cambios se me ocurrió esto. El código de Bing está abajo. Como dijo Bing, ¡espero que esto ayude!
Código:
'' Password hardness level
Static Public Function Hardness(s As String) As Integer
Dim i As Integer
i = Len(s)
' Tiene un digito
If RegExp.Match(s, "[0-9]{2}") Then
Inc i
Else
Dec i
Endif
' Tiene 2 mayúsculas?
If RegExp.Match(s, "[A-Z]{2}") Then
Inc i
Else
Dec i
Endif
' Tiene 2 minúsculas?
If RegExp.Match(s, "[a-z]{2}") Then
Inc i
Else
Dec i
Endif
' Tiene 2 símbolos?
' If RegExp.Match(s, "^[a-z][A-Z][0-9]") Then
' Inc i
' Else
' Dec i
' Endif
Return i
End
Public Function Hardeness(sPass As String) As Integer
Dim r As Integer
Dim o As New JSONCollection
Dim i As Integer
Dim p As Integer
Dim q As Integer = 0
Dim rep As New JSONCollection
Dim aCUL As New String[] '' Consecutivas Upper and Lower
Dim cul As New JSONCollection
o["chars"] = String.Len(sPass) '' Número de Caracteres
o["length"] = 8
o["uppers"] = 0 '' Letras Mayúsculas
o["lowers"] = 0 '' Letras minúsculas
o["numbers"] = 0 '' Números
o["symbols"] = 0 '' Símbolos
cul.Add(0, "U")
cul.Add(0, "L")
cul.Add(0, "N")
cul.Add(0, "S")
For i = 1 To String.Len(sPass)
p = Asc(TextBox1.Text, i)
If p > 32 And p < 48 Then ' symbol
o["symbols"] = o["symbols"] + 1
aCUL.Add("S")
Else
If p > 57 And p < 65 Then
o["symbols"] = o["symbols"] + 1
aCUL.Add("S")
Else
If p > 64 And p < 91 Then ' upper
o["uppers"] = o["uppers"] + 1
aCUL.Add("U")
Else
If p > 96 And p < 123 Then ' lower
o["lowers"] = o["lowers"] + 1
aCUL.Add("L")
Else
If p > 47 And p < 58 Then ' number
o["numbers"] = o["numbers"] + 1
aCUL.Add("N")
Endif
Endif
Endif
Endif
Endif
If rep.Exist(CStr(p)) Then
rep[CStr(p)] = rep[CStr(p)] + 1
Else
rep[CStr(p)] = 1
Endif
Next
'' Adiciones
If o["chars"] > 0 Then
r = (o["chars"] * 4)
If o["uppers"] > 0 Then
r = r + ((o["chars"] - o["uppers"]) * 2)
Endif
If o["lowers"] > 0 Then
r = r + ((o["chars"] - o["lowers"]) * 2)
Endif
If o["numbers"] > 0 Then
r = r + (o["numbers"] * 4)
Endif
If o["symbols"] > 0 Then
r = r + (o["symbols"] * 4)
Endif
' Mitad números o simbolos
If o["symbols"] + o["numbers"] = (o["chars"] / 2) Then
r = r + ((o["symbols"] + o["numbers"]) * 2)
Endif
' Requerimientos
If o["chars"] >= o["length"] Then
Inc q
Endif
If o["uppers"] > 0 Then
Inc q
Endif
If o["lowers"] > 0 Then
Inc q
Endif
If o["numbers"] > 0 Then
Inc q
Endif
If o["symbols"] > 0 Then
Inc q
Endif
r = r + (q * 2)
Endif
'' Restas
'Solo letras
If o["numbers"] = 0 And o["symbols"] = 0 Then
r = r - o["chars"]
Endif
'Solo números
If o["lowers"] = 0 And o["symbols"] = 0 And o["uppers"] = 0 And o["numbers"] > 0 Then
r = r - o["numbers"]
Endif
'Caracteres repetidos (No sensible)
For Each i In rep
If i > 1 Then
r = r - (i * (i - 1))
Endif
Next
If aCUL.Count > 0 Then
For i = 0 To aCUL.Max - 1
If aCUL[ i ] = aCUL[ i + 1] Then
cul[aCUL[ i ]] = cul[aCUL[ i ] ] + 1
Endif
Next
Endif
'Letras mayúsuculas consecutivas
If cul["U"] > 0 Then
r = r - (cul["U"] * 2)
If cul["U"] > 3 Then
'Sencuencia de letras (3+)
r = r - (cul["U"] * 3)
Endif
Endif
'Letras minúsculas consecutivas
If cul["L"] > 0 Then
r = r - (cul["L"] * 2)
If cul["L"] > 3 Then
'Sencuencia de letras (3+)
r = r - (cul["L"] * 3)
Endif
Endif
'Números consecutivos
If cul["N"] > 0 Then
r = r - (cul["N"] * 2)
If cul["N"] > 3 Then
'Sencuencia de números (3+)
r = r - (cul["N"] * 3)
Endif
Endif
Return r
End
) As Float
' ***** Calcula fortaleza de una clave
' >>>>> sClave : Clave a examinar.
' >>>>> asGrp (*) : Grupos de caracteres que puede contener la clave.
' <<<<< Devuelve la fortaleza de la clave como Float
Dim fMax As Float = +8.98846567431105E+307 ' El máximo entero, para controlar errores
Dim fLogMax As Float = Log10(fMax) ' Logaritmo del anterior, por lo mismo
Dim iBase As Integer
Dim iExponente As Integer
Dim i, j As Integer
' Si no se incluyen grupos de caracteres, se toman los «estándard»
If IsNull(asGrp) Then
asGrp.Add("abcdefghaijklmnopqrstuwxyz")
asGrp.Add("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
asGrp.Add("0123456789")
asGrp.Add(" !\"#$%&\'()*+,-./:;<=>?@[\\]^_{|}~")
Endif
' Matriz para ver qué grupos están incluidos en la clave
Dim abGrp As New Boolean[asGrp.Count]
' El exponente es el número de caracteres de la clave
iExponente = String.Len(sClave)
' Para cada grupo se ve si hay uno de sus caracteres en la clave.
' Si es así, se marca ese grupo.
For i = 0 To iExponente
For j = 0 To asGrp.Max
If String.InStr(asGrp[j], sClave[i]) <> 0 Then
abGrp[j] = Si
Endif
Next
Next
' La base es el número de caracteres posible, esto es, la suma de los
' cardinales de cada conjunto de caracteres
For i = 0 To abGrp.Max
If abGrp[i] = Si Then iBase += asGrp[i].Len
Next
' Si no se incluyen caracteres posibles, devuelve «1»
If iBase = 0 Then Return 0
' Si el número obtenido es mayor que el mayor float posible,
' devuelve el log10 del máximo float
If iExponente * Log10(iBase) > fLogMax Then Return fLogMax
' Devuelve Log10(iBase^iExponente)
Return iExponente * Log10(iBase)
End