Skip to content

Commit 16c0bea

Browse files
committed
#3 | Add AES key length validation
1 parent ebd97ef commit 16c0bea

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

Crypto.AES/Public/Protect-Data.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ function Protect-Data {
3131
$tag = [byte[]]::new(16)
3232

3333
if ($PSCmdlet.ParameterSetName -eq 'Key') {
34+
if ($Key.Length -notin @(16, 24, 32)) {
35+
throw "Invalid AES key length. Must be 16, 24, or 32 bytes."
36+
}
3437
$GCM = [System.Security.Cryptography.AesGcm]::new($Key)
3538
try {
3639
$GCM.Encrypt($Nonce, $Data, $cipherOutput, $tag)

Tests/Crypto.AES.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ Describe 'Crypto.AES.Tests' {
1717
}
1818
}
1919

20+
Context "Protect-Data - parameter validation" {
21+
It "Throws error for invalid key length" {
22+
{ Protect-Data -Key ([byte[]]::new(10)) -Data ([byte[]]::new(16)) } | Should -Throw "Invalid AES key length. Must be 16, 24, or 32 bytes."
23+
}
24+
}
25+
2026
Context "Protect-Data - Result" {
2127
$Key = [byte[]]::new(32)
2228
$nonce = [byte[]]::new(12)

0 commit comments

Comments
 (0)