Jun
12
Posted on 12-06-2008
Filed Under (Visual Basic Code) by Sidetalker

Unix time (aka. POSIX time) is the number of seconds elasped since midnight UTC January 1, 1970 (not counting leap seconds) and is widely used in not only Unix-like operating systems but in many other computer systems as well. I found myself needing to find the UNIX timestamp very often (every time I packaged something for Installer.app) and I was using obnoxious PHP scripts I had found online. I wanted to make myself a small application that would determine the current UNIX timestamp from my current system time.

There are tons of ways to do this but I believe I found the most efficient… It consists of only one line of code:

DateTime.UtcNow.Subtract(#1/1/1970#).TotalSeconds

If that beautiful little line of code is thrown into a function like so:

Function UnixTimeStamp() As Integer
Return DateTime.UtcNow.Subtract(#1/1/1970#).TotalSeconds
End Function

It provides a very easy way to call UnixTimeStamp() from anywhere in your program to receive the current UNIX timestamp from your system time.

It’s worth mentioning that this little function does not take timezones into account so you will have to add code to do so yourself. For example, if you are in Eastern Standard Time (UTC-5), you could modify the function like so:

Function UnixTimeStamp() As Integer
Return DateTime.UtcNow.Subtract(#1/1/1970#).TotalSeconds - 18000
End Function

You don’t really need an example application for this snippet of code but, nonetheless, I’ve included the source (in both vb.net and .txt forms) and a standalone .exe for a simple application that, when launched, displays the current UNIX timestamp and a refresh button to update the timestamp.

Download VB.NET 2008 Source Code

View VB.NET 2008 Source Code in .txt format

Download the standalone .exe

(1) Comment    Read More   
Jun
11
Posted on 11-06-2008
Filed Under (Visual Basic Code) by Sidetalker

I’ve decided to start blogging about some code that took me a while to find or figure out… Hopefully other people looking for this code won’t have to suffer what I did :).

MD5 Checksums are used frequently to verify both the validity of files and to check for corruption after large downloads or uploads. There are several free applications for Windows that can calculate the MD5 Checksum of a file though I wanted to be able to make myself an application customized for my own use.

You will need to import a few .NET Classes to get started:

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

IO is used to manage loading the files in and out of your application. Cryptography will retrieve the raw MD5 Hash and Text will append it. Of course, you don’t need to import any of these but it will clean up your code and is a good programming practice.

You now need to select the file you wish to run the MD5 algorithm on. If you are building an application that will always be checking the MD5 of the same file in the same place, this is very simple. However, this is usually not the case.

Dim o As OpenFileDialog = New OpenFileDialog
Dim temp As String
Dim path As String

o.Filter = “.zip Files|*.zip|All Files|*.*”
If (o.ShowDialog() = DialogResult.OK) Then
path = o.FileName

This snippet of code creates an OpenFileDialog and declares temp and path (temp will be used later on). The OpenFileDialog is then showed and if the user successfully selects a file it is set to path. The rest of the coding will be done in this If/Then block to avoid attempting to run the MD5 algorithm on an incorrect or nonexistent file.

The o.Filter can be used if you want only certain types of files to be visible in the file browser. The way o.Filter is set in my example, .zip Files and All Files will be selectable from the drop down list. It is quite simple to customize the o.Filter to your desire or simply omit it altogether.

We will now get the raw MD5 checksum of our file with the following code:

Dim fs As FileStream

fs = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)

Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider

md5.ComputeHash(fs)

fs.Close()

Here we create a new FileStream for our previously selected file and use the built in MD5CryptoServiceProvider to compute the raw hash of our file. We end by closing the FileStream so the file can be accessed by other programs and the user.

We will now use the StringBuilder class to append our string and format it correctly:

Dim hash As Byte() = md5.Hash
Dim buff As StringBuilder = New StringBuilder
Dim hashByte As Byte
For Each hashByte In hash
buff.Append(String.Format(”{0:X2}”, hashByte))
Next

temp = buff.ToString()

We now have our MD5 Checksum stored to temp! By default it is in all caps… This may be suitable in your situation but i need lower case. This is as simple as:

return temp.ToLower

And there you have it! For those of you that don’t like to read and understand things for yourself here is a fully working function.

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Function MD5Checksum(ByVal path As String) As String
Dim temp As String

Dim fs As FileStream

fs = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
md5.ComputeHash(fs)
fs.Close()

Dim hash As Byte() = md5.Hash
Dim buff As StringBuilder = New StringBuilder
Dim hashByte As Byte
For Each hashByte In hash
buff.Append(String.Format(”{0:X2}”, hashByte))
Next

temp = buff.ToString()

Return temp
End Function

You will need to call this function in one of two ways:

MD5Checksum(”C:\MyFile.ext”)

In that case, you know exactly where the file you want is… if you want to browse for the file, you can use this:

Dim o As OpenFileDialog = New OpenFileDialog
Dim path As String

If (o.ShowDialog() = DialogResult.OK) Then
MD5Checksum(o.FileName)

Feel free to download the source code to the MD5 Demo App I’ve created below! Note: I use VB.NET 2008 so you will not be able to view the source on anything below 2008. I’ve added a download link for a .txt document of the source code so you can view it if you have VB.NET 2005 or lower. And of course, you can download the standalone .exe and just use the program!

Download VB.NET 2008 Source Code

View VB.NET 2008 Source Code in .txt format

Download the standalone .exe

(1) Comment    Read More