Sunday 28 July 2013

QTP vs SilkTest WorkBench

Hi all,

It's been a while, I thought of talking about automation tools instead on this post. This is because of the last few months, I've been working with the Silk Tools (particularly the WorkBench .NET variant IDE) and I must say that I like what I've been using. The libraries provided by Silk are quite good for your typical automation and when you run into custom applications, the .NET IDE (which is based on the MS Visual Studio IDE) allows you to code purely in VB.NET. There are some limitations using WorkBench but they can completely removed when you switch completely to MS Visual Studio and import the Silk libraries. From an automation perspective, I'd say this is actually more powerful than QTP but for the majority of client engagements, QTP out of the box, can handle the engagement a lot more easily because it is catered to almost every situation. However, when it comes to the bang for your buck, I'd actually go with Silk/Borland! The only real problem comes down to the fact that you have very little online presence for the Silk products compared to HP's QTP. So finding solutions to problems requires more self help and reading of more technical articles than compared with finding solutions for QTP, as it has already been done and the code is most likely available online somewhere.

So take what you want from this and if you have any questions, feel free to comment and I'll get back to you when I look at this next.

Just in keep in mind, I'm talking in the broadest sense and what I've said may not apply to your current situation. If you want advise I can also provide that.

Cheers,
Pat

Tuesday 5 March 2013

Simple Cryptography library in VB.NET (SilkTest)


'-------------------------------------------------------------------------------------------------------
'                                        Crypto Library
'Library Name:   Crypto
'Authors:        Patrick  Tsang
'Dates:          18-Feb-2013 - Present
'Purpose:             Cryptography library
'Parameters:     N/A
'Pre and Post Conditions: N/A
'Script Version: N/A
'--------------------------------------------------------------------------------------------------------
'Additional Requirements:
'--------------------------------------------------------------------------------------------------------
'Functions List:
'Public Function HashValue
'Public Sub PrintByteArray
'Public Function ByteArrayToString
'Public Function ByteArrayToBase64String
'Public Function Base64StrToString
'Public Function Base64StrToByteArray
'Public Function StreamToString
'Public Function StringToStream
'Public Function EncryptStringToBytes_Aes
'Public Function DecryptStringFromBytes_Aes
'Public Function GetAESKeyBase64Str
'Public Function GetAESIVBase64Str
'Public Sub OutputNewAESKeyIV
'Public Function GetDecryptedValue
'Public Function GetEncryptedValue
'--------------------------------------------------------------------------------------------------------
'Amendments:
'
'--------------------------------------------------------------------------------------------------------
Imports System
Imports System.IO
Imports System.Security.Cryptography

Public Module Crypto

       'PT 18/02/2013 - Hash a string using SHA256
    Public Function HashValue(ByVal str) As Byte()

    ' Initialize a SHA256 hash object.
    Dim mySHA256 As SHA256 = SHA256Managed.Create()
    Dim hashedValue() As Byte
      
       'Convert string to stream
       Using strStream As Stream = StringToStream(str)
              hashedValue = mySHA256.ComputeHash(strStream)
              'PrintByteArray(hashValue)
              HashValue = hashedValue
       End Using
                    
    End Function

    ' Print the byte array in a readable format.
    Public Sub PrintByteArray(ByVal array() As Byte)
        Dim i As Integer
        For i = 0 To array.Length - 1
            Console.Write(String.Format("{0:X2}", array(i)))
            If i Mod 4 = 3 Then
                Console.Write(" ")
            End If
        Next i
        Console.WriteLine()

    End Sub 'PrintByteArray
      
       'PT 18/02/2013 - Convert byte array to string
       Public Function ByteArrayToString (ByVal array() As Byte) As String
        Dim i As Integer
              Dim str As String = ""
        For i = 0 To array.Length - 1
            str = str & String.Format("{0:X2}", array(i))
            If i Mod 4 = 3 Then
                str = str & " "
            End If
        Next i
        ByteArrayToString = str
       End Function
      
       'PT 18/02/2013 - Convert string to base 64 String
       Public Function ByteArrayToBase64String (ByVal array As Byte()) As String
              Return Convert.ToBase64String(array)
       End Function
      
       'PT 18/02/2013 - Convert Base64 string to String
       Public Function Base64StrToString (ByVal str As String) As String
              Dim b As Byte() = Convert.FromBase64String(str)
              Return System.Text.Encoding.UTF8.GetString(b)
       End Function
      
       'PT 18/02/2013 - Convert String to byte array
       Public Function Base64StrToByteArray (ByVal base64 As String) As Byte()
              Return Convert.FromBase64String(base64)
       End Function
      
       'PT 18/02/2013 - Converts a stream to a string
       'Reference: http://www.codekeep.net/snippets/5d986fb1-077a-4126-8cd1-e07b9e5a47cb.aspx
        Public Function StreamToString(ByVal s As IO.Stream) As String
           If s Is Nothing Then Return ""
           If s.CanRead = False Then Return ""
           Dim sw As New IO.StreamReader(s)
              Return sw.ReadToEnd()
              'End Using
         End Function

       'PT 18/02/2013 - Converts a String to a stream
       'Reference: http://www.codekeep.net/snippets/5d986fb1-077a-4126-8cd1-e07b9e5a47cb.aspx  
         Public Function StringToStream(ByVal strToStream As String) As IO.Stream 'layoutString
           If String.IsNullOrEmpty(strToStream) Then Return Nothing
           Dim sw As IO.StreamWriter = New IO.StreamWriter(New IO.MemoryStream)
                  sw.Write(strToStream)
                  sw.Flush()
                  Return sw.BaseStream 'caller must close this stream when done
              'End Using   
         End Function      

       'PT 18/02/2013 - Encrypt strings using Advanced Encryption Standard (AES) algorithm
       'Referenced from MSDN
    Public Function EncryptStringToBytes_Aes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
        ' Check arguments.
        If plainText Is Nothing OrElse plainText.Length <= 0 Then
            Throw New ArgumentNullException("plainText")
        End If
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        Dim encrypted() As Byte
        ' Create an Aes object
        ' with the specified key and IV.
        Using aesAlg As Aes = Aes.Create()

            aesAlg.Key = Key
            aesAlg.IV = IV

            ' Create a decrytor to perform the stream transform.
            Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
            ' Create the streams used for encryption.
            Using msEncrypt As New MemoryStream()
                Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                    Using swEncrypt As New StreamWriter(csEncrypt)

                        'Write all data to the stream.
                        swEncrypt.Write(plainText)
                    End Using
                    encrypted = msEncrypt.ToArray()
                End Using
            End Using
        End Using

        ' Return the encrypted bytes from the memory stream.
        Return encrypted

    End Function 'EncryptStringToBytes_Aes

       'PT 18/02/2013 - Decrypt strings using Advanced Encryption Standard (AES) algorithm
       'Referenced from MSDN     
    Public Function DecryptStringFromBytes_Aes(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
        ' Check arguments.
        If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
            Throw New ArgumentNullException("cipherText")
        End If
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        ' Declare the string used to hold
        ' the decrypted text.
        Dim plaintext As String = Nothing

        ' Create an Aes object
        ' with the specified key and IV.
        Using aesAlg As Aes = Aes.Create()
            aesAlg.Key = Key
            aesAlg.IV = IV

            ' Create a decrytor to perform the stream transform.
            Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)

            ' Create the streams used for decryption.
            Using msDecrypt As New MemoryStream(cipherText)

                Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

                    Using srDecrypt As New StreamReader(csDecrypt)


                        ' Read the decrypted bytes from the decrypting stream
                        ' and place them in a string.
                        plaintext = srDecrypt.ReadToEnd()
                    End Using
                End Using
            End Using
        End Using

        Return plaintext

    End Function 'DecryptStringFromBytes_Aes          
 
       'PT 18/02/2013 - Generate new AES Key that is converted to Base64 String
       Public Function GetAESKeyBase64Str () As String
              Using myAES As Aes = Aes.Create()
                     Return ByteArrayToBase64String(myAES.Key)
              End Using
       End Function
      
       'PT 18/02/2013 - Generate new AES IV that is converted to Base64 String
       Public Function GetAESIVBase64Str () As String
              Using myAES As Aes = Aes.Create()
                     Return ByteArrayToBase64String(myAES.IV)
              End Using
       End Function
      
       'PT 18/02/2013 - Get a new key and IV value outputted in the console
       'NOTE: This is used so that you can update the Config value with newer values.
       'Usage: just copy and paste the values into the config module
       Public Sub OutputNewAESKeyIV ()
              Console.WriteLine("New Key: {0}", Crypto.GetAESKeyBase64Str)
              Console.WriteLine("New IV: {0}", Crypto.GetAESIVBase64Str)          
       End Sub
      
       'PT 18/02/2013 - Decrypt value (base64 encrypted string)
       Public Function GetDecryptedValue (ByVal encryptedStr As String) As String
             
              'Convert base 64 string (key) back to byte array
              Dim key As Byte() = Crypto.Base64StrToByteArray(Config.CRYPTOKEY)         
             
              'Convert base 64 string (IV) back to byte array
              Dim IV As Byte() = Crypto.Base64StrToByteArray(Config.CRYPTOIV)
             
              'Convert base 64 string (encrypted string) back to byte array
              Dim encryptByteArray As Byte() = Crypto.Base64StrToByteArray(encryptedStr)
             
              'Decrypt string
              Return Crypto.DecryptStringFromBytes_Aes(encryptByteArray, key, IV) 'encrypted          
       End Function
      
       'PT 19/02/2013 - Get the Encrypted value (base64 encrypted string)
       Public Function GetEncryptedValue (ByVal str As String) As String
              'Convert the key to a byte array from base64 string
              Dim newCryptKey As Byte() = Crypto.Base64StrToByteArray(Config.CRYPTOKEY)
             
              'Convert the IV to a byte array from base64 string
              Dim newCryptIV As Byte() = Crypto.Base64StrToByteArray(Config.CRYPTOIV)
             
              Dim newByteArray As Byte() = _
              Crypto.EncryptStringToBytes_Aes(str, newCryptKey, newCryptIV)
              Return Crypto.ByteArrayToBase64String(newByteArray)
       End Function
End Module