DSCResources/POSHOrigin_vSphere_VM/Provisioners/Chef/helpers/_GetChecksum.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<#
    .SYNOPSIS
    Return the checksum of the specified file
#>

param (
    [Parameter(ParameterSetName="file")]
    [string]
    # Path to the file to get the checksum for
    $path,

    [Parameter(ParameterSetName="string")]
    [string]
    # The string to get the checksum for
    $string,

    [ValidateSet("sha1", "md5", "sha256")]
    [string]
    # Algorithm to use when generating the checksum
    $algorithm = "md5",

    [string]
    # The encoding method to use
    $encoding = "ASCII",

    [switch]
    # If trim is specified then the system will trim whitespace from the begining and end of input
    $trim,

    [switch]
    # Disable the Base64 encoding
    $nobase64
)

# $md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
# $hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($path)))

$encoder = "{0}Encoding" -f $encoding

$algo = "{0}CryptoServiceProvider" -f $algorithm
$provider = New-Object -TypeName System.Security.Cryptography.$algo
$engine = New-Object -TypeName System.Text.$encoder

# Use the ParameterSetName to determine if a string or a path has been specified
# If it is a path then get the contents
switch ($PScmdlet.ParameterSetName) {
    "file" {
        $string = Get-Content -Path $path -Raw -Encoding UTF8
    }
}

# Strip characters from the string
# This is the the UNIX to Windows line ending problem
# Ruby is UNIX based and will strip LF from files when they are read in
#$string = $string -replace "`r", ""

# work out the checksum of the file
$hash = ([System.BitConverter]::ToString($provider.ComputeHash($engine.GetBytes($string)))).replace("-", "").tolower()

if ($nobase64) {
    $checksum = $hash
} else {
    # So that the hash is the same as that is generated by chef-client, it needs to be packed
    # and then base64 encoded
    $packed = for($i = 0; $i -lt $hash.length; $i += 2) {
        [char][int]::Parse($hash.substring($i,2), 'HexNumber')
    }

    # Now build up the checksum that is base64 encoded
    $checksum = & "$PSScriptRoot\_GetBase64.ps1" -data $packed
}

# Return the hash to the calling function
return $checksum