Guest User

RainbowUtility Re-Done

a guest
Sep 24th, 2014
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.55 KB | None | 0 0
  1. '' ************************************************* ''
  2. '' * Original utility released by RainbowTech @ SL * ''
  3. '' * Re-coded and cleaned up by .Shebang @ SL      * ''
  4. '' ************************************************* ''
  5.  
  6. Option Strict On
  7.  
  8. Imports System.Net
  9. Imports System.IO
  10. Imports System.Text
  11. Imports System.Text.RegularExpressions
  12.  
  13. Module Tools
  14.  
  15.     Private LetterChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  16.     Private NumberChars As String = "1234567890"
  17.     Private SymbolChars As String = "`~!@#$%^&*()_-+={[}]|\:;'""<,>.?/"
  18.  
  19.     ''' <summary>
  20.     ''' Creates a random string based on the length of the string, and
  21.     ''' the character set defined by the programmer.
  22.     ''' </summary>
  23.     ''' <param name="StringLength">The length of the random string.</param>
  24.     ''' <param name="CustomCharacters">Any custom characters not included in the default character set.</param>
  25.     ''' <param name="Letters">True if the programmer wants to include letters.</param>
  26.     ''' <param name="Numbers">True if the programmer wants to include numbers.</param>
  27.     ''' <param name="Symbols">True if the programmer wants to include symbols.</param>
  28.     ''' <param name="UpperCase">Depends on Letters' value. True if the programmer wants uppercase letters.</param>
  29.     ''' <param name="LowerCase">Depends on Letters' value. True if the programmer wants lowercase letters.</param>
  30.     ''' <returns>Random string of length StringLength.</returns>
  31.     ''' <remarks></remarks>
  32.     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
  33.         Dim Charset As String = String.Empty
  34.         Dim Chars As Char() = Nothing
  35.         Dim Output As New StringBuilder
  36.         Dim Rand As New Random
  37.         Try
  38.             If Letters Then
  39.                 If UpperCase Then Charset = String.Concat(Charset, LetterChars)
  40.                 If LowerCase Then Charset = String.Concat(Charset, LetterChars.ToLower())
  41.             End If
  42.             If Numbers Then Charset = String.Concat(Charset, NumberChars)
  43.             If Symbols Then Charset = String.Concat(Charset, SymbolChars)
  44.             If CustomCharacters IsNot Nothing Then Charset = String.Concat(Charset, CustomCharacters)
  45.             If StringLength > 0 And Charset.Length() > 0 Then
  46.                 Chars = Charset.ToCharArray()
  47.                 For i = 0 To StringLength - 1
  48.                     Output.Append(Chars(Rand.Next(0, Chars.Length)))
  49.                 Next
  50.             End If
  51.             Return Output.ToString
  52.         Catch ex As Exception
  53.             Return String.Concat("An error occurred: ", ex.Message)
  54.         End Try
  55.     End Function
  56.  
  57.     ''' <summary>
  58.     ''' Uses a hexidecimal string to convert to a readable string.
  59.     ''' </summary>
  60.     ''' <param name="Input">The hex string.</param>
  61.     ''' <returns>A readable string.</returns>
  62.     ''' <remarks></remarks>
  63.     Public Function HexToString(ByVal Input As String) As String
  64.         Dim Output As New StringBuilder
  65.         Try
  66.             For i = 0 To Input.Length - 2 Step 2
  67.                 Output.Append(Convert.ToChar(Convert.ToByte(Input.Substring(i, 2), 16)))
  68.             Next
  69.             Return Output.ToString()
  70.         Catch ex As Exception
  71.             Return String.Concat("An error occurred: ", ex.Message)
  72.         End Try
  73.     End Function
  74.  
  75.     ''' <summary>
  76.     ''' Processes a string and converts it to binary.
  77.     ''' </summary>
  78.     ''' <param name="Text">The original string.</param>
  79.     ''' <param name="Separator">JHow characters are separated. Default is a space.</param>
  80.     ''' <returns>A binary string.</returns>
  81.     ''' <remarks></remarks>
  82.     Public Function StringToBinary(ByVal Text As String, Optional ByVal Separator As String = " ") As String
  83.         Dim Output As New StringBuilder
  84.         Try
  85.             For Each Chr As Byte In ASCIIEncoding.ASCII.GetBytes(Text)
  86.                 Output.Append(String.Concat(Convert.ToString(Chr, 2).PadLeft(8, "0"c), Separator))
  87.             Next
  88.             Return Output.ToString().Substring(0, Output.Length - 1)
  89.         Catch ex As Exception
  90.             Return String.Concat("An error occurred: ", ex.Message)
  91.         End Try
  92.     End Function
  93.  
  94.     ''' <summary>
  95.     ''' Processes a binary string and makes it readable.
  96.     ''' </summary>
  97.     ''' <param name="Binary">Binary string input.</param>
  98.     ''' <returns>A readable string.</returns>
  99.     ''' <remarks>The regular expression pattern isn't mine.</remarks>
  100.     Public Function BinaryToString(ByVal Binary As String) As String
  101.         Try
  102.             Dim Characters As String = Regex.Replace(Binary, "[^01]", "")
  103.             Dim OutputBytes((Characters.Length \ 2) - 1) As Byte
  104.             For i = 0 To OutputBytes.Length - 1
  105.                 OutputBytes(i) = Convert.ToByte(Characters.Substring(i * 8, 8), 2)
  106.             Next
  107.             Return ASCIIEncoding.ASCII.GetString(OutputBytes)
  108.         Catch ex As Exception
  109.             Return String.Concat("An error occurred: ", ex.Message)
  110.         End Try
  111.     End Function
  112.  
  113.     ''' <summary>
  114.     ''' Sends POST data to a specified website.
  115.     ''' </summary>
  116.     ''' <param name="PostData">The data the user wants to POST.</param>
  117.     ''' <param name="Site">The site to POST to.</param>
  118.     ''' <returns>The data from the website after POSTing.</returns>
  119.     ''' <remarks></remarks>
  120.     Public Function SendPOSTData(ByVal PostData As String, ByVal Site As String) As String
  121.         Dim RequestClient As New WebClient
  122.         Try
  123.             With RequestClient
  124.                 .Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")
  125.                 .Headers.Add("ContentType", "application/x-www-form-urlencoded")
  126.                 Return .UploadString(Site, PostData)
  127.             End With
  128.         Catch ex As Exception
  129.             Return String.Concat("An error occurred: ", ex.Message)
  130.         Finally
  131.             If RequestClient IsNot Nothing Then
  132.                 RequestClient.Dispose()
  133.             End If
  134.         End Try
  135.     End Function
  136.  
  137.     ''' <summary>
  138.     ''' Gets the current date and time.
  139.     ''' </summary>
  140.     ''' <returns>Current date and time in format yyyy-mm-dd hh:mm:ss.</returns>
  141.     ''' <remarks></remarks>
  142.     Public Function GetDateAndTime() As String
  143.         Try
  144.             Dim Output As String = Format(Date.Now, "[yyyy-MM-dd HH:mm:ss]")
  145.             Return Output
  146.         Catch ex As Exception
  147.             Return String.Concat("An error occurred: ", ex.Message)
  148.         End Try
  149.     End Function
  150.  
  151.     ''' <summary>
  152.     ''' Converts a string to it's Base64 equivalent.
  153.     ''' </summary>
  154.     ''' <param name="Input">A normal string.</param>
  155.     ''' <returns>A Base64 string.</returns>
  156.     ''' <remarks></remarks>
  157.     Public Function StringToBase64(ByVal Input As String) As String
  158.         Try
  159.             Return Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(Input))
  160.         Catch ex As Exception
  161.             Return String.Concat("An error occurred: ", ex.Message)
  162.         End Try
  163.     End Function
  164.  
  165.     ''' <summary>
  166.     ''' Converts a Base64 string to it's normal equivalent.
  167.     ''' </summary>
  168.     ''' <param name="Input">A Base64 string.</param>
  169.     ''' <returns>A normal string.</returns>
  170.     ''' <remarks></remarks>
  171.     Public Function Base64ToString(ByRef Input As String) As String
  172.         Try
  173.             Return UTF8Encoding.UTF8.GetString(Convert.FromBase64String(Input))
  174.         Catch ex As Exception
  175.             Return String.Concat("An error occurred: ", ex.Message)
  176.         End Try
  177.     End Function
  178.     ''' <summary>
  179.     ''' Gets the IP of a specified hostname.
  180.     ''' </summary>
  181.     ''' <param name="Host">Website hostname.</param>
  182.     ''' <returns>IP Address.</returns>
  183.     ''' <remarks></remarks>
  184.     Public Function HostToIP(ByVal Host As String) As String
  185.         Try
  186.             Return Dns.GetHostAddresses(Host)(0).ToString()
  187.         Catch ex As Exception
  188.             Return String.Concat("An error occurred: ", ex.Message)
  189.         End Try
  190.     End Function
  191.  
  192.     ''' <summary>
  193.     ''' Checks for updates to a program via a Pastebin link.
  194.     ''' </summary>
  195.     ''' <param name="PasteID">The ID of the paste.</param>
  196.     ''' <param name="Version">The current version of the program.</param>
  197.     ''' <param name="SplitChar">How the version is split from the download link in the paste.</param>
  198.     ''' <remarks></remarks>
  199.     Public Sub PastebinUpdate(ByVal PasteID As String, ByVal Version As String, Optional ByVal SplitChar As Char = "-"c)
  200.         Dim RequestClient As New WebClient
  201.         Try
  202.             Dim PasteData As String = RequestClient.DownloadString(String.Concat("http://pastebin.com/raw.php?i=", PasteID))
  203.             If PasteData.Split(SplitChar)(0) IsNot Version Then
  204.                 Dim UserInputResponse As DialogResult = MessageBox.Show("There is an update available! Would you like to download it?", "Update Available", MessageBoxButtons.YesNo)
  205.                 If UserInputResponse = DialogResult.Yes Then
  206.                     Process.Start(PasteData.Split(SplitChar)(1))
  207.                 End If
  208.             End If
  209.         Catch ex As Exception
  210.             MessageBox.Show(String.Concat("An error occurred: ", ex.Message), "Error!", MessageBoxButtons.OK)
  211.         Finally
  212.             If RequestClient IsNot Nothing Then
  213.                 RequestClient.Dispose()
  214.             End If
  215.         End Try
  216.     End Sub
  217.  
  218. End Module
Advertisement
Add Comment
Please, Sign In to add comment