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 Code
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:
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