-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInitialize-Sodium.ps1
More file actions
50 lines (43 loc) · 1.87 KB
/
Copy pathInitialize-Sodium.ps1
File metadata and controls
50 lines (43 loc) · 1.87 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
function Initialize-Sodium {
<#
.SYNOPSIS
Initializes Sodium for cryptographic operations.
.DESCRIPTION
Initializes the native Sodium library once per module session and caches fixed buffer sizes used by the public commands.
.NOTES
Requires the platform-specific PSModule.Sodium assembly and native libsodium runtime to be loaded.
#>
[OutputType([void])]
[CmdletBinding()]
param()
begin {}
process {
if (-not $script:Supported) { throw 'Sodium is not supported on this platform.' }
if ($script:SodiumInitialized) { return }
try {
$initializationResult = [PSModule.Sodium]::sodium_init()
} catch {
$script:Supported = $false
if ($IsWindows) {
if ($script:ProcessArchitecture -in @('X64', 'X86')) {
$hasRuntime = Assert-VisualCRedistributableInstalled -Version '14.0' -Architecture $script:ProcessArchitecture
if (-not $hasRuntime) {
$message = "Sodium native initialization failed; the Visual C++ Redistributable for " +
"$($script:ProcessArchitecture) appears to be missing or below the required version."
throw $message
}
}
}
throw
}
if ($initializationResult -lt 0) {
throw 'Sodium initialization failed.'
}
$script:SodiumPublicKeyBytes = [PSModule.Sodium]::crypto_box_publickeybytes().ToUInt32()
$script:SodiumPrivateKeyBytes = [PSModule.Sodium]::crypto_box_secretkeybytes().ToUInt32()
$script:SodiumSealBytes = [PSModule.Sodium]::crypto_box_sealbytes().ToUInt32()
$script:SodiumSeedBytes = [PSModule.Sodium]::crypto_box_seedbytes().ToUInt32()
$script:SodiumInitialized = $true
}
end {}
}