Skip to content

Commit 0ba6173

Browse files
committed
!deploy v0.2.1 with additional functionality on New-RSAKeyPair to resolve #5
## 0.2.1 - 2019-08-24 * [Issue #5](#5) * Added: Missing path creation to `New-RSAKeyPair` if the parent folder does not yet exist. * Miscellaneous * Fixed: Error handling while using `New-RSAKeyPair -Interactive` if the specified Path already exists and `-Force` was not specified. * Added: Additional status updates within `New-RSAKeyPair` for a better understanding of what step the key generation is at.
1 parent ef8185d commit 0ba6173

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
* [PEMEncrypt - ChangeLog](#pemencrypt---changelog)
2+
* [0.2.1 - 2019-08-24](#021---2019-08-24)
23
* [0.2.0 - 2019-08-10](#020---2019-08-10)
34
* [0.1.1 - 2019-07-07](#011---2019-07-07)
45
* [0.1.0 - 2019-07-07](#010---2019-07-07)
@@ -7,6 +8,14 @@
78

89
# PEMEncrypt - ChangeLog
910

11+
## 0.2.1 - 2019-08-24
12+
13+
* [Issue #5](https://github.com/scrthq/PEMEncrypt/issues/5)
14+
* Added: Missing path creation to `New-RSAKeyPair` if the parent folder does not yet exist.
15+
* Miscellaneous
16+
* Fixed: Error handling while using `New-RSAKeyPair -Interactive` if the specified Path already exists and `-Force` was not specified.
17+
* Added: Additional status updates within `New-RSAKeyPair` for a better understanding of what step the key generation is at.
18+
1019
## 0.2.0 - 2019-08-10
1120

1221
* Added `New-RSAKeyPair` to enable generation of RSA PEM and SSH keys directly from PowerShell

PEMEncrypt/PEMEncrypt.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'PEMEncrypt.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.2.0'
15+
ModuleVersion = '0.2.1'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = @('Desktop','Core')

PEMEncrypt/Public/New-RSAKeyPair.ps1

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ function New-RSAKeyPair {
136136
$Path
137137
}
138138
if (-not $Force -and (Test-Path $newPath)) {
139-
throw "Key already exists at desired path: $newPath. Use -Force to overwrite the existing key or choose a different path"
139+
Write-Error "Key already exists at desired path: $newPath. Use -Force to overwrite the existing key or choose a different path"
140+
return
141+
}
142+
$parent = Split-Path $newPath -Parent
143+
if (-not (Test-Path $parent)) {
144+
Write-Host "Creating missing parent folder: $parent"
145+
New-Item -ItemType Directory $parent -Force | Out-Null
140146
}
141147
}
142148
$Length = if ($choice = Read-Host -Prompt "Enter desired key bit length (Default: 4096)") {
@@ -149,15 +155,17 @@ function New-RSAKeyPair {
149155
if (-not ([System.String]::IsNullOrEmpty((Unprotect-SecureString -SecureString $Password)))) {
150156
$confirmed = Read-Host -AsSecureString -Prompt "Enter the same passphrase to confirm"
151157
if ((Unprotect-SecureString -SecureString $confirmed) -ne (Unprotect-SecureString -SecureString $Password)) {
152-
Write-Warning "Passphrases provided do not match! Exiting"
153-
throw
158+
Write-Error "Passphrases provided do not match! Exiting"
159+
return
154160
}
161+
Write-Host "Generating passphrase protected key pair"
155162
$keys = [SCRTHQ.PEMEncrypt.RSA]::Generate(
156163
$Length,
157164
(Unprotect-SecureString -SecureString $Password)
158165
)
159166
}
160167
else {
168+
Write-Host "Generating key pair"
161169
$keys = [SCRTHQ.PEMEncrypt.RSA]::Generate(
162170
$Length
163171
)
@@ -181,22 +189,37 @@ function New-RSAKeyPair {
181189
}
182190
}
183191
else {
184-
$keys = if ($PSBoundParameters.ContainsKey('Password')) {
185-
[SCRTHQ.PEMEncrypt.RSA]::Generate(
186-
$Length,
187-
$(if($Password -is [SecureString]){(Unprotect-SecureString -SecureString $Password)}else{"$Password"})
188-
)
192+
if (-not $NoFile -and -not $Force -and (Test-Path $Path)) {
193+
Write-Error "Key already exists at desired path: $Path. Use -Force to overwrite the existing key or choose a different path."
194+
return
189195
}
190196
else {
191-
[SCRTHQ.PEMEncrypt.RSA]::Generate(
192-
$Length
193-
)
194-
}
195-
if (-not $NoFile) {
196-
if (-not $Force -and (Test-Path $Path)) {
197-
throw "Key already exists at desired path: $Path. Use -Force to overwrite the existing key or choose a different path."
197+
$parent = Split-Path $Path -Parent
198+
if (-not (Test-Path $parent)) {
199+
Write-Host "Creating missing parent folder: $parent"
200+
New-Item -ItemType Directory $parent -Force | Out-Null
201+
}
202+
$keys = if ($PSBoundParameters.ContainsKey('Password')) {
203+
Write-Host "Generating passphrase protected key pair"
204+
[SCRTHQ.PEMEncrypt.RSA]::Generate(
205+
$Length,
206+
$(
207+
if ($Password -is [SecureString]) {
208+
(Unprotect-SecureString -SecureString $Password)
209+
}
210+
else {
211+
"$Password"
212+
}
213+
)
214+
)
198215
}
199216
else {
217+
Write-Host "Generating key pair"
218+
[SCRTHQ.PEMEncrypt.RSA]::Generate(
219+
$Length
220+
)
221+
}
222+
if (-not $NoFile) {
200223
Write-Host "Saving private key to path : $Path"
201224
$keys.PrivatePEM | Set-Content -Path $Path -Force
202225
if (-not $NoSSH) {
@@ -210,9 +233,9 @@ function New-RSAKeyPair {
210233
$keys.PublicPEM | Set-Content -Path $pemPath -Force
211234
}
212235
}
213-
}
214-
if ($PassThru -or $NoFile) {
215-
$keys
236+
if ($PassThru -or $NoFile) {
237+
$keys
238+
}
216239
}
217240
}
218241
}

0 commit comments

Comments
 (0)