-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_UpdateTypeData.ps1
More file actions
88 lines (73 loc) · 3.27 KB
/
18_UpdateTypeData.ps1
File metadata and controls
88 lines (73 loc) · 3.27 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
<#
.SYNOPSIS
OOP Reference: Update-TypeData
.DESCRIPTION
Topic: Extend existing .NET types with ScriptMethod and ScriptProperty
Category: PS-Specific
Agent Task: Add a ToBase58() ScriptMethod on byte[] that encodes using a
Bitcoin-style base58 alphabet (no padding, no 0/O/l/I ambiguity).
Add a IsKeySize ScriptProperty that returns $true if Length is 16, 24, or 32.
Add Pester tests for all extended methods.
Done Conditions:
- All new methods are idempotent (can be called multiple times)
- -Force prevents duplicate-definition errors on reload
- Pester tests pass: Invoke-Pester -Output Detailed
Non-Scope:
- Do not modify types globally without the -Force flag
#>
# Extend byte[] with crypto convenience methods
Update-TypeData -TypeName 'System.Byte[]' -MemberType ScriptMethod -MemberName 'ToHex' -Value {
($this | ForEach-Object { $_.ToString('x2') }) -join ''
} -Force
Update-TypeData -TypeName 'System.Byte[]' -MemberType ScriptMethod -MemberName 'ToBase64' -Value {
[Convert]::ToBase64String($this)
} -Force
Update-TypeData -TypeName 'System.Byte[]' -MemberType ScriptMethod -MemberName 'SHA256Hash' -Value {
$h = [System.Security.Cryptography.SHA256]::Create()
try { return $h.ComputeHash($this) } finally { $h.Dispose() }
} -Force
Update-TypeData -TypeName 'System.Byte[]' -MemberType ScriptProperty -MemberName 'IsKeySize' -Value {
$this.Length -in @(16, 24, 32)
} -Force
Update-TypeData -TypeName 'System.Byte[]' -MemberType ScriptMethod -MemberName 'ToBase58' -Value {
# Bitcoin-style base58 alphabet (no 0, O, I, l)
$alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
if ($this.Length -eq 0) { return '' }
# Count leading zeros
$leadingZeros = 0
for ($i = 0; $i -lt $this.Length -and $this[$i] -eq 0; $i++) { $leadingZeros++ }
# Convert bytes to big integer (need to reverse for little-endian, make copy first)
$copy = [byte[]]::new($this.Length)
[Array]::Copy($this, $copy, $this.Length)
[Array]::Reverse($copy)
# Add 0x00 byte to ensure positive number
$bytes = $copy + [byte]0x00
$num = [System.Numerics.BigInteger]::new($bytes)
$encoded = ''
while ($num -gt 0) {
$remainder = [int]($num % 58)
$num = $num / 58
$encoded = $alphabet[$remainder] + $encoded
}
# Add '1' for each leading zero byte
$encoded = ('1' * $leadingZeros) + $encoded
return $encoded
} -Force
Update-TypeData -TypeName 'System.String' -MemberType ScriptMethod -MemberName 'ToUTF8Bytes' -Value {
[System.Text.Encoding]::UTF8.GetBytes($this)
} -Force
Update-TypeData -TypeName 'System.String' -MemberType ScriptMethod -MemberName 'SHA256Hex' -Value {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($this)
$h = [System.Security.Cryptography.SHA256]::Create()
try { return ($h.ComputeHash($bytes) | ForEach-Object { $_.ToString('x2') }) -join '' }
finally { $h.Dispose() }
} -Force
# Usage after loading:
# $key = [byte[]]::new(32)
# [System.Security.Cryptography.RandomNumberGenerator]::Fill($key)
# $key.ToHex()
# $key.ToBase64()
# $key.ToBase58()
# $key.SHA256Hash().ToHex()
# $key.IsKeySize # -> $true
# "hello world".SHA256Hex()