Skip to content

Commit a372daa

Browse files
🩹 [Patch]: Verify that vcruntime140 is installed on Windows (#23)
## Description - Fixes #13 This pull request includes several changes to improve platform compatibility and ensure the necessary dependencies are installed. The most important changes include adding a new function to check for the Visual C++ Redistributable, updating scripts to verify platform support, and initializing the Sodium library conditionally. ### Platform compatibility improvements: * [`src/functions/private/Assert-VisualCRedistributableInstalled.ps1`](diffhunk://#diff-35f6fe814179c7dc7dbe445edfeaec04e40ead8fd994ff12ff293aa5f6883d6dR1-R47): Added a new function `Assert-VisualCRedistributableInstalled` to check if the Visual C++ Redistributable for Visual Studio 2015 or later is installed and meets the specified minimum version. * [`src/main.ps1`](diffhunk://#diff-75cbc94876914e3617b466284eb14da2baf68299fb67315e8a5a2f6f1c38ab3fR4-R20): Modified the main script to set the `$script:Supported` variable based on platform checks and the result of `Assert-VisualCRedistributableInstalled` for Windows. * [`src/variables/private/Supported.ps1`](diffhunk://#diff-7904f48b2851050ac94d151a468c3e9f107ca59a76c6297d8dfe3adaee8837daR1): Added a new variable `$script:Supported` initialized to `$false` to track platform support status. ### Sodium library initialization: * `src/functions/public/ConvertFrom-SodiumSealedBox.ps1`, `src/functions/public/ConvertTo-SodiumSealedBox.ps1`, `src/functions/public/New-SodiumKeyPair.ps1`: Updated these scripts to throw an error if Sodium is not supported on the platform by checking the `$script:Supported` variable before initializing the Sodium library. [[1]](diffhunk://#diff-afea5aa86ab4622c3a4bff63aa61976494cb620d338df534bf7121018056bbadR44) [[2]](diffhunk://#diff-be3e1727721972ed3d4275a880537b89ce6fbb16c1d97405e02d7e798b6c7bfbR33) [[3]](diffhunk://#diff-b951668ffde5fc1b700af8f8b2b6076d7ce27b0ec23e9d556ad1a893e204c87bR60) ## 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 f88a0f0 commit a372daa

7 files changed

Lines changed: 54 additions & 2 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ PublicKey PrivateKey
5050
WQakMx2mIAQMwLqiZteHUTwmMP6mUdK2FL0WEybWgB8= ci5/7eZ0IbGXtqQMaNvxhJ2d9qwFxA8Kjx+vivSTXqU=
5151
```
5252

53-
54-
5553
### Example 3: Encrypt a message using a public key (Sealed Boxes encryption)
5654

5755
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).
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function Assert-VisualCRedistributableInstalled {
2+
<#
3+
.SYNOPSIS
4+
Determines if a version of the Visual C++ Redistributable is installed and meets the specified minimum version.
5+
6+
.DESCRIPTION
7+
This function checks whether the Visual C++ Redistributable for Visual Studio 2015 or later is installed on
8+
the system and ensures that the installed version is greater than or equal to the specified minimum version.
9+
If the required version is not found, a warning is displayed, suggesting where to download the latest
10+
redistributable package.
11+
12+
.EXAMPLE
13+
Assert-VisualCRedistributableInstalled -Version '14.29.30037'
14+
15+
Output:
16+
```powershell
17+
True
18+
```
19+
20+
Checks if the installed Visual C++ Redistributable version is at least 14.29.30037 and returns `$true`
21+
if the requirement is met.
22+
#>
23+
[CmdletBinding()]
24+
[OutputType([bool])]
25+
param (
26+
# The minimum required version of the Visual C++ Redistributable.
27+
[Parameter(Mandatory)]
28+
[Version] $Version
29+
)
30+
31+
process {
32+
$result = $false
33+
if ($IsWindows) {
34+
$key = 'HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\X64'
35+
if (Test-Path -Path $key) {
36+
$installedVersion = (Get-ItemProperty -Path $key).Version
37+
$result = [Version]($installedVersion.SubString(1, $installedVersion.Length - 1)) -ge $Version
38+
}
39+
}
40+
if (-not $result) {
41+
Write-Warning 'The Visual C++ Redistributable for Visual Studio 2015 or later is required.'
42+
Write-Warning 'Download and install the appropriate version from:'
43+
Write-Warning ' - https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads'
44+
}
45+
$result
46+
}
47+
}

src/functions/public/ConvertFrom-SodiumSealedBox.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242

4343
begin {
44+
if (-not $script:Supported) { throw 'Sodium is not supported on this platform.' }
4445
$null = [PSModule.Sodium]::sodium_init()
4546
}
4647

src/functions/public/ConvertTo-SodiumSealedBox.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
[string] $PublicKey
3131
)
3232
begin {
33+
if (-not $script:Supported) { throw 'Sodium is not supported on this platform.' }
3334
$null = [PSModule.Sodium]::sodium_init()
3435
}
3536

src/functions/public/New-SodiumKeyPair.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
)
5858

5959
begin {
60+
if (-not $script:Supported) { throw 'Sodium is not supported on this platform.' }
6061
$null = [PSModule.Sodium]::sodium_init()
6162
}
6263

src/main.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
switch ($true) {
22
$IsLinux {
33
Import-Module "$PSScriptRoot/libs/linux-x64/PSModule.Sodium.dll"
4+
$script:Supported = $true
45
}
56
$IsMacOS {
67
if ("$(sysctl -n machdep.cpu.brand_string)" -Like 'Apple*') {
78
Import-Module "$PSScriptRoot/libs/osx-arm64/PSModule.Sodium.dll"
89
} else {
910
Import-Module "$PSScriptRoot/libs/osx-x64/PSModule.Sodium.dll"
1011
}
12+
$script:Supported = $true
1113
}
1214
$IsWindows {
1315
if ([System.Environment]::Is64BitProcess) {
1416
Import-Module "$PSScriptRoot/libs/win-x64/PSModule.Sodium.dll"
1517
} else {
1618
Import-Module "$PSScriptRoot/libs/win-x86/PSModule.Sodium.dll"
1719
}
20+
$script:Supported = Assert-VisualCRedistributableInstalled -Version '14.0'
1821
}
1922
default {
2023
throw 'Unsupported platform. Please refer to the documentation for more information.'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$script:Supported = $false

0 commit comments

Comments
 (0)