File tree 3 files changed +30
-3
lines changed
3 files changed +30
-3
lines changed Original file line number Diff line number Diff line change
1
+ function Get-RandomNonce {
2
+ [CmdletBinding ()]
3
+ param (
4
+ [Parameter (Mandatory = $true )]
5
+ [ValidateRange (1 , [int ]::MaxValue)]
6
+ [int ]$Length
7
+ )
8
+
9
+ begin {
10
+ Write-Verbose " Cmdlet Get-RandomNonce - Begin"
11
+ }
12
+
13
+ process {
14
+ Write-Verbose " Cmdlet Get-RandomNonce - Generating a nonce of length $Length "
15
+
16
+ $nonce = [byte []]::new($Length )
17
+ [System.Security.Cryptography.RandomNumberGenerator ]::Fill($nonce )
18
+ Write-Output $nonce - NoEnumerate
19
+ }
20
+
21
+ end {
22
+ Write-Verbose " Cmdlet Get-RandomNonce - End"
23
+ }
24
+ }
Original file line number Diff line number Diff line change @@ -24,8 +24,7 @@ function Protect-Data {
24
24
process {
25
25
Write-Verbose " Cmdlet Protect-Data - Process"
26
26
if (! $Nonce ) {
27
- $Nonce = [byte []]::new(12 )
28
- [System.Security.Cryptography.RandomNumberGenerator ]::Fill($Nonce )
27
+ $Nonce = Get-RandomNonce - Length 12
29
28
}
30
29
$cipherOutput = [byte []]::new($Data.Length )
31
30
$tag = [byte []]::new(16 )
Original file line number Diff line number Diff line change @@ -58,10 +58,14 @@ Describe 'Crypto.AES.Tests' {
58
58
}
59
59
60
60
Context " Protect-Data - signature" {
61
- $Key = [byte []]::new(32 )
61
+ $Key = [byte []]::new(32 )
62
62
$nonce = [byte []]::new(12 )
63
+ $nonce [0 ] = 104 # random value to test with mock
63
64
$data = $encoding.GetBytes (" Test" )
64
65
66
+ Mock - CommandName Get-RandomNonce - ModuleName Crypto.AES - MockWith {
67
+ Write-Output $nonce - NoEnumerate
68
+ }
65
69
66
70
It " optional nonce" {
67
71
$r_explicit = Protect-Data - Key $Key - Data $data - Nonce $nonce
You can’t perform that action at this time.
0 commit comments