作成 2012.03.20
更新 2014.02.07
PowerShell でハッシュの計算
コード
PowerShell で MD5, SHA1, SHA256, SHA384, SHA512 を計算する。
hash.ps1
param([string] $file="NODATA", [string] $hash="md5")

$provider = New-Object Microsoft.CSharp.CSharpCodeProvider
$params = New-Object System.CodeDom.Compiler.CompilerParameters
$params.GenerateInMemory = $True
$params.TreatWarningsAsErrors = $True
$refs = "System.dll","mscorlib.dll"
$params.ReferencedAssemblies.AddRange($refs)
$txtCode = '
using System;
using System.IO;
using System.Security.Cryptography;

public class MySystem {
  public string md5(string path){
    string result = "";
    MD5 hash = MD5.Create();
    try{
      FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
      byte[] hashValue = hash.ComputeHash(fs);
      fs.Close();
      foreach (int b in hashValue){
        result += b.ToString("x2");
      }
    }catch(Exception e){
      Console.WriteLine(e.StackTrace+":"+path);
    }
    return result;
  }
  public string sha1(string path){
    string result = "";
    SHA1 hash = SHA1.Create();
    try{
      FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
      byte[] hashValue = hash.ComputeHash(fs);
      fs.Close();
      foreach (int b in hashValue){
        result += b.ToString("x2");
      }
    }catch(Exception e){
      Console.WriteLine(e.StackTrace+":"+path);
    }
    return result;
  }
  public string sha256(string path){
    string result = "";
    SHA256 hash = SHA256.Create();
    try{
      FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
      byte[] hashValue = hash.ComputeHash(fs);
      fs.Close();
      foreach (int b in hashValue){
        result += b.ToString("x2");
      }
    }catch(Exception e){
      Console.WriteLine(e.StackTrace+":"+path);
    }
    return result;
  }
  public string sha384(string path){
    string result = "";
    SHA384 hash = SHA384.Create();
    try{
      FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
      byte[] hashValue = hash.ComputeHash(fs);
      fs.Close();
      foreach (int b in hashValue){
        result += b.ToString("x2");
      }
    }catch(Exception e){
      Console.WriteLine(e.StackTrace+":"+path);
    }
    return result;
  }
  public string sha512(string path){
    string result = "";
    SHA512 hash = SHA512.Create();
    try{
      FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
      byte[] hashValue = hash.ComputeHash(fs);
      fs.Close();
      foreach (int b in hashValue){
        result += b.ToString("x2");
      }
    }catch(Exception e){
      Console.WriteLine(e.StackTrace+":"+path);
    }
    return result;
  }
  public string GetHash(string path, string operation){
    string result_hash = "";
    if(string.Compare(operation,"MD5",false) == 0){
      result_hash = md5(path);
      result_hash = result_hash.ToUpper();
    }else if(string.Compare(operation,"SHA1",false) == 0){
      result_hash = sha1(path);
      result_hash = result_hash.ToUpper();
    }else if(string.Compare(operation,"SHA256",false) == 0){
      result_hash = sha256(path);
      result_hash = result_hash.ToUpper();
    }else if(string.Compare(operation,"SHA384",false) == 0){
      result_hash = sha384(path);
      result_hash = result_hash.ToUpper();
    }else if(string.Compare(operation,"SHA512",false) == 0){
      result_hash = sha512(path);
      result_hash = result_hash.ToUpper();
    }else if(string.Compare(operation,"sha1",false) == 0){
      result_hash = sha1(path);
    }else if(string.Compare(operation,"sha256",false) == 0){
      result_hash = sha256(path);
    }else if(string.Compare(operation,"sha384",false) == 0){
      result_hash = sha384(path);
    }else if(string.Compare(operation,"sha512",false) == 0){
      result_hash = sha512(path);
    }else{
      result_hash = md5(path);
    }
    return operation+" ("+Path.GetFileName(path)+") = "+result_hash;
  }
}
'
$results = $provider.CompileAssemblyFromSource($params, $txtCode)
$results.Errors
$mAssembly = $results.CompiledAssembly
$i = $mAssembly.CreateInstance("MySystem")

if(Test-Path $file){
  $item = Get-Item $file
  $attr = $item.Attributes.value__
  # ディレクトリじゃないことを確認する
  if(($attr -band 16) -eq 0){
    # コードの中の実行ディレクトリが異なるためフルパスで指定する必要がある
    [string] $file = $item.FullName
    $i.GetHash($file,$hash)
  }else{
    Write-Error "ファイルを指定してください。"
  }
}else{
  Write-Host ".\hash.ps1 <FilePath> [md5|sha1|sha256|sha384|sha512|MD5|SHA1|SHA256|SHA384|SHA512]"
}
実行例
引数が無いとコマンド例を表示します。
PS> .\hash.ps1
.\hash.ps1 <FilePath> [md5|sha1|sha256|sha384|sha512|MD5|SHA1|SHA256|SHA384|SHA512]
小文字でハッシュを指定すると小文字のハッシュ値が得られます。
PS> .\hash.ps1 FreeBSD-10.0-RELEASE-amd64-disc1.iso md5
md5 (FreeBSD-10.0-RELEASE-amd64-disc1.iso) = fd25619fa0d69c29bea8347b1070ac75
大文字でハッシュを指定すると大文字のハッシュ値が得られます。
PS> .\hash.ps1 FreeBSD-10.0-RELEASE-amd64-disc1.iso MD5
MD5 (FreeBSD-10.0-RELEASE-amd64-disc1.iso) = FD25619FA0D69C29BEA8347B1070AC75

©2004-2017 UPKEN IPv4