public/Set-ChocolateyDecryptKey.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

$chocolateyPrivateKey = $null;

function Set-ChocolateyDecryptKey() {
    Param(
        [Parameter(Position = 0,ValueFromPipeline = $true)]
        [Byte[]] $Key
    )

    $chocolateyPrivateKey = $Key
}

function Get-ChocolateyDecryptKey() {
    if($chocolateyPrivateKey) {
        return $chocolateyPrivateKey
    }

    if($Env:CHOCOLATEY_DECRYPT_KEY) {
        switch -Regex ($Env:CHOCOLATEY_DECRYPT_KEY) {
            "^file:" {  
                $file = $Env:CHOCOLATEY_DECRYPT_KEY
                $file = $file.Replace("file://", "")
                $chocolateyPrivateKey = [System.IO.File]::ReadAllBytes($file);
            }
            "^secureString:" {
                $name = $Env:CHOCOLATEY_DECRYPT_KEY
                $name = $name.Replace("secureString://", "")
                $value = Get-Item Variable:\$name
                $chocolateyPrivateKey = ConvertTo-UnprotectedBytes -SecureString $value  
            }
            "^thumprint:" {
                Write-Warning "Not supported yet"
            }
            Default {
                $chocolateyPrivateKey = [System.Text.Encoding]::UTF8.GetBytes($Env:CHOCOLATEY_DECRYPT_KEY)
            }
        }

        return $chocolateyPrivateKey;
    }

    return $false;
}