Skip to content

Commit 05192e2

Browse files
committed
Improved efficiency when working with large files
1 parent 68c7f4e commit 05192e2

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

Crypto.AES/Public/Protect-Data.ps1

+12-5
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,21 @@ function Protect-Data {
3030
}
3131

3232
$gcm.Encrypt($Nonce, $Data, $cipherOutput, $tag)
33+
$gcm.Dispose()
3334

3435
if ($Combined) {
35-
return $tag + $cipherOutput + $Nonce
36+
$output = [byte[]]::new($cipherOutput.Length + $Nonce.Length + $tag.Length)
37+
[System.Buffer]::BlockCopy($tag, 0, $output, 0, $tag.Length);
38+
[System.Buffer]::BlockCopy($cipherOutput, 0, $output, $tag.Length, $cipherOutput.Length);
39+
[System.Buffer]::BlockCopy($Nonce, 0, $output, $tag.Length + $cipherOutput.Length, $Nonce.Length);
40+
Write-Output $output -NoEnumerate
3641
}
37-
@{
38-
CipherText = $cipherOutput
39-
Nonce = $Nonce
40-
Tag = $tag
42+
else {
43+
@{
44+
CipherText = $cipherOutput
45+
Nonce = $Nonce
46+
Tag = $tag
47+
}
4148
}
4249
}
4350

Crypto.AES/Public/Unprotect-Data.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Unprotect-Data {
2525
$gcm = [System.Security.Cryptography.AesGcm]::new($Key)
2626
}
2727
$gcm.Decrypt($nonce, $Data, $Tag, $decrypted)
28-
$decrypted
28+
Write-Output $decrypted -NoEnumerate
2929
}
3030

3131
end {

Tests/Crypto.AES.Tests.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Describe 'Crypto.AES.Tests' {
4646
$r.CipherText | Should -BeNullOrEmpty
4747
$r.Nonce | Should -BeNullOrEmpty
4848
$r.Tag | Should -BeNullOrEmpty
49-
$r -is [System.Object[]] | Should -BeTrue
49+
$r -is [byte[]] | Should -BeTrue
5050
$r.Length | Should -BeExactly ($data.Length + $nonce.Length + 16)
5151
}
5252
}

0 commit comments

Comments
 (0)