Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion PSModule/Sodium/Sodium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ public static KeyPairBase64 GenerateKeyPairBase64(string seedText)
}

public static string DerivePublicKeyBase64(string privateKeyBase64)
{
return Convert.ToBase64String(DerivePublicKey(privateKeyBase64));
}

public static byte[] DerivePublicKey(string privateKeyBase64)
{
ArgumentNullException.ThrowIfNull(privateKeyBase64);
var privateKey = DecodeBase64Exact(privateKeyBase64, SecretKeyBytes, "private key");
Expand All @@ -213,7 +218,7 @@ public static string DerivePublicKeyBase64(string privateKeyBase64)
{
throw new InvalidOperationException("Unable to derive public key from private key.");
}
return Convert.ToBase64String(publicKey);
return publicKey;
}
finally
{
Expand Down
4 changes: 0 additions & 4 deletions src/functions/public/ConvertFrom-SodiumSealedBox.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@
[string] $PrivateKey
)

begin {
Initialize-Sodium
}

process {
try {
if (-not [string]::IsNullOrWhiteSpace($PublicKey)) {
Expand Down
4 changes: 0 additions & 4 deletions src/functions/public/ConvertTo-SodiumSealedBox.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@
[ValidateNotNullOrEmpty()]
[string] $PublicKey
)
begin {
Initialize-Sodium
}

process {
try {
return [PSModule.Sodium]::SealBase64($Message, $PublicKey)
Expand Down
7 changes: 1 addition & 6 deletions src/functions/public/Get-SodiumPublicKey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,10 @@
[switch] $AsByteArray
)

begin {
Initialize-Sodium
}

process {
if ($AsByteArray) {
try {
return [System.Convert]::FromBase64String([PSModule.Sodium]::DerivePublicKeyBase64($PrivateKey))
return [PSModule.Sodium]::DerivePublicKey($PrivateKey)
} catch [System.Management.Automation.MethodInvocationException] {
throw $_.Exception.InnerException
}
Expand All @@ -100,5 +96,4 @@
}
}

end {}
}
4 changes: 0 additions & 4 deletions src/functions/public/New-SodiumKeyPair.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@
[string] $Seed
)

begin {
Initialize-Sodium
}

process {
try {
if ($PSCmdlet.ParameterSetName -eq 'SeededKeyPair') {
Expand Down
50 changes: 50 additions & 0 deletions tests/Sodium.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ Describe 'Sodium' {
$derivedPublicKey | Should -Be $expectedPublicKey
}

It 'Get-SodiumPublicKey - Returns the public key as a byte array' {
$keyPair = New-SodiumKeyPair
$derivedPublicKey = Get-SodiumPublicKey -PrivateKey $keyPair.PrivateKey -AsByteArray

$derivedPublicKey | Should -BeOfType ([byte])
Comment thread
MariusStorhaug marked this conversation as resolved.
Outdated
[Convert]::ToBase64String($derivedPublicKey) | Should -Be $keyPair.PublicKey
}

It 'Get-SodiumPublicKey - Throws an error when an invalid private key is provided' {
$invalidPrivateKey = 'InvalidKey'

Expand All @@ -186,4 +194,46 @@ Describe 'Sodium' {
}
}
}

Context 'Parallel sessions' {
It 'Loads and completes crypto round trips in parallel runspaces' {
$modulePath = (Get-Module -Name Sodium -ErrorAction Stop).Path
Test-Path -Path $modulePath | Should -BeTrue
$results = 1..4 | ForEach-Object -Parallel {
Import-Module -Name $using:modulePath -Force
$keyPair = New-SodiumKeyPair -Seed "Runspace-$_"
$message = "Parallel runspace $_"
$sealedBox = ConvertTo-SodiumSealedBox -Message $message -PublicKey $keyPair.PublicKey
ConvertFrom-SodiumSealedBox -SealedBox $sealedBox -PrivateKey $keyPair.PrivateKey
} -ThrottleLimit 4

$results | Should -HaveCount 4
$results | Should -Contain 'Parallel runspace 1'
$results | Should -Contain 'Parallel runspace 4'
}

It 'Loads and completes crypto round trips in parallel processes' {
$modulePath = (Get-Module -Name Sodium -ErrorAction Stop).Path
$jobs = 1..4 | ForEach-Object {
Start-Job -ScriptBlock {
$id = $args[0]
$modulePath = $args[1]
Import-Module -Name $modulePath -Force
$keyPair = New-SodiumKeyPair -Seed "Process-$id"
$message = "Parallel process $id"
$sealedBox = ConvertTo-SodiumSealedBox -Message $message -PublicKey $keyPair.PublicKey
ConvertFrom-SodiumSealedBox -SealedBox $sealedBox -PrivateKey $keyPair.PrivateKey
} -ArgumentList $_, $modulePath
}

try {
$results = $jobs | Receive-Job -Wait
$results | Should -HaveCount 4
$results | Should -Contain 'Parallel process 1'
$results | Should -Contain 'Parallel process 4'
} finally {
$jobs | Remove-Job -Force
}
}
}
}
Loading