-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDetachedSign.psm1
130 lines (114 loc) · 3.7 KB
/
DetachedSign.psm1
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<#
.Synopsis
Creates a OpenPGP Detached signature for a file.
.DESCRIPTION
Creates a OpenPGP Detached signature for a file given a secret key and its paraphrase.
.EXAMPLE
$seckey = Get-PGPSecretKey -keyring $env:APPDATA\gnupg\secring.gpg -UserId "Carlos"
PS C:\ > New-PGPDetachedSignature -SecretKey $seckey -File C:\evidence.txt -PassPhrase (Read-Host -AsSecureString) -Armour -OutFile C:\evidence.sig -Algorithm SHA1
#>
function New-PGPDetachedSignature
{
[CmdletBinding()]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullorEmpty()]
[Org.BouncyCastle.Bcpg.OpenPgp.PgpSecretKey]$SecretKey,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateScript({Test-Path $_})]
[string]$File,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage = "Secure String representing the passphase for the key.")]
[securestring]$PassPhrase,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[string]$OutFile,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[ValidateSet('SHA1', "SHA256",'SHA384','SHA512','RIPEMD160')]
[string]$HashAlgorithm = 'SHA512',
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[switch]$Armour
)
Begin
{
}
Process
{
if ($HashAlgorithm)
{
Write-Verbose "Using hash algorithm $($HashAlgorithm)"
}
else
{
Write-Verbose "Using preferred hash algorithm $($SecretKey.PreferedHash[0])"
$HashAlgorithm = $SecretKey.PreferedHash[0]
}
Write-Verbose "Creating detached signature for $($File) as $($OutFile)"
$instream = ($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($File))
$outstream = [System.IO.File]::Create(($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutFile)))
[PGPHelper.DetachedSignedFileProcessor]::CreateSignature($instream,
$SecretKey,
$outstream,
([Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($PassPhrase))),
$Armour,
$HashAlgorithm
)
$outstream.close()
Write-Verbose "Detached signatured saved as $($OutFile)"
}
End
{
}
}
<#
.Synopsis
Confirms the integrity of a file using an OpenPG Detached Signature.
.DESCRIPTION
Confirms the integrity of a file using an OpenPG Detached Signature.
.EXAMPLE
Confirm-PGPDetachedSignature -File C:\evidence.txt -Signature C:\evidence.sig -KeyRing $env:APPDATA\gnupg\pubring.gpg
Valid : True
Created : 10/4/2013 12:26:28 AM
KeyID : DCC9422A3F0DB692
HashAlgorithm : Sha512
Version : 4
Signature : Org.BouncyCastle.Bcpg.OpenPgp.PgpSignature
#>
function Confirm-PGPDetachedSignature
{
[CmdletBinding()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateScript({Test-Path $_})]
[string]$File,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[string]$Signature,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateScript({Test-Path $_})]
$KeyRing
)
Begin
{
}
Process
{
[PGPHelper.DetachedSignedFileProcessor]::VerifySignature($File, $Signature, $KeyRing)
}
End
{
}
}