Create a gist now

Instantly share code, notes, and snippets.

VB.net example for Desk.com SSO
Public Class UserData
Public uid As String
Public expires As String = DateTime.Now.AddDays(1).ToString("s",
System.Globalization.CultureInfo.InvariantCulture)
Public customer_email As String
Public customer_name As String
End Class
Public Class MultiPass
Public status As Boolean = False
Public multipass As String
Public signature As String
End Class
Public Class Auth
Public Shared Function encryptUserData(user_data As UserData) As
MultiPass
Dim ret As New MultiPass
Dim site_key As String =
ConfigurationManager.AppSettings("desk.com_SiteKey")
Dim api_key As String =
ConfigurationManager.AppSettings("desk.com_ApiKey")
' Encode the data into a JSON object
Dim s As New JavaScriptSerializer()
Dim json_data As String = s.Serialize(user_data)
' Using byte arrays now instead of strings
Dim data As Byte() = Encoding.ASCII.GetBytes(json_data)
Dim iv As Byte() = Nothing
Dim encrypted As Byte() = Nothing
Dim prepended As Byte() = Nothing
Dim prepended_base64 As String = Nothing
Dim signature As Byte() = Nothing
'use the managed object to do the encryption..
Using aesAlg As New RijndaelManaged()
'define encryption details..
aesAlg.KeySize = 128
aesAlg.Mode = CipherMode.CBC
aesAlg.Padding = PaddingMode.PKCS7
'create the 16-byte salted hash (used as encryption key)..
Dim sha1__1 As SHA1 = SHA1.Create()
Dim saltedHash As Byte() =
sha1__1.ComputeHash(Encoding.ASCII.GetBytes(api_key & site_key))
Array.Resize(saltedHash, 16)
aesAlg.Key = saltedHash
'generate a random 16 byte IV..
aesAlg.GenerateIV()
iv = aesAlg.IV
'encrypt data..
Dim encryptor As ICryptoTransform =
aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
Using msEncrypt As New MemoryStream()
Using csEncrypt As New CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Write)
csEncrypt.Write(data, 0, data.Length)
csEncrypt.FlushFinalBlock()
End Using
encrypted = msEncrypt.ToArray()
End Using
End Using
'prepend encrypted data with the IV..
prepended = New Byte(iv.Length + (encrypted.Length - 1)) {}
iv.CopyTo(prepended, 0)
encrypted.CopyTo(prepended, iv.Length)
'base64 and url encode encrypted data..
prepended_base64 = Convert.ToBase64String(prepended,
Base64FormattingOptions.None)
ret.multipass = System.Web.HttpUtility.UrlEncode(prepended_base64)
'create signature..
Using myHMACSHA1 As New HMACSHA1(Encoding.ASCII.GetBytes(api_key))
signature =
myHMACSHA1.ComputeHash(Encoding.ASCII.GetBytes(prepended_base64))
ret.signature =
System.Web.HttpUtility.UrlEncode(Convert.ToBase64String(signature))
End Using
ret.status = True
End Function
End Class
@adam91holt

Hi,

What syntax highlight did you use for Gist for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment