Android Question Help Converting VB.NET to B4X

fbritop

Active Member
Licensed User
Longtime User
Hi,
I am doing a ONE TIME PASSWORD system for offline devices auth.
I tried to help myself from this post: https://www.b4x.com/android/forum/threads/calculate-hash.17449/#content
I have successfully implemented on the server side (VB.NET), but I have some trouble converting my VB.NET function to B4X, so if someone could give me some help regarding de MD5 functions:

VB.NET Code
B4X:
    Private  Function OTPGenerator(ByVal uniqueIdentity) As String
        Dim length = 5
        Dim oneTimePassword = ""
        Dim _strParsedReqNo = uniqueIdentity
        Using md5 As MD5 = MD5.Create
            Dim _reqByte As Byte() = md5.ComputeHash(Encoding.UTF8.GetBytes(_strParsedReqNo))
            Dim _parsedReqNo = BitConverter.ToInt32(_reqByte, 0)
            response.Write("_parsedReqNo:" & _parsedReqNo & "<br>")
            Dim _strParsedReqId = Math.Abs(_parsedReqNo).ToString

            If _strParsedReqId.Length < 9 Then
                Dim sb = New StringBuilder(_strParsedReqId)

                For k = 0 To 9 - _strParsedReqId.Length - 1
                    sb.Insert(0, "0"c)
                Next

                _strParsedReqId = sb.ToString
            End If

            oneTimePassword = _strParsedReqId
        End Using

        If oneTimePassword.Length >= length Then
            oneTimePassword = oneTimePassword.Substring(0, length)
        End If

        Return oneTimePassword
    End Function
B4X Code
B4X:
Sub OTPGenerator(uniqueIdentity As String) As String
    Dim oneTimePassword As String =""
    Try
        Dim length As Int=5
        Dim bc As ByteConverter
        Dim b64 As Base64
        Dim md As MessageDigest

        Dim data() As Byte
        Dim encrypted() As Byte

        data = bc.StringToBytes(uniqueIdentity, "utf-8")
        encrypted = md.GetMessageDigest(data, "MD5")
        Dim parsedReqNo As Int= bc.IntsFromBytes(encrypted)
        
        Dim strParsedReqId As String = Abs(parsedReqNo)
        If strParsedReqId.Length < 9 Then
            Dim sb As String = strParsedReqId

            For k = 0 To 9 - strParsedReqId.Length - 1
                sb="0" & sb 'sb.Insert(0, "0"c)
            Next

            strParsedReqId = sb
        End If

        oneTimePassword = strParsedReqId
        Log(oneTimePassword)
        Log(b64.EncodeBtoS(encrypted, 0, encrypted.Length))

        If oneTimePassword.Length >= length Then
            oneTimePassword = oneTimePassword.Substring2(0, length)
        End If
    Catch
        Log(LastException)
    End Try
    Return oneTimePassword
End Sub
 

stevel05

Expert
Licensed User
Longtime User
You don't say what problems you are having with the code.

As it stands it won't compile because bc.IntsFromBytes(encrypted) returns an int array and can't be cast to an Int. you can get the first int by appending (0):

B4X:
Dim parsedReqNo As Int= bc.IntsFromBytes(encrypted)(0)

As the variable it is assigned to is an int, this may give the result you are after. If this is not it, please describe the problems you are having.

You may also need to change the endieness of ByteConverter if it is cross platform as default for Android is BigEndian,
 
Last edited:
Upvote 0
Top