-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConvertTo-SodiumSealedBox.ps1
More file actions
65 lines (53 loc) · 2.05 KB
/
Copy pathConvertTo-SodiumSealedBox.ps1
File metadata and controls
65 lines (53 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function ConvertTo-SodiumSealedBox {
<#
.SYNOPSIS
Encrypts a message using a sealed public key box.
.DESCRIPTION
This function encrypts a given message using a public key with the SealedPublicKeyBox method from the Sodium library.
The result is a base64-encoded sealed box that can only be decrypted by the corresponding private key.
.EXAMPLE
ConvertTo-SodiumSealedBox -Message "Hello world!" -PublicKey $publicKey
Output:
```powershell
hhCon4PO1X0TIPeh1i4GM6Wg9HSF5ge/x4L7p1vNd3lIdiJqNmBfswkcHipyM4HUr9wDLebjARVp5tsB
```
Encrypts the message "Hello world!" using the provided base64-encoded public key and returns a base64-encoded sealed box.
.EXAMPLE
"Sensitive Data" | ConvertTo-SodiumSealedBox -PublicKey $publicKey
Output:
```powershell
p3PGL162uLCvrsCRLUDrc/Kfc5biGVzxRDg25ZdJoR9Y6ABZUKo8pvDoOGdchv0iBYQO2LP0Q6BkVbIDBUw=
```
Uses pipeline input to encrypt the provided message using the specified public key.
.OUTPUTS
System.String
.NOTES
The function returns a base64-encoded sealed box string that can only be decrypted by the corresponding private key.
.LINK
https://psmodule.io/Sodium/Functions/ConvertTo-SodiumSealedBox/
.LINK
https://doc.libsodium.org/public-key_cryptography/sealed_boxes
#>
[OutputType([string])]
[CmdletBinding()]
param(
# The message string to be encrypted.
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName
)]
[string] $Message,
# The base64-encoded public key used for encryption.
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $PublicKey
)
process {
try {
return [PSModule.Sodium]::SealBase64($Message, $PublicKey)
} catch [System.Management.Automation.MethodInvocationException] {
throw $_.Exception.InnerException
}
}
}