-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_FactoryMethod.ps1
More file actions
89 lines (79 loc) · 3.12 KB
/
05_FactoryMethod.ps1
File metadata and controls
89 lines (79 loc) · 3.12 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<#
.SYNOPSIS
OOP Reference: Factory Method Pattern
.DESCRIPTION
Topic: Factory Method — decouple creation from usage
Category: Creational
Agent Task: Add a 'ChaCha20' branch to CryptoAlgorithmFactory.Create() that
throws NotSupportedException with a clear message. Add Pester tests
covering: correct type returned, unknown algo throws ArgumentException,
keyed overload sets key correctly.
Done Conditions:
- Factory returns correct concrete type for each supported algo string
- Unknown algo throws System.ArgumentException
- Pester tests pass: Invoke-Pester -Output Detailed
Non-Scope:
- Do not add runtime plugin loading
#>
class CryptoAlgorithm {
[string]$Name
CryptoAlgorithm([string]$name) { $this.Name = $name }
[byte[]] Encrypt([byte[]]$data) {
throw [System.NotImplementedException]'Encrypt'
}
}
class AesCryptoAlgorithm : CryptoAlgorithm {
hidden [System.Security.Cryptography.AesGcm]$_gcm
hidden [byte[]]$_key
AesCryptoAlgorithm([byte[]]$key) : base('AES-256-GCM') {
$this._key = $key
$this._gcm = [System.Security.Cryptography.AesGcm]::new($key)
}
[byte[]] Encrypt([byte[]]$data) {
$nonce = [byte[]]::new(12)
$ct = [byte[]]::new($data.Length)
$tag = [byte[]]::new(16)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($nonce)
$this._gcm.Encrypt($nonce, $data, $ct, $tag)
return $nonce + $tag + $ct
}
[byte[]] GetKey() { return $this._key }
}
class CryptoAlgorithmFactory {
static [CryptoAlgorithm] Create([string]$algorithmName) {
$upper = $algorithmName.ToUpper()
if ($upper -eq 'AES-256-GCM') {
$key = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($key)
return [AesCryptoAlgorithm]::new($key)
}
elseif ($upper -eq 'CHACHA20') {
throw [System.NotSupportedException]"ChaCha20 is not yet implemented in this library"
}
else {
throw [System.ArgumentException]"Unknown algorithm: $algorithmName"
}
}
static [CryptoAlgorithm] Create([string]$algorithmName, [byte[]]$key) {
$upper = $algorithmName.ToUpper()
if ($upper -eq 'AES-256-GCM') {
return [AesCryptoAlgorithm]::new($key)
}
elseif ($upper -eq 'CHACHA20') {
throw [System.NotSupportedException]"ChaCha20 is not yet implemented in this library"
}
else {
throw [System.ArgumentException]"Key overload not supported for: $algorithmName"
}
}
}
#region Agent Task Implementation
<#
Agent Task: Add 'ChaCha20' branch to CryptoAlgorithmFactory.Create()
Implementation Notes:
- Added 'CHACHA20' case to both Create() overloads
- Throws NotSupportedException with clear message indicating it's not yet implemented
- Case-insensitive matching via ToUpper() ensures 'ChaCha20', 'chacha20', 'CHACHA20' all work
- Unknown algorithms still throw ArgumentException as per existing behavior
#>
#endregion