RosettaCodeData/Task/OpenWebNet-password/FreeBASIC/openwebnet-password.basic

62 lines
1.8 KiB
Plaintext

Function ownCalcPass(password As String, nonce As String) As Integer
Dim As Boolean start = True
Dim As Integer b, num1 = 0, num2 = 0
For b = 0 To Len(nonce)
Dim As String c = Mid(nonce,b,1)
If c <> "0" And start Then
num2 = Cint(password)
start = False
End If
Select Case c
Case "1"
num1 = (num2 And &HFFFFFF80) Shr 7
num2 = num2 Shl 25
Case "2"
num1 = (num2 And &HFFFFFFF0) Shr 4
num2 = num2 Shl 28
Case "3"
num1 = (num2 And &HFFFFFFF8) Shr 3
num2 = num2 Shl 29
Case "4"
num1 = num2 Shl 1
num2 = num2 Shr 31
Case "5"
num1 = num2 Shl 5
num2 = num2 Shr 27
Case "6"
num1 = num2 Shl 12
num2 = num2 Shr 20
Case "7"
num1 = num2 And &H0000FF00 Or ((num2 And &H000000FF) Shl 24) Or ((num2 And &H00FF0000) Shr 16)
num2 = (num2 And &HFF000000) Shr 8
Case "8"
num1 = (num2 And &H0000FFFF) Shl 16 Or (num2 Shr 24)
num2 = (num2 And &H00FF0000) Shr 8
Case "9"
num1 = Not num2
Case Else
num1 = num2
End Select
num1 And= &HFFFFFFFF
num2 And= &HFFFFFFFF
If c <> "0" And c <> "9" Then num1 Or= num2
num2 = num1
Next b
Return num1
End Function
Sub testPasswordCalc(password As String, nonce As String, expected As Integer)
Dim As Integer res = ownCalcPass(password, nonce)
Print Iif(res = expected, "PASS ", "FAIL ");
Print Using "& & & ##########"; password; nonce; res; expected
End Sub
testPasswordCalc("12345", "603356072", 25280520)
testPasswordCalc("12345", "410501656", 119537670)
testPasswordCalc("12345", "630292165", 4269684735)
Sleep