| 1049 | == Powershell 2.0 == |
| 1050 | You can use GetHash.dll.[[BR]] |
| 1051 | |
| 1052 | [http://trac.opensubtitles.org/projects/opensubtitles/attachment/wiki/HashSourceCodes/GetHash.dll][[BR]] |
| 1053 | |
| 1054 | Use Example:[[BR]] |
| 1055 | |
| 1056 | {{{ |
| 1057 | Add-Type -Path "GetHash.dll" |
| 1058 | |
| 1059 | function MovieHash([string]$path) { |
| 1060 | $hash = [GetHash.Main] |
| 1061 | $hash::ToHexadecimal($hash::ComputeHash($path)) |
| 1062 | } |
| 1063 | |
| 1064 | MovieHash $filename |
| 1065 | }}} |
| 1066 | or without using GetHash.dll: |
| 1067 | {{{ |
| 1068 | $dataLength = 65536 |
| 1069 | |
| 1070 | function LongSum([UInt64]$a, [UInt64]$b) { |
| 1071 | [UInt64](([Decimal]$a + $b) % ([Decimal]([UInt64]::MaxValue) + 1)) |
| 1072 | } |
| 1073 | |
| 1074 | function StreamHash([IO.Stream]$stream) { |
| 1075 | $hashLength = 8 |
| 1076 | [UInt64]$lhash = 0 |
| 1077 | [byte[]]$buffer = New-Object byte[] $hashLength |
| 1078 | $i = 0 |
| 1079 | while ( ($i -lt ($dataLength / $hashLength)) -and ($stream.Read($buffer,0,$hashLength) -gt 0) ) { |
| 1080 | $i++ |
| 1081 | $lhash = LongSum $lhash ([BitConverter]::ToUInt64($buffer,0)) |
| 1082 | } |
| 1083 | $lhash |
| 1084 | } |
| 1085 | |
| 1086 | function MovieHash([string]$path) { |
| 1087 | try { |
| 1088 | $stream = [IO.File]::OpenRead($path) |
| 1089 | [UInt64]$lhash = $stream.Length |
| 1090 | $lhash = LongSum $lhash (StreamHash $stream) |
| 1091 | $stream.Position = [Math]::Max(0, $stream.Length - $dataLength) |
| 1092 | $lhash = LongSum $lhash (StreamHash $stream) |
| 1093 | "{0:X}" -f $lhash |
| 1094 | } |
| 1095 | finally { $stream.Close() } |
| 1096 | } |
| 1097 | |
| 1098 | MovieHash $filename |
| 1099 | }}} |