Skip to content

Commit b2ae5fa

Browse files
🌟 [Major]: Align function naming with libsodium (#22)
## Description This pull request attempts to align naming of functions, parameters and outputs to be closer with `libsodiium` terminology. This includes significant changes to the encryption and decryption functions, transitioning from using the term "SodiumEncryptedString" to "SodiumSealedBox". - Fixes #20 ### Changes to function names and parameters * `ConvertFrom-SodiumEncryptedString` renamed to `ConvertFrom-SodiumSealedBoxString`, with updates to function name, parameters, and documentation to use "SealedBox" instead of "EncryptedString". Also updates the parameter from `Secret` to `SealedBox` to better align with `libsodium`. * `ConvertTo-SodiumEncryptedString` renamed to `ConvertTo-SodiumSealedBoxString`, with updates to function name, parameters, and documentation to use "SealedBox" instead of "EncryptedString". Also uipdates the parameter from `Secret` to `Message` to better align with `libsodium`. ### Updates to examples * [`examples/CreateGitHubSecret.ps1`](diffhunk://#diff-b7943e4c6e438d9b3c4c861bdc8fe422a1d5e5df32be6f6aab3e34edb699a149L4-R4): Updated the encryption function call to use `ConvertTo-SodiumSealedBox` instead of `ConvertTo-SodiumEncryptedString`. * [`examples/GitHubSecretService.ps1`](diffhunk://#diff-fe6c0fb0a8d439a5e6ed90bbaa6394857934aaf8c8771ef862546e69ca9437a6L25-R25): Updated encryption and decryption function calls to use `ConvertTo-SodiumSealedBox` and `ConvertFrom-SodiumSealedBox` respectively. [[1]](diffhunk://#diff-fe6c0fb0a8d439a5e6ed90bbaa6394857934aaf8c8771ef862546e69ca9437a6L25-R25) [[2]](diffhunk://#diff-fe6c0fb0a8d439a5e6ed90bbaa6394857934aaf8c8771ef862546e69ca9437a6L46-R50) ### Documentation updates * [`README.md`](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L40-R67): Updated examples to use "Sealed Boxes encryption" and changed function calls to `ConvertTo-SodiumSealedBox` and `ConvertFrom-SodiumSealedBox`. ### Test updates: * [`tests/Module.Tests.ps1`](diffhunk://#diff-a94f0e90abaaff044e3cb0327ce3a63a10cd61551269a414e4dc40ad2af8a951L2-R75): Added new tests for encryption and decryption using `ConvertTo-SodiumSealedBox` and `ConvertFrom-SodiumSealedBox`, including error cases for invalid keys. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [x] 🌟 [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 894a50e commit b2ae5fa

11 files changed

Lines changed: 179 additions & 169 deletions

README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Import-Module -Name Sodium
2828

2929
The module provides functionality to create a new cryptographic key pair.
3030
The keys are returned as a PowerShell custom object with `PublicKey` and `PrivateKey` properties, encoded in base64 format.
31+
For more info on the key pair generation, refer to the [Public-key signatures documentation](https://doc.libsodium.org/public-key_cryptography/public-key_signatures).
3132

3233
```powershell
3334
New-SodiumKeyPair
@@ -37,30 +38,34 @@ PublicKey PrivateKey
3738
9fv51aqi00MYN4UR7Ew/DLXMS9t1NapLs7yyo+vegz4= MiJAFUZxZ1UCbQTwKfH7HY6AhIFYQlnok5fBD2K+y/g=
3839
```
3940

40-
### Example 2: Encrypt a secret using a public key
41+
## Example 2: Encrypt a message using a public key (Sealed Boxes encryption)
4142

42-
After generating a key pair, a secret can be encrypted using the associated public key.
43-
Below, a secret is encrypted using the public key from the previous example.
43+
After generating a key pair, a message can be encrypted using the associated public key with [Sealed Boxes encryption](https://doc.libsodium.org/public-key_cryptography/sealed_boxes).
44+
Below, a message is encrypted using the public key from the previous example.
4445

4546
```powershell
46-
ConvertTo-SodiumEncryptedString -Secret "mysecret" -PublicKey "9fv51aqi00MYN4UR7Ew/DLXMS9t1NapLs7yyo+vegz4="
47+
$params = @{
48+
Message = "mymessage"
49+
PublicKey = "9fv51aqi00MYN4UR7Ew/DLXMS9t1NapLs7yyo+vegz4="
50+
}
51+
ConvertTo-SodiumSealedBox @params
4752
4853
905j4S/JyP9XBBmOIdHSOXiDu7fUtZo9TFIMnAfBMESgcVBwttLnEyxJn4xPEX5OMKQ+Bc4P6Hg=
4954
```
5055

51-
### Example 3: Decrypt a Sodium-encrypted string
56+
## Example 3: Decrypt a Sodium-encrypted sealed box string
5257

53-
To decrypt an encrypted string, both the private and public keys are required.
58+
To decrypt a string that was encrypted using [Sealed Boxes encryption](https://doc.libsodium.org/public-key_cryptography/sealed_boxes), both the private and public keys are required.
5459

5560
```powershell
5661
$params = @{
57-
EncryptedSecret = '905j4S/JyP9XBBmOIdHSOXiDu7fUtZo9TFIMnAfBMESgcVBwttLnEyxJn4xPEX5OMKQ+Bc4P6Hg='
58-
PublicKey = '9fv51aqi00MYN4UR7Ew/DLXMS9t1NapLs7yyo+vegz4='
59-
PrivateKey = 'MiJAFUZxZ1UCbQTwKfH7HY6AhIFYQlnok5fBD2K+y/g='
62+
SealedBox = '905j4S/JyP9XBBmOIdHSOXiDu7fUtZo9TFIMnAfBMESgcVBwttLnEyxJn4xPEX5OMKQ+Bc4P6Hg='
63+
PublicKey = '9fv51aqi00MYN4UR7Ew/DLXMS9t1NapLs7yyo+vegz4='
64+
PrivateKey = 'MiJAFUZxZ1UCbQTwKfH7HY6AhIFYQlnok5fBD2K+y/g='
6065
}
61-
ConvertFrom-SodiumEncryptedString @params
66+
ConvertFrom-SodiumSealedBox @params
6267
63-
mysecret
68+
mymessage
6469
```
6570

6671
### Finding More Examples

examples/CreateGitHubSecret.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#Requires -Module GitHub
22

33
$response = Invoke-GitHubAPI -ApiEndpoint '/orgs/PSModule/actions/secrets/public-key' | Select-Object -ExpandProperty response
4-
$sealedPublicKeyBox = ConvertTo-SodiumEncryptedString -Secret 'mysecret' -PublicKey $response.key
4+
$sealedPublicKeyBox = ConvertTo-SodiumSealedBox -Secret 'mysecret' -PublicKey $response.key
55
Invoke-GitHubAPI -ApiEndpoint '/orgs/PSModule/actions/secrets/mysecret' -Method PUT -Body @{
66
encrypted_value = $sealedPublicKeyBox
77
key_id = $response.key_id

examples/GitHubSecretService.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $userInfo = @{
2222
}
2323

2424
# 3a) You encrypt the secret with the public key.
25-
$encryptedSecret = ConvertTo-SodiumEncryptedString -Secret $secret -PublicKey $userInfo.PublicKey
25+
$encryptedSecret = ConvertTo-SodiumSealedBox -Message $secret -PublicKey $userInfo.PublicKey
2626

2727
# 3b) You send the encrypted secret to GitHub with the name of the secret and the ID GitHub sent you.
2828
$secretInfo = @{
@@ -43,11 +43,11 @@ $GitHubSecretStore[$secretInfo.SecretName] = [pscustomobject]@{
4343
# 5) When used in GitHub Actions, the GitHub Secret Service likely ONLY trusts the 'GitHub Actions' App,
4444
# and retrieves the secret by its name.
4545
$actionParams = @{
46-
Secret = $GitHubSecretStore[$secretName].Secret
46+
SealedBox = $GitHubSecretStore[$secretName].Secret
4747
PublicKey = $GitHubSecretStore[$secretName].PublicKey
4848
PrivateKey = $GitHubSecretStore[$secretName].PrivateKey
4949
}
50-
$decryptedString = ConvertFrom-SodiumEncryptedString @actionParams
50+
$decryptedString = ConvertFrom-SodiumSealedBox @actionParams
5151

5252
# 6) The decrypted secret is now available for use in the GitHub Action.
5353
Write-Warning $decryptedString

src/functions/private/ConvertTo-ByteArray.ps1

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/functions/private/Initialize-Sodium.ps1

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/functions/public/ConvertFrom-SodiumEncryptedString.ps1 renamed to src/functions/public/ConvertFrom-SodiumSealedBox.ps1

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
function ConvertFrom-SodiumEncryptedString {
1+
function ConvertFrom-SodiumSealedBox {
22
<#
33
.SYNOPSIS
4-
Decrypts a base64-encoded, Sodium-encrypted string.
4+
Decrypts a base64-encoded, Sodium SealedBox-encrypted string.
55
66
.DESCRIPTION
7-
Converts a base64-encoded, Sodium-encrypted string into its original plaintext form.
7+
Converts a base64-encoded, Sodium SealedBox-encrypted string into its original plaintext form.
88
Uses the provided public and private keys to decrypt the sealed message.
99
1010
.EXAMPLE
1111
$params = @{
12-
EncryptedSecret = $encryptedSecret
12+
SealedBox = $encryptedMessage
1313
PublicKey = $publicKey
1414
PrivateKey = $privateKey
1515
}
16-
ConvertFrom-SodiumEncryptedString @params
16+
ConvertFrom-SodiumSealedBox @params
1717
18-
Decrypts the given encrypted secret using the specified public and private keys and returns the original string.
18+
Decrypts the given encrypted message using the specified public and private keys and returns the original string.
1919
2020
.LINK
21-
https://psmodule.io/Sodium/Functions/ConvertFrom-SodiumEncryptedString/
21+
https://psmodule.io/Sodium/Functions/ConvertFrom-SodiumSealedBox/
22+
23+
.LINK
24+
https://doc.libsodium.org/public-key_cryptography/sealed_boxes
2225
#>
2326
[OutputType([string])]
2427
[CmdletBinding()]
2528
param(
2629
# The base64-encoded encrypted secret string to decrypt.
2730
[Parameter(Mandatory)]
28-
[string] $Secret,
31+
[Alias('CipherText')]
32+
[string] $SealedBox,
2933

3034
# The base64-encoded public key used for decryption.
3135
[Parameter(Mandatory)]
@@ -37,13 +41,13 @@
3741
)
3842

3943
begin {
40-
Initialize-Sodium
44+
$null = [PSModule.Sodium]::sodium_init()
4145
}
4246

4347
process {
44-
$ciphertext = [Convert]::FromBase64String($Secret)
45-
$publicKeyByteArray = ConvertTo-ByteArray $PublicKey
46-
$privateKeyByteArray = ConvertTo-ByteArray $PrivateKey
48+
$ciphertext = [Convert]::FromBase64String($SealedBox)
49+
$publicKeyByteArray = [Convert]::FromBase64String($PublicKey)
50+
$privateKeyByteArray = [Convert]::FromBase64String($PrivateKey)
4751

4852
if ($publicKeyByteArray.Length -ne 32) { throw 'Invalid public key.' }
4953
if ($privateKeyByteArray.Length -ne 32) { throw 'Invalid private key.' }

src/functions/public/ConvertTo-SodiumEncryptedString.ps1

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function ConvertTo-SodiumSealedBox {
2+
<#
3+
.SYNOPSIS
4+
Encrypts a message using a sealed public key box.
5+
6+
.DESCRIPTION
7+
This function encrypts a given message using a public key with the SealedPublicKeyBox method from the Sodium library.
8+
The result is a base64-encoded sealed box that can only be decrypted by the corresponding private key.
9+
10+
.EXAMPLE
11+
ConvertTo-SodiumSealedBox -Message "Hello world!" -PublicKey "BASE64_PUBLIC_KEY"
12+
13+
Encrypts the message "Hello world!" using the provided base64-encoded public key and returns a base64-encoded sealed box.
14+
15+
.LINK
16+
https://psmodule.io/Sodium/Functions/ConvertTo-SodiumSealedBox/
17+
18+
.LINK
19+
https://doc.libsodium.org/public-key_cryptography/sealed_boxes
20+
#>
21+
[OutputType([string])]
22+
[CmdletBinding()]
23+
param(
24+
# The message string to be encrypted.
25+
[Parameter(Mandatory)]
26+
[string] $Message,
27+
28+
# The base64-encoded public key used for encryption.
29+
[Parameter(Mandatory)]
30+
[string] $PublicKey
31+
)
32+
begin {
33+
$null = [PSModule.Sodium]::sodium_init()
34+
}
35+
36+
process {
37+
# Convert public key from Base64 or space-separated string
38+
try {
39+
$publicKeyByteArray = [Convert]::FromBase64String($PublicKey)
40+
} catch {
41+
$PSCmdlet.ThrowTerminatingError($_)
42+
}
43+
if ($publicKeyByteArray.Length -ne 32) {
44+
throw "Invalid public key. Expected 32 bytes but got $($publicKeyByteArray.Length)."
45+
}
46+
47+
$messageBytes = [System.Text.Encoding]::UTF8.GetBytes($Message)
48+
$overhead = [PSModule.Sodium]::crypto_box_sealbytes().ToUInt32()
49+
$cipherLength = $messageBytes.Length + $overhead
50+
$ciphertext = New-Object byte[] $cipherLength
51+
52+
# Encrypt message
53+
$result = [PSModule.Sodium]::crypto_box_seal($ciphertext, $messageBytes, [uint64]$messageBytes.Length, $publicKeyByteArray)
54+
55+
if ($result -ne 0) {
56+
throw 'Encryption failed.'
57+
}
58+
59+
return [Convert]::ToBase64String($ciphertext)
60+
}
61+
}

src/functions/public/New-SodiumKeyPair.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
1717
.LINK
1818
https://psmodule.io/Sodium/Functions/New-SodiumKeyPair/
19+
20+
.LINK
21+
https://doc.libsodium.org/public-key_cryptography/public-key_signatures
1922
#>
2023
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
2124
'PSUseShouldProcessForStateChangingFunctions', '',
@@ -27,7 +30,7 @@
2730
param()
2831

2932
begin {
30-
Initialize-Sodium
33+
$null = [PSModule.Sodium]::sodium_init()
3134
}
3235

3336
process {

tests/Module.Tests.ps1

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)