Changes between Version 35 and Version 36 of HashSourceCodes


Ignore:
Timestamp:
Jul 25, 2011, 5:58:44 PM (13 years ago)
Author:
guest
Comment:

Previous change worked for the test files but didn't work in a wider range of files.

Legend:

Unmodified
Added
Removed
Modified
  • HashSourceCodes

    v35 v36  
    318318Imports System.Text
    319319Imports System.IO
    320 Imports System.Runtime.InteropServices
     320'Note: you must remove integer overflow checking.
    321321
    322322Namespace MovieHasher
    323     Class Program
    324         Private Shared Function ComputeMovieHash(ByVal filename As String) As Byte()
    325             Dim result As Byte()
    326             Using input As Stream = File.OpenRead(filename)
    327                 result = ComputeMovieHash(input)
    328             End Using
    329             Return result
    330         End Function
    331 
    332         Private Shared Function ComputeMovieHash(ByVal input As Stream) As Byte()
    333             Dim lhash As System.Decimal, streamsize As Long
    334             streamsize = input.Length
    335             lhash = streamsize
    336 
    337             Dim i As Long = 0
    338             Dim buffer As Byte() = New Byte(Marshal.SizeOf(GetType(Long)) - 1) {}
    339             While i < 65536 / Marshal.SizeOf(GetType(Long)) AndAlso (input.Read(buffer, 0, Marshal.SizeOf(GetType(Long))) > 0)
    340                 i += 1
    341                 lhash += BitConverter.ToInt64(buffer, 0)
    342             End While
    343 
    344             input.Position = Math.Max(0, streamsize - 65536)
    345             i = 0
    346             While i < 65536 / Marshal.SizeOf(GetType(Long)) AndAlso (input.Read(buffer, 0, Marshal.SizeOf(GetType(Long))) > 0)
    347                 i += 1
    348                 lhash += BitConverter.ToInt64(buffer, 0)
    349             End While
    350             input.Close()
    351 
    352             Dim lhashArray() As Integer = Decimal.GetBits(lhash)
    353 
    354             Dim result(7) As Byte
    355 
    356             Array.Copy(BitConverter.GetBytes(lhashArray(0)), 0, result, 0, 4)
    357             Array.Copy(BitConverter.GetBytes(lhashArray(1)), 0, result, 4, 4)
    358 
    359             Array.Reverse(result)
    360             Return result
    361         End Function
    362 
    363         Private Shared Function ToHexadecimal(ByVal bytes As Byte()) As String
    364             Dim hexBuilder As New StringBuilder()
    365             For i As Integer = 0 To bytes.Length - 1
    366                 hexBuilder.Append(bytes(i).ToString("x2"))
    367             Next
    368             Return hexBuilder.ToString()
    369         End Function
    370 
    371         Public Sub New(ByVal Path As String)
    372             Dim moviehash As Byte() = ComputeMovieHash(Path)
    373             Console.WriteLine("The hash of the movie-file is: {0}", ToHexadecimal(moviehash))
    374         End Sub
    375     End Class
     323        Class Program
     324                Private Shared Function ComputeMovieHash(ByVal filename As String) As Byte()
     325                        Dim result As Byte()
     326                        Using input As Stream = File.OpenRead(filename)
     327                                result = ComputeMovieHash(input)
     328                        End Using
     329                        Return result
     330                End Function
     331               
     332                Private Function ComputeMovieHash(ByVal input As Stream) As Byte()
     333                        Dim lhash As System.Int64, streamsize As Long
     334                        streamsize = input.Length
     335                        lhash = streamsize
     336                       
     337                        Dim i As Long = 0
     338                        Dim buffer As Byte() = New Byte(Marshal.SizeOf(GetType(Long)) - 1) {}
     339                        While i < 65536 / Marshal.SizeOf(GetType(Long)) AndAlso (input.Read(buffer, 0, Marshal.SizeOf(GetType(Long))) > 0)
     340                                i += 1
     341                               
     342                                lhash += BitConverter.ToInt64(buffer, 0)
     343                        End While
     344                       
     345                        input.Position = Math.Max(0, streamsize - 65536)
     346                        i = 0
     347                        While i < 65536 / Marshal.SizeOf(GetType(Long)) AndAlso (input.Read(buffer, 0, Marshal.SizeOf(GetType(Long))) > 0)
     348                                i += 1
     349                                lhash += BitConverter.ToInt64(buffer, 0)
     350                        End While
     351                        input.Close()
     352                        Dim result As Byte() = BitConverter.GetBytes(lhash)
     353                        Array.Reverse(result)
     354                        Return result
     355                End Function
     356               
     357                Private Shared Function ToHexadecimal(ByVal bytes As Byte()) As String
     358                        Dim hexBuilder As New StringBuilder()
     359                        For i As Integer = 0 To bytes.Length - 1
     360                                hexBuilder.Append(bytes(i).ToString("x2"))
     361                        Next
     362                        Return hexBuilder.ToString()
     363                End Function
     364               
     365                Private Shared Sub Main(ByVal args As String())
     366                        Dim moviehash As Byte() = ComputeMovieHash("C:\test.avi")
     367                        Console.WriteLine("The hash of the movie-file is: {0}", ToHexadecimal(moviehash))
     368                End Sub
     369        End Class
    376370End Namespace
    377371}}}