Skip to content

Commit a7c7fed

Browse files
committed
Added cmdlets: Get-PrivateKey, Get-PublicKey for given csp
1 parent ffdb117 commit a7c7fed

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Crypto.RSA/Public/Get-PrivateKey.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function Get-PrivateKey {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $false, Position = 0 )]
5+
[System.Security.Cryptography.RSACryptoServiceProvider]$cryptoServiceProvider
6+
)
7+
8+
begin {
9+
Write-Verbose "Cmdlet Get-PrivateKey - Begin"
10+
}
11+
12+
process {
13+
Write-Verbose "Cmdlet Get-PrivateKey - Process"
14+
if ($cryptoServiceProvider -eq $null) {
15+
$cryptoServiceProvider = [System.Security.Cryptography.RSACryptoServiceProvider]::new(2048)
16+
}
17+
Get-KeyString $cryptoServiceProvider.ExportParameters($true)
18+
}
19+
20+
end {
21+
Write-Verbose "Cmdlet Get-PrivateKey - End"
22+
}
23+
}

Crypto.RSA/Public/Get-PublicKey.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function Get-PublicKey {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $false, Position = 0 )]
5+
[System.Security.Cryptography.RSACryptoServiceProvider]$cryptoServiceProvider
6+
)
7+
8+
begin {
9+
Write-Verbose "Cmdlet Get-PublicKey - Begin"
10+
}
11+
12+
process {
13+
Write-Verbose "Cmdlet Get-PublicKey - Process"
14+
if ($cryptoServiceProvider -eq $null) {
15+
$cryptoServiceProvider = [System.Security.Cryptography.RSACryptoServiceProvider]::new(2048)
16+
}
17+
Get-KeyString $cryptoServiceProvider.ExportParameters($false)
18+
}
19+
20+
end {
21+
Write-Verbose "Cmdlet Get-PublicKey - End"
22+
}
23+
}

Tests/Crypto.RSA.Tests.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ Describe 'Crypto.RSA.Tests' {
99
$keys.public | Should -Not -BeNullOrEmpty
1010
$keys.private | Should -Not -BeNullOrEmpty
1111
}
12+
13+
It "Generates private key - using the same csp" {
14+
$csp = [System.Security.Cryptography.RSACryptoServiceProvider]::new(2048)
15+
$keys = New-KeyPair $csp
16+
Get-PrivateKey $csp | Should Be $keys.private
17+
}
18+
19+
It "Generates private key - using different csp" {
20+
$keys = New-KeyPair
21+
Get-PrivateKey | Should Not Be $keys.private
22+
}
23+
24+
It "Generates public key - using the same csp" {
25+
$csp = [System.Security.Cryptography.RSACryptoServiceProvider]::new(2048)
26+
$keys = New-KeyPair $csp
27+
Get-PublicKey $csp | Should Be $keys.public
28+
}
29+
30+
It "Generates public key - using different csp" {
31+
$keys = New-KeyPair
32+
Get-PublicKey | Should Not Be $keys.public
33+
}
1234
}
1335

1436
Context "String encryption - happy path" {

0 commit comments

Comments
 (0)