Verifique se há IMEI válido
Pergunta
Alguém sabe como verificar se há um IMEI válido?
Eu encontrei uma função para verificar nesta página: http://www.dotnetfunda.com/articles/article597-imeivalidator-in-vbnet-.aspx
Mas ele retorna false
Para IMEI válido (Fe 352972024585360
). Eu posso validá -los on -line nesta página: http://www.numberingplans.com/?page=analysis&sub=imeinr
Qual é a maneira correta (no vb.net) para verificar se um determinado IMEI é válido?
PS: Esta função da página acima deve estar incorreta de alguma forma:
Public Shared Function isImeiValid(ByVal IMEI As String) As Boolean
Dim cnt As Integer = 0
Dim nw As String = String.Empty
Try
For Each c As Char In IMEI
cnt += 1
If cnt Mod 2 <> 0 Then
nw += c
Else
Dim d As Integer = Integer.Parse(c) * 2 ' Every Second Digit has to be Doubled '
nw += d.ToString() ' Genegrated a new number with doubled digits '
End If
Next
Dim tot As Integer = 0
For Each ch As Char In nw.Remove(nw.Length - 1, 1)
tot += Integer.Parse(ch) ' Adding all digits together '
Next
Dim chDigit As Integer = 10 - (tot Mod 10) ' Finding the Check Digit my Finding the Remainder of the sum and subtracting it from 10 '
If chDigit = Integer.Parse(IMEI(IMEI.Length - 1)) Then ' Checking the Check Digit with the last digit of the Given IMEI code '
Return True
Else
Return False
End If
Catch ex As Exception
Return False
End Try
End Function
EDITAR: Este é o meu "checkimei" funcionando:
Public Shared Function checkIMEI(ByRef IMEI As String) As Boolean
Const allowed As String = "0123456789"
Dim cleanNumber As New System.Text.StringBuilder
For i As Int32 = 0 To IMEI.Length - 1
If (allowed.IndexOf(IMEI.Substring(i, 1)) >= 0) Then
cleanNumber.Append(IMEI.Substring(i, 1))
End If
Next
If cleanNumber.Length <> 15 Then
Return False
Else
IMEI = cleanNumber.ToString
End If
For i As Int32 = cleanNumber.Length + 1 To 16
cleanNumber.Insert(0, "0")
Next
Dim multiplier As Int32, digit As Int32, sum As Int32, total As Int32 = 0
Dim number As String = cleanNumber.ToString()
For i As Int32 = 1 To 16
multiplier = 1 + (i Mod 2)
digit = Int32.Parse(number.Substring(i - 1, 1))
sum = digit * multiplier
If (sum > 9) Then
sum -= 9
End If
total += sum
Next
Return (total Mod 10 = 0)
End Function
Solução
Os números IMEI são validados usando o Luhn algoritmo. A página vinculada possui implementações em vários idiomas. Esta postagem Também possui mais algumas implementações e uma metodologia geral sobre como resolver o algoritmo Luhn.
Outras dicas
O IMEISV (versão do software IMEI) não possui o dígito de verificação do algoritmo Luhn. Em vez disso, possui o número da versão do software apresentado com dois dígitos. O formato do número IMEI vem evoluindo há anos.
Wikipedia É a boa fonte que acho que ver as mudanças no formato por anos. Se você procurar o novo formato de versão IMEI e IMEISV, verá que o Código FAC (FAC) removido e o código de alocação de tipo (TAC) aumentou o 8 dígito. Há gratuitamente e listas comerciais de TAC existem na internet.
Pode ser uma opção validar o número TAC, procurando as listas TAC no topo da validação do algoritmo Luhn para números novos e antigos da IMEI. Para números IMEI antigos, FAC, pois 2 dígitos devem ser descartados e a validação do TAC deve ser feita por 6 dígitos.