-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_Prototype.ps1
More file actions
56 lines (51 loc) · 2.03 KB
/
09_Prototype.ps1
File metadata and controls
56 lines (51 loc) · 2.03 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
<#
.SYNOPSIS
OOP Reference: Prototype / Deep Clone Pattern
.DESCRIPTION
Topic: Clone objects without re-running expensive constructors
Category: Creational
Agent Task: Add a CloneWithNewKey() method that deep-clones the config and
generates fresh random KeyMaterial. Add Pester tests verifying
that mutations to the clone do not affect the original.
Done Conditions:
- Clone() produces independent copy (mutate clone, original unchanged)
- CloneWith() applies overrides correctly
- byte[] is deep-copied, not reference-copied
- Pester tests pass: Invoke-Pester -Output Detailed
Non-Scope:
- No serialization-based clone (BinaryFormatter is deprecated)
#>
class CloneableConfig {
[string]$Algorithm
[int]$KeyBits
[hashtable]$Parameters
[byte[]]$KeyMaterial
CloneableConfig([string]$algo, [int]$bits) {
$this.Algorithm = $algo
$this.KeyBits = $bits
$this.Parameters = @{}
$this.KeyMaterial = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($this.KeyMaterial)
}
[CloneableConfig] Clone() {
$copy = [CloneableConfig]::new($this.Algorithm, $this.KeyBits)
foreach ($k in $this.Parameters.Keys) {
$copy.Parameters[$k] = $this.Parameters[$k]
}
$copy.KeyMaterial = [byte[]]::new($this.KeyMaterial.Length)
[System.Buffer]::BlockCopy($this.KeyMaterial, 0, $copy.KeyMaterial, 0, $this.KeyMaterial.Length)
return $copy
}
[CloneableConfig] CloneWith([hashtable]$overrides) {
$copy = $this.Clone()
foreach ($k in $overrides.Keys) { $copy.$k = $overrides[$k] }
return $copy
}
[CloneableConfig] CloneWithNewKey() {
$copy = $this.Clone()
# Generate fresh random KeyMaterial
$copy.KeyMaterial = [byte[]]::new($this.KeyMaterial.Length)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($copy.KeyMaterial)
return $copy
}
}