Imports System.Drawing
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Imports DevExpress.Utils.Svg
Imports EnvelopeGenerator.Common.Constants
Public Class Helpers
Private Shared key As String = "$xzBvyPETUS&amm8)D8x#)f;4%;?[BPd" ' Passwort-Schlüssel (16, 24, or 32 bytes)
Private Shared iv As String = "1wN&e[zrQ6_B7X/0" ' Initialisierungsvektor (16 bytes)
' Entschlüsselungsfunktion
Public Shared Function Decrypt(cipherText As String) As String
Dim aesAlg As Aes = Aes.Create()
aesAlg.Key = Encoding.UTF8.GetBytes(key)
aesAlg.IV = Encoding.UTF8.GetBytes(iv)
Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)
Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText)
Dim msDecrypt As New IO.MemoryStream(cipherBytes)
Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
Using srDecrypt As New IO.StreamReader(csDecrypt)
Return srDecrypt.ReadToEnd()
End Using
End Using
End Function
'''
''' Encodes the EnvelopeUUID and the ReceiverSignature into an EnvelopeKey
'''
'''
'''
''' The EnvelopeKey
Public Shared Function EncodeEnvelopeReceiverId(pEnvelopeUuid As String, pReceiverSignature As String) As String
Dim oString = $"{pEnvelopeUuid}::{pReceiverSignature}"
Dim oBytes = System.Text.Encoding.UTF8.GetBytes(oString)
Dim oBase64String = Convert.ToBase64String(oBytes)
Return oBase64String
End Function
'''
''' Decodes the EnvelopeKey and returns the EnvelopeUUID and the ReceiverSignature
'''
''' The EnvelopeKey
''' A tuple containing EnvelopeUUID and Receiver Signature
Public Shared Function DecodeEnvelopeReceiverId(pEnvelopeReceiverId As String) As Tuple(Of String, String)
Dim oBytes = Convert.FromBase64String(pEnvelopeReceiverId)
Dim oString = System.Text.Encoding.UTF8.GetString(oBytes)
Dim oSplit = oString.Split(New String() {"::"}, StringSplitOptions.None)
Return New Tuple(Of String, String)(oSplit(0), oSplit(1))
End Function
Public Shared Function GetEnvelopeURL(pHost As String, pEnvelopeUuid As String, pReceiverSignature As String) As String
Dim oEnvelopeUserReference As String = EncodeEnvelopeReceiverId(pEnvelopeUuid, pReceiverSignature)
Dim oURL As String = String.Format("{0}/EnvelopeKey/{1}", pHost.Trim(), oEnvelopeUserReference)
Return oURL
End Function
Public Shared Function GetAccessCode() As String
Return Guid.NewGuid().ToString("d").Substring(1, 6).ToUpper()
End Function
Public Shared Function ColorTypeToColor(pColorType As ColorType) As Color
Select Case pColorType
Case ColorType.ReceiverColor1
Return ColorTranslator.FromHtml("#22c55e")
Case ColorType.ReceiverColor2
Return ColorTranslator.FromHtml("#3b82f6")
Case ColorType.ReceiverColor3
Return ColorTranslator.FromHtml("#8b5cf6")
Case ColorType.ReceiverColor4
Return ColorTranslator.FromHtml("#f59e0b")
Case ColorType.ReceiverColor5
Return ColorTranslator.FromHtml("#ef4444")
Case ColorType.ReceiverColor6
Return ColorTranslator.FromHtml("#14b8a6")
Case ColorType.ReceiverColor7
Return ColorTranslator.FromHtml("#d946ef")
Case ColorType.ReceiverColor8
Return ColorTranslator.FromHtml("#06b6d4")
Case ColorType.ReceiverColor9
Return ColorTranslator.FromHtml("#10b981")
Case ColorType.ReceiverColor10
Return ColorTranslator.FromHtml("#84cc16")
End Select
End Function
Public Shared Function GetColorCircle(pBaseImage As SvgImage, pColor As Color) As SvgImage
Dim oColoredImage As SvgImage = pBaseImage.
Clone(Sub(el As SvgElement, table As Hashtable)
If TypeOf el Is SvgCircle Then
el.Styles.Clear()
table("StyleName") = String.Empty
table("Fill") = ColorTranslator.ToHtml(pColor)
End If
End Sub)
Return oColoredImage
End Function
Public Shared Function IsValidEmailAddress(pEmailAddress As String) As Boolean
Try
Dim oAddress = New System.Net.Mail.MailAddress(pEmailAddress)
Return oAddress.Address = pEmailAddress
Catch ex As Exception
Return False
End Try
End Function
Public Shared Async Function CopyFileAsync(sourceFile As String, destinationFile As String) As Task
Dim fileOptions = IO.FileOptions.Asynchronous Or IO.FileOptions.SequentialScan
Dim bufferSize = 4096
Using sourceStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, fileOptions)
Using destinationStream = New FileStream(destinationFile, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize, fileOptions)
Await sourceStream.CopyToAsync(destinationStream, bufferSize).ConfigureAwait(False)
End Using
End Using
End Function
End Class