Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '' ************************************************* ''
- '' * Original utility released by RainbowTech @ SL * ''
- '' * Re-coded and cleaned up by .Shebang @ SL * ''
- '' ************************************************* ''
- Option Strict On
- Imports System.Net
- Imports System.IO
- Imports System.Text
- Imports System.Text.RegularExpressions
- Module Tools
- Private LetterChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- Private NumberChars As String = "1234567890"
- Private SymbolChars As String = "`~!@#$%^&*()_-+={[}]|\:;'""<,>.?/"
- ''' <summary>
- ''' Creates a random string based on the length of the string, and
- ''' the character set defined by the programmer.
- ''' </summary>
- ''' <param name="StringLength">The length of the random string.</param>
- ''' <param name="CustomCharacters">Any custom characters not included in the default character set.</param>
- ''' <param name="Letters">True if the programmer wants to include letters.</param>
- ''' <param name="Numbers">True if the programmer wants to include numbers.</param>
- ''' <param name="Symbols">True if the programmer wants to include symbols.</param>
- ''' <param name="UpperCase">Depends on Letters' value. True if the programmer wants uppercase letters.</param>
- ''' <param name="LowerCase">Depends on Letters' value. True if the programmer wants lowercase letters.</param>
- ''' <returns>Random string of length StringLength.</returns>
- ''' <remarks></remarks>
- Public Function RandomString(ByVal StringLength As Integer, Optional ByVal CustomCharacters As String = "", Optional ByVal Letters As Boolean = True, Optional ByVal Numbers As Boolean = True, Optional ByVal Symbols As Boolean = False, Optional ByVal UpperCase As Boolean = True, Optional ByVal LowerCase As Boolean = True) As String
- Dim Charset As String = String.Empty
- Dim Chars As Char() = Nothing
- Dim Output As New StringBuilder
- Dim Rand As New Random
- Try
- If Letters Then
- If UpperCase Then Charset = String.Concat(Charset, LetterChars)
- If LowerCase Then Charset = String.Concat(Charset, LetterChars.ToLower())
- End If
- If Numbers Then Charset = String.Concat(Charset, NumberChars)
- If Symbols Then Charset = String.Concat(Charset, SymbolChars)
- If CustomCharacters IsNot Nothing Then Charset = String.Concat(Charset, CustomCharacters)
- If StringLength > 0 And Charset.Length() > 0 Then
- Chars = Charset.ToCharArray()
- For i = 0 To StringLength - 1
- Output.Append(Chars(Rand.Next(0, Chars.Length)))
- Next
- End If
- Return Output.ToString
- Catch ex As Exception
- Return String.Concat("An error occurred: ", ex.Message)
- End Try
- End Function
- ''' <summary>
- ''' Uses a hexidecimal string to convert to a readable string.
- ''' </summary>
- ''' <param name="Input">The hex string.</param>
- ''' <returns>A readable string.</returns>
- ''' <remarks></remarks>
- Public Function HexToString(ByVal Input As String) As String
- Dim Output As New StringBuilder
- Try
- For i = 0 To Input.Length - 2 Step 2
- Output.Append(Convert.ToChar(Convert.ToByte(Input.Substring(i, 2), 16)))
- Next
- Return Output.ToString()
- Catch ex As Exception
- Return String.Concat("An error occurred: ", ex.Message)
- End Try
- End Function
- ''' <summary>
- ''' Processes a string and converts it to binary.
- ''' </summary>
- ''' <param name="Text">The original string.</param>
- ''' <param name="Separator">JHow characters are separated. Default is a space.</param>
- ''' <returns>A binary string.</returns>
- ''' <remarks></remarks>
- Public Function StringToBinary(ByVal Text As String, Optional ByVal Separator As String = " ") As String
- Dim Output As New StringBuilder
- Try
- For Each Chr As Byte In ASCIIEncoding.ASCII.GetBytes(Text)
- Output.Append(String.Concat(Convert.ToString(Chr, 2).PadLeft(8, "0"c), Separator))
- Next
- Return Output.ToString().Substring(0, Output.Length - 1)
- Catch ex As Exception
- Return String.Concat("An error occurred: ", ex.Message)
- End Try
- End Function
- ''' <summary>
- ''' Processes a binary string and makes it readable.
- ''' </summary>
- ''' <param name="Binary">Binary string input.</param>
- ''' <returns>A readable string.</returns>
- ''' <remarks>The regular expression pattern isn't mine.</remarks>
- Public Function BinaryToString(ByVal Binary As String) As String
- Try
- Dim Characters As String = Regex.Replace(Binary, "[^01]", "")
- Dim OutputBytes((Characters.Length \ 2) - 1) As Byte
- For i = 0 To OutputBytes.Length - 1
- OutputBytes(i) = Convert.ToByte(Characters.Substring(i * 8, 8), 2)
- Next
- Return ASCIIEncoding.ASCII.GetString(OutputBytes)
- Catch ex As Exception
- Return String.Concat("An error occurred: ", ex.Message)
- End Try
- End Function
- ''' <summary>
- ''' Sends POST data to a specified website.
- ''' </summary>
- ''' <param name="PostData">The data the user wants to POST.</param>
- ''' <param name="Site">The site to POST to.</param>
- ''' <returns>The data from the website after POSTing.</returns>
- ''' <remarks></remarks>
- Public Function SendPOSTData(ByVal PostData As String, ByVal Site As String) As String
- Dim RequestClient As New WebClient
- Try
- With RequestClient
- .Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")
- .Headers.Add("ContentType", "application/x-www-form-urlencoded")
- Return .UploadString(Site, PostData)
- End With
- Catch ex As Exception
- Return String.Concat("An error occurred: ", ex.Message)
- Finally
- If RequestClient IsNot Nothing Then
- RequestClient.Dispose()
- End If
- End Try
- End Function
- ''' <summary>
- ''' Gets the current date and time.
- ''' </summary>
- ''' <returns>Current date and time in format yyyy-mm-dd hh:mm:ss.</returns>
- ''' <remarks></remarks>
- Public Function GetDateAndTime() As String
- Try
- Dim Output As String = Format(Date.Now, "[yyyy-MM-dd HH:mm:ss]")
- Return Output
- Catch ex As Exception
- Return String.Concat("An error occurred: ", ex.Message)
- End Try
- End Function
- ''' <summary>
- ''' Converts a string to it's Base64 equivalent.
- ''' </summary>
- ''' <param name="Input">A normal string.</param>
- ''' <returns>A Base64 string.</returns>
- ''' <remarks></remarks>
- Public Function StringToBase64(ByVal Input As String) As String
- Try
- Return Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(Input))
- Catch ex As Exception
- Return String.Concat("An error occurred: ", ex.Message)
- End Try
- End Function
- ''' <summary>
- ''' Converts a Base64 string to it's normal equivalent.
- ''' </summary>
- ''' <param name="Input">A Base64 string.</param>
- ''' <returns>A normal string.</returns>
- ''' <remarks></remarks>
- Public Function Base64ToString(ByRef Input As String) As String
- Try
- Return UTF8Encoding.UTF8.GetString(Convert.FromBase64String(Input))
- Catch ex As Exception
- Return String.Concat("An error occurred: ", ex.Message)
- End Try
- End Function
- ''' <summary>
- ''' Gets the IP of a specified hostname.
- ''' </summary>
- ''' <param name="Host">Website hostname.</param>
- ''' <returns>IP Address.</returns>
- ''' <remarks></remarks>
- Public Function HostToIP(ByVal Host As String) As String
- Try
- Return Dns.GetHostAddresses(Host)(0).ToString()
- Catch ex As Exception
- Return String.Concat("An error occurred: ", ex.Message)
- End Try
- End Function
- ''' <summary>
- ''' Checks for updates to a program via a Pastebin link.
- ''' </summary>
- ''' <param name="PasteID">The ID of the paste.</param>
- ''' <param name="Version">The current version of the program.</param>
- ''' <param name="SplitChar">How the version is split from the download link in the paste.</param>
- ''' <remarks></remarks>
- Public Sub PastebinUpdate(ByVal PasteID As String, ByVal Version As String, Optional ByVal SplitChar As Char = "-"c)
- Dim RequestClient As New WebClient
- Try
- Dim PasteData As String = RequestClient.DownloadString(String.Concat("http://pastebin.com/raw.php?i=", PasteID))
- If PasteData.Split(SplitChar)(0) IsNot Version Then
- Dim UserInputResponse As DialogResult = MessageBox.Show("There is an update available! Would you like to download it?", "Update Available", MessageBoxButtons.YesNo)
- If UserInputResponse = DialogResult.Yes Then
- Process.Start(PasteData.Split(SplitChar)(1))
- End If
- End If
- Catch ex As Exception
- MessageBox.Show(String.Concat("An error occurred: ", ex.Message), "Error!", MessageBoxButtons.OK)
- Finally
- If RequestClient IsNot Nothing Then
- RequestClient.Dispose()
- End If
- End Try
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment