Showing posts with label hash. Show all posts
Showing posts with label hash. Show all posts

3/13/10

Hash Message Digest Comparisons

Hash    Bits  Bytes
MD5     128   16  (128/8 = 16)
SHA1    160   20
SHA224  224   28
SHA256  256   32
SHA384  384   48
SHA512  512   64

8/19/09

Hash Functions

GetHash returns the hash value of a byte array.
GetHashStr returns the hash value of a string.

These functions can use any of the hash classes from the System.Security.Cryptography namespace, e.g. MD5, SHA1, SHA256, SHA512.
//C#
using System;
using System.Security.Cryptography;
using System.Text;

namespace ExpressionSoftware.Security.Crypt
{
    public static class Hash
    {
        public static byte[] GetHash(byte[] bs, Type hashType)
        {
            return HashAlgorithm.Create(hashType.Name).ComputeHash(bs);
        }
        
        public static byte[] GetHashStr(string input, Type hashType)
        {
            byte[] bs = Encoding.UTF8.GetBytes(input);
            return GetHash(bs, hashType);
        }
    }
}

//F# v1.9.7.8
namespace ExpressionSoftware.Security.Crypt
open System
open System.Security.Cryptography
open System.Text

module Hash =
  let GetHash(bs:byte[], hashType:Type) =
    HashAlgorithm.Create(hashType.Name).ComputeHash bs

  let GetHashStr(input:string, hashType:Type) =
    Encoding.UTF8.GetBytes input
    |> (fun bs -> GetHash(bs, hashType))

#PowerShell
function getHash($bs, $hashType)
{
  $hashType::create().computeHash($bs)
}

function getHashStr($inputStr, $hashType)
{
  getHash ([system.text.encoding]::utf8.getBytes($inputStr)) $hashType
}

Examples
//C#
byte[] bs = Encoding.UTF8.GetBytes("foo");
byte[] hash = Hash.GetHash(bs, typeof(SHA1));
Debug.WriteLine(Byte.BytesToString(hash, "{0} "));
11 238 199 181 234 63 15 219 201 93 13 212 127 60 91 194 117 218 138 51  //output

//F#
let hash = Hash.GetHashStr("foo", typeof<SHA1>)
Byte.BytesToString(hash, "{0:x} ")
|> (fun s -> printfn "%s" s)
b ee c7 b5 ea 3f f db c9 5d d d4 7f 3c 5b c2 75 da 8a 33  //output

#PowerShell
$hashType = [system.security.cryptography.SHA1]
$hash = getHashStr 'foo' $hashType
bytesToString $hash '{0:X}'
BEEC7B5EA3FFDBC95DDD47F3C5BC275DA8A33  #output

5/30/09

PowerShell Hash Scripts for File Comparisons

See the Windows PowerShell Cookbook recipe 17.10 "Get the MD5 or SHA1 Hash of a File", for original script.

GetFileHash returns the hash value for a file. The hash type can be any hash class from the System.Security.Cryptography namespace, e.g. MD5, SHA1, SHA256, SHA512.
CompareFileHash returns true if the files have the same hash.
CompareFileHashInfo returns the hash comparison information.
GetFileHashObject returns a file hash wrapper object.
function getFileHash($file, $hashType)
{
$stream = new-object io.streamReader $file
$hash = $hashType::create().computeHash($stream.baseStream)
$stream.close()
trap
{
if ($stream -ne $null)
{
$stream.close()
}
break
}
[string] $hash
}

function compareFileHash($file1, $file2, $hashType)
{
(getFileHash $file1 $hashType) -eq (getFileHash $file2 $hashType)
}

function compareFileHashInfo($file1, $file2, $hashType)
{
$f1 = getFileHashObject $file1 $hashType
$f2 = getFileHashObject $file2 $hashType

$f1
$f2
"Hash Type: " + $hashType.name
"Duplicate Files: " + ($f1.hash -eq $f2.hash)
}

function getFileHashObject($file, $hashType)
{
$fileHash = new-object psObject
$fileHash | add-member noteProperty file $file
$fileHash | add-Member noteProperty hash (getFileHash $file $hashType)
return $fileHash
}

Examples
>> $hashType = [system.security.cryptography.MD5]
>> getFileHash c:\file1.txt $hashType
52 85 168 127 81 223 163 157 187 191 244 34 221 99 48 201

>> compareFileHash c:\file1.txt c:\file2.txt $hashType
False

>> compareFileHashInfo c:\file1.txt c:\file2.txt $hashType | fl
file : c:\file1.txt
hash : 52 85 168 127 81 223 163 157 187 191 244 34 221 99 48 201

file : c:\file2.txt
hash : 100 221 101 253 63 62 26 193 155 80 181 198 78 247 255 196

Hash Type: MD5
Duplicate Files: False