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]"
}