Forum VB .Net Auth code snippet for VB

Auth code snippet for VB

Postby Cyclone103 » February 21st, 2010, 6:24 pm

Code: Select all
Imports System
Imports System.Text
Imports System.Security.Cryptography

Namespace PlayerIOConnect
    Public Class PlayerIOAuth
        Public Shared Function Create(ByVal userId As String, ByVal sharedSecret As String) As String
            Dim unixTime As Integer = CInt((DateTime.UtcNow - New DateTime(1970, 1, 1)).TotalSeconds)
            Dim hmac = New HMACSHA1(Encoding.UTF8.GetBytes(sharedSecret)).ComputeHash(Encoding.UTF8.GetBytes((unixTime & ":") + userId))
            Return (unixTime & ":") + toHexString(hmac)
        End Function
       
        Private Shared Function toHexString(ByVal bytes As Byte()) As String
            Dim hex As New StringBuilder(bytes.Length * 2)
            For Each b As Byte In bytes
                hex.AppendFormat("{0:x2}", b)
            Next
            Return hex.ToString()
        End Function
       
        Private Shared Sub Main(ByVal args As String())
            Console.WriteLine("bob: " & Create("bob", "nobody can know this string"))
            Console.WriteLine("Joan1234: " & Create("Joan1234", "1234$@_abc"))
            Console.ReadLine()
        End Sub
    End Class
End Namespace


There you go
Cyclone103
 
Posts: 155
Joined: January 18th, 2010, 6:47 pm

Return to VB .Net