Skip to content

Commit 79f0462

Browse files
committed
Initial commit
0 parents  commit 79f0462

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed

Crypto.AES/Crypto.AES.psd1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
@{
3+
RootModule = 'Crypto.AES.psm1'
4+
ModuleVersion = '1.0.0'
5+
GUID = '11dfc05f-b1e6-4ff9-87fc-7ff4dca7457e'
6+
Author = 'Alan Plocieniak'
7+
CompanyName = 'Alan Plocieniak'
8+
Copyright = '(c) 2022 Alan Plocieniak. All rights reserved.'
9+
Description = 'PowerShell module for cryptography (AES)'
10+
PowerShellVersion = '5.0'
11+
FunctionsToExport = '*'
12+
ScriptsToProcess = @('Public\Classes\IVGenerator.ps1')
13+
PrivateData = @{
14+
PSData = @{
15+
}
16+
}
17+
}

Crypto.AES/Crypto.AES.psm1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Get public and private function definition files.
2+
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
3+
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
4+
5+
#Dot source the files
6+
Foreach ($import in @($Public + $Private)) {
7+
try {
8+
. $import.fullname
9+
}
10+
catch {
11+
Write-Error -Message "Failed to import function $($import.fullname): $_"
12+
}
13+
}
14+
Export-ModuleMember -Function $Public.Basename
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class IVGenerator {
2+
[System.Security.Cryptography.AesManaged]$AesManaged
3+
IVGenerator() {
4+
$this.AesManaged = New-AesManagedObject
5+
}
6+
7+
[byte[]] GenerateIV () {
8+
$this.AesManaged.GenerateIV()
9+
return $this.AesManaged.IV
10+
}
11+
}

Crypto.AES/Public/New-AesKey.ps1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
enum AesKeyFormat {
2+
String
3+
ByteArray
4+
}
5+
6+
function New-AesKey {
7+
[CmdletBinding()]
8+
param (
9+
[Parameter(Mandatory = $false, Position = 0 )]
10+
[AesKeyFormat]$Format = [AesKeyFormat]::String
11+
)
12+
13+
begin {
14+
Write-Verbose "Cmdlet New-AesKey - Begin"
15+
}
16+
17+
process {
18+
Write-Verbose "Cmdlet New-AesKey - Process"
19+
$aes = New-AesManagedObject
20+
$aes.GenerateKey()
21+
22+
23+
if ($Format -eq [AesKeyFormat]::String) {
24+
[System.Convert]::ToBase64String($aes.Key)
25+
}
26+
elseif ($Format -eq [AesKeyFormat]::ByteArray) {
27+
$aes.Key
28+
}
29+
}
30+
31+
end {
32+
Write-Verbose "Cmdlet New-AesKey - End"
33+
}
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function New-AesManagedObject {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $false, Position = 0 )]
5+
$Key,
6+
[Parameter(Mandatory = $false, Position = 1 )]
7+
$InitializationVector,
8+
[Parameter(Mandatory = $false, Position = 2 )]
9+
[System.Security.Cryptography.CipherMode]$Mode = [System.Security.Cryptography.CipherMode]::CBC,
10+
[Parameter(Mandatory = $false, Position = 3 )]
11+
[System.Security.Cryptography.PaddingMode]$Padding = [System.Security.Cryptography.PaddingMode]::Zeros,
12+
[Parameter(Mandatory = $false, Position = 4 )]
13+
[int]$BlockSize = 128,
14+
[Parameter(Mandatory = $false, Position = 5 )]
15+
[int]$KeySize = 256
16+
)
17+
18+
begin {
19+
Write-Verbose "Cmdlet New-AesManagedObject - Begin"
20+
}
21+
22+
process {
23+
Write-Verbose "Cmdlet New-AesManagedObject - Process"
24+
$aesManaged = New-Object "System.Security.Cryptography.AesManaged"
25+
$aesManaged.Mode = $Mode
26+
$aesManaged.Padding = $Padding
27+
$aesManaged.BlockSize = $BlockSize
28+
$aesManaged.KeySize = $KeySize
29+
if ($InitializationVector) {
30+
if ($InitializationVector.getType().Name -eq "String") {
31+
$aesManaged.IV = [System.Convert]::FromBase64String($InitializationVector)
32+
}
33+
else {
34+
$aesManaged.IV = $InitializationVector
35+
}
36+
}
37+
if ($Key) {
38+
if ($Key.getType().Name -eq "String") {
39+
$aesManaged.Key = [System.Convert]::FromBase64String($Key)
40+
}
41+
else {
42+
$aesManaged.Key = $Key
43+
}
44+
}
45+
$aesManaged
46+
}
47+
48+
end {
49+
Write-Verbose "Cmdlet New-AesManagedObject - End"
50+
}
51+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Crypto.AES
2+
3+
This is a PowerShell module for cryptography (RSA).

Tests/Crypto.AES.Tests.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Clear-Host
2+
Import-Module -Name Pester -Force
3+
Import-Module .\Crypto.AES\Crypto.AES.psm1 -Force
4+
5+
Describe 'Crypto.AES.Tests' {
6+
Context "AES key generation" {
7+
It "Should return key as string - default" {
8+
New-AesKey | Should -BeOfType [String]
9+
}
10+
It "Should return key as string - explicit" {
11+
New-AesKey -Format String | Should -BeOfType [String]
12+
}
13+
It "Should return key as Object[]- explicit" {
14+
(New-AesKey -Format ByteArray) -is [System.Object[]] | Should -BeTrue
15+
}
16+
}
17+
18+
Context "IVGenerator" {
19+
$obj = [IVGenerator]::new()
20+
$v1 = $obj.GenerateIV()
21+
$v2 = $obj.GenerateIV()
22+
23+
It "Should return valid type" {
24+
$v1 -is [System.Byte[]] | Should -BeTrue
25+
}
26+
It "Should generate unique keys" {
27+
$v1 | Should -Not -Be $v2
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)