-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_StaticConstructors.ps1
More file actions
59 lines (51 loc) · 2.37 KB
/
20_StaticConstructors.ps1
File metadata and controls
59 lines (51 loc) · 2.37 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
<#
.SYNOPSIS
OOP Reference: Static Constructors / Type Initializers
.DESCRIPTION
Topic: One-time type-level initialization in PS classes
Category: PS-Specific
Agent Task: Add a static Refresh() method that re-runs initialization
(useful when new algorithms are registered at runtime).
Add a static GetKeySize([string]$algo) that throws on unknown.
Add Pester tests verifying static ctor fires exactly once.
Done Conditions:
- Static ctor does not fire twice on multiple accesses
- GetKeySize throws ArgumentException on unknown algorithm
- Pester tests pass: Invoke-Pester -Output Detailed
Non-Scope:
- No thread-safety needed for static ctor (PS is single-threaded per runspace)
#>
class AlgorithmCatalog {
static [System.Collections.Generic.HashSet[string]]$Supported
static [System.Collections.Generic.Dictionary[string,int]]$KeySizes
static [string]$DefaultAlgorithm
hidden static [int]$_initCount = 0
static AlgorithmCatalog() {
[AlgorithmCatalog]::_Initialize()
}
hidden static [void] _Initialize() {
[AlgorithmCatalog]::_initCount++
[AlgorithmCatalog]::Supported = [System.Collections.Generic.HashSet[string]]::new(
[string[]]@('AES-128-GCM','AES-256-GCM','ChaCha20-Poly1305','AES-256-CBC'),
[System.StringComparer]::OrdinalIgnoreCase)
[AlgorithmCatalog]::KeySizes = [System.Collections.Generic.Dictionary[string,int]]::new(
[System.StringComparer]::OrdinalIgnoreCase)
[AlgorithmCatalog]::KeySizes['AES-128-GCM'] = 128
[AlgorithmCatalog]::KeySizes['AES-256-GCM'] = 256
[AlgorithmCatalog]::KeySizes['ChaCha20-Poly1305'] = 256
[AlgorithmCatalog]::KeySizes['AES-256-CBC'] = 256
[AlgorithmCatalog]::DefaultAlgorithm = 'AES-256-GCM'
}
static [void] Refresh() { [AlgorithmCatalog]::_Initialize() }
static [bool] IsSupported([string]$algo) {
return [AlgorithmCatalog]::Supported.Contains($algo)
}
static [int] GetKeySize([string]$algo) {
$size = 0
if (-not [AlgorithmCatalog]::KeySizes.TryGetValue($algo, [ref]$size)) {
throw [System.ArgumentException]"Unknown algorithm: $algo"
}
return $size
}
static [int] GetInitCount() { return [AlgorithmCatalog]::_initCount }
}