Skip to content

Commit 20316fc

Browse files
🩹 [Patch]: Support pipeline input on ConvertTo- and ConvertFrom-SodiumSealedBox and New-SodiumKeyPair (#24)
## Description This pull request enhances the `Sodium` PowerShell module by adding support for pipeline input to various functions, updating documentation, and adding new tests to ensure proper functionality. The most important changes include the addition of pipeline input support, updates to function documentation, and new tests for pipeline input scenarios. Enhancements to functions: * [`src/functions/public/ConvertFrom-SodiumSealedBox.ps1`](diffhunk://#diff-afea5aa86ab4622c3a4bff63aa61976494cb620d338df534bf7121018056bbadL30-R56): Added support for `ValueFromPipeline` and `ValueFromPipelineByPropertyName` to the `SealedBox` parameter. * [`src/functions/public/ConvertTo-SodiumSealedBox.ps1`](diffhunk://#diff-be3e1727721972ed3d4275a880537b89ce6fbb16c1d97405e02d7e798b6c7bfbL25-R50): Added support for `ValueFromPipeline` and `ValueFromPipelineByPropertyName` to the `Message` parameter. * [`src/functions/public/New-SodiumKeyPair.ps1`](diffhunk://#diff-b951668ffde5fc1b700af8f8b2b6076d7ce27b0ec23e9d556ad1a893e204c87bL51-R82): Added support for `ValueFromPipeline` to the `Seed` parameter. Documentation updates: * [`src/functions/public/ConvertFrom-SodiumSealedBox.ps1`](diffhunk://#diff-afea5aa86ab4622c3a4bff63aa61976494cb620d338df534bf7121018056bbadR18-R41): Enhanced documentation with examples, outputs, and notes for better clarity. * [`src/functions/public/ConvertTo-SodiumSealedBox.ps1`](diffhunk://#diff-be3e1727721972ed3d4275a880537b89ce6fbb16c1d97405e02d7e798b6c7bfbL11-R35): Enhanced documentation with examples, outputs, and notes for better clarity. * [`src/functions/public/New-SodiumKeyPair.ps1`](diffhunk://#diff-b951668ffde5fc1b700af8f8b2b6076d7ce27b0ec23e9d556ad1a893e204c87bR40-R62): Enhanced documentation with examples, outputs, and notes for better clarity. New tests: * [`tests/Sodium.Tests.ps1`](diffhunk://#diff-f601c73f9a5c51929710c76b4065f37025d3704dae0b299be2a892f7702e6875R66-R94): Added tests to verify encryption and decryption using pipeline input for `ConvertTo-SodiumSealedBox` and `ConvertFrom-SodiumSealedBox`. * [`tests/Sodium.Tests.ps1`](diffhunk://#diff-f601c73f9a5c51929710c76b4065f37025d3704dae0b299be2a892f7702e6875R116-R124): Added tests to verify deterministic key generation using pipeline input for `New-SodiumKeyPair`. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent a372daa commit 20316fc

4 files changed

Lines changed: 121 additions & 5 deletions

File tree

src/functions/public/ConvertFrom-SodiumSealedBox.ps1

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,30 @@
1515
}
1616
ConvertFrom-SodiumSealedBox @params
1717
18+
Output:
19+
```powershell
20+
Secret message revealed!
21+
```
22+
1823
Decrypts the given encrypted message using the specified public and private keys and returns the original string.
1924
25+
.EXAMPLE
26+
$encryptedMessage | ConvertFrom-SodiumSealedBox -PublicKey $publicKey -PrivateKey $privateKey
27+
28+
Output:
29+
```powershell
30+
Confidential Data
31+
```
32+
33+
Uses pipeline input to decrypt the given encrypted message with the specified keys.
34+
35+
.OUTPUTS
36+
System.String
37+
38+
.NOTES
39+
Returns the original plaintext string after decryption.
40+
If decryption fails, an exception is thrown.
41+
2042
.LINK
2143
https://psmodule.io/Sodium/Functions/ConvertFrom-SodiumSealedBox/
2244
@@ -27,7 +49,11 @@
2749
[CmdletBinding()]
2850
param(
2951
# The base64-encoded encrypted secret string to decrypt.
30-
[Parameter(Mandatory)]
52+
[Parameter(
53+
Mandatory,
54+
ValueFromPipeline,
55+
ValueFromPipelineByPropertyName
56+
)]
3157
[Alias('CipherText')]
3258
[string] $SealedBox,
3359

src/functions/public/ConvertTo-SodiumSealedBox.ps1

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,31 @@
88
The result is a base64-encoded sealed box that can only be decrypted by the corresponding private key.
99
1010
.EXAMPLE
11-
ConvertTo-SodiumSealedBox -Message "Hello world!" -PublicKey "BASE64_PUBLIC_KEY"
11+
ConvertTo-SodiumSealedBox -Message "Hello world!" -PublicKey $publicKey
12+
13+
Output:
14+
```powershell
15+
hhCon4PO1X0TIPeh1i4GM6Wg9HSF5ge/x4L7p1vNd3lIdiJqNmBfswkcHipyM4HUr9wDLebjARVp5tsB
16+
```
1217
1318
Encrypts the message "Hello world!" using the provided base64-encoded public key and returns a base64-encoded sealed box.
1419
20+
.EXAMPLE
21+
"Sensitive Data" | ConvertTo-SodiumSealedBox -PublicKey $publicKey
22+
23+
Output:
24+
```powershell
25+
p3PGL162uLCvrsCRLUDrc/Kfc5biGVzxRDg25ZdJoR9Y6ABZUKo8pvDoOGdchv0iBYQO2LP0Q6BkVbIDBUw=
26+
```
27+
28+
Uses pipeline input to encrypt the provided message using the specified public key.
29+
30+
.OUTPUTS
31+
System.String
32+
33+
.NOTES
34+
The function returns a base64-encoded sealed box string that can only be decrypted by the corresponding private key.
35+
1536
.LINK
1637
https://psmodule.io/Sodium/Functions/ConvertTo-SodiumSealedBox/
1738
@@ -22,7 +43,11 @@
2243
[CmdletBinding()]
2344
param(
2445
# The message string to be encrypted.
25-
[Parameter(Mandatory)]
46+
[Parameter(
47+
Mandatory,
48+
ValueFromPipeline,
49+
ValueFromPipelineByPropertyName
50+
)]
2651
[string] $Message,
2752

2853
# The base64-encoded public key used for encryption.

src/functions/public/New-SodiumKeyPair.ps1

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@
3737
Generates a deterministic key pair using the given seed string. The same seed will produce
3838
the same key pair every time.
3939
40+
.EXAMPLE
41+
"MySecureSeed" | New-SodiumKeyPair
42+
43+
Output:
44+
```powershell
45+
PublicKey PrivateKey
46+
--------- ----------
47+
WQakMx2mIAQMwLqiZteHUTwmMP6mUdK2FL0WEybWgB8= ci5/7eZ0IbGXtqQMaNvxhJ2d9qwFxA8Kjx+vivSTXqU=
48+
```
49+
50+
Generates a deterministic key pair using the given seed string via pipeline. The same seed will produce
51+
the same key pair every time.
52+
53+
.OUTPUTS
54+
55+
PSCustomObject
56+
57+
.NOTES
58+
Returns a PowerShell custom object with the following properties:
59+
- **PublicKey**: The base64-encoded public key.
60+
- **PrivateKey**: The base64-encoded private key.
61+
If key generation fails, an exception is thrown.
62+
4063
.LINK
4164
https://psmodule.io/Sodium/Functions/New-SodiumKeyPair/
4265
@@ -48,11 +71,15 @@
4871
Scope = 'Function',
4972
Justification = 'Does not change state'
5073
)]
51-
[OutputType([pscustomobject])]
74+
[OutputType([PSCustomObject])]
5275
[CmdletBinding(DefaultParameterSetName = 'NewKeyPair')]
5376
param(
5477
# A seed value to use for key generation.
55-
[Parameter(Mandatory, ParameterSetName = 'SeededKeyPair')]
78+
[Parameter(
79+
Mandatory,
80+
ParameterSetName = 'SeededKeyPair',
81+
ValueFromPipeline
82+
)]
5683
[string] $Seed
5784
)
5885

tests/Sodium.Tests.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,35 @@
6363
ConvertFrom-SodiumSealedBox -SealedBox $sealedBox -PublicKey $keyPair.PublicKey -PrivateKey $invalidPrivateKey
6464
} | Should -Throw
6565
}
66+
67+
It 'Encrypts a message correctly when using pipeline input on ConvertTo-SodiumSealedBox' {
68+
$keyPair = New-SodiumKeyPair
69+
$publicKey = $keyPair.PublicKey
70+
$privateKey = $keyPair.PrivateKey
71+
$message = 'Pipeline input encryption test'
72+
73+
# Pass the message via pipeline input instead of -Message parameter
74+
$sealedBox = $message | ConvertTo-SodiumSealedBox -PublicKey $publicKey
75+
76+
$decryptedString = ConvertFrom-SodiumSealedBox -SealedBox $sealedBox -PublicKey $publicKey -PrivateKey $privateKey
77+
78+
$decryptedString | Should -Be $message
79+
}
80+
81+
It 'Decrypts a sealed box correctly when using pipeline input on ConvertFrom-SodiumSealedBox' {
82+
$keyPair = New-SodiumKeyPair
83+
$publicKey = $keyPair.PublicKey
84+
$privateKey = $keyPair.PrivateKey
85+
$message = 'Pipeline input decryption test'
86+
87+
# Encrypt using normal parameter binding
88+
$sealedBox = ConvertTo-SodiumSealedBox -Message $message -PublicKey $publicKey
89+
90+
# Pass the sealed box via pipeline input to the decryption function
91+
$decryptedString = $sealedBox | ConvertFrom-SodiumSealedBox -PublicKey $publicKey -PrivateKey $privateKey
92+
93+
$decryptedString | Should -Be $message
94+
}
6695
}
6796

6897
Context 'Key Pair Generation' {
@@ -84,6 +113,15 @@
84113
$keyPair1.PrivateKey | Should -Be $keyPair2.PrivateKey
85114
}
86115

116+
It 'Generates deterministic keys when a seed is provided - using a pipeline input' {
117+
$seed = 'DeterministicSeed'
118+
$keyPair1 = $seed | New-SodiumKeyPair
119+
$keyPair2 = $seed | New-SodiumKeyPair
120+
121+
$keyPair1.PublicKey | Should -Be $keyPair2.PublicKey
122+
$keyPair1.PrivateKey | Should -Be $keyPair2.PrivateKey
123+
}
124+
87125
It 'Generates different keys for different seeds' {
88126
$seed1 = 'SeedOne'
89127
$seed2 = 'SeedTwo'

0 commit comments

Comments
 (0)