diff --git a/src/ChallengeFile/Get-ChallengeFile.ps1 b/src/ChallengeFile/Get-ChallengeFile.ps1 new file mode 100644 index 0000000..e4062f0 --- /dev/null +++ b/src/ChallengeFile/Get-ChallengeFile.ps1 @@ -0,0 +1,44 @@ +function Get-ChallengeFile { + <# + .SYNOPSIS + Reads the challenge file as binary content. + + .DESCRIPTION + Use this function to tread a challenge file. Returns a [Byte[]] Array. + + .PARAMETER Path + Specify a file to read. + + .INPUTS + [None] + + .OUTPUTS + [Byte[]] + + .EXAMPLE + .\Get-RandomKey -Path "C:\TMP\Challenge.bin" + + .NOTES + File Name : Get-ChallengeFile.ps1 + Author : Marco Blessing - marco.blessing@googlemail.com + Requires : + + .LINK + https://github.com/OCram85/PSCredentialStore + #> + + [CmdletBinding()] + param( + [Parameter(Mandatory = $false)] + [string]$Path = "{0}\PSCredentialStore\Challenge.bin" -f $env:ProgramData + ) + + if (Test-Path $Path) { + try { + [io.file]::ReadAllBytes($Path) + } + catch { + Write-Error ("Could not read file {0}." -f $Path) -ErrorAction Stop + } + } +} diff --git a/src/ChallengeFile/Set-ChallengeFile.ps1 b/src/ChallengeFile/Set-ChallengeFile.ps1 new file mode 100644 index 0000000..7024daa --- /dev/null +++ b/src/ChallengeFile/Set-ChallengeFile.ps1 @@ -0,0 +1,72 @@ +Function Set-ChallengeFile() { + <# + .SYNOPSIS + Writes the given key into the challenge file + + .DESCRIPTION + You can use the file content for ConvertTo-SecureString operations. + + .PARAMETER Path + The file you wish to create. + + .PARAMETER KeySize + Specify the key size for the encryption key. + + .PARAMETER Force + Use this switch to override an older file version. + + .INPUTS + [None] + + .OUTPUTS + [None] + + .EXAMPLE + .\Set-ChallengeFile -Path "C:\TMP\myfile.json" -Force + + .NOTES + File Name : Set-ChallengeFile.ps1 + Author : Marco Blessing - marco.blessing@googlemail.com + Requires : + + .LINK + https://github.com/OCram85/PSCredentialStore + #> + + [CmdletBinding()] + param( + [Parameter(Mandatory = $false)] + [string]$Path = "{0}\PSCredentialStore\Challenge.bin" -f $env:ProgramData, + + [Parameter(Mandatory = $false)] + [ValidateSet(16, 24, 32)] + [string]$KeySize = "24", + + [switch]$Force + ) + + if ((Test-Path -Path $Path)) { + if ($Force -eq $true) { + Remove-Item -Path $Path -Confirm:$false -Force + } + else { + Write-Error "The given file already exists!. Use the -Force switch to override it." -ErrorAction Stop + } + } + $PSCredentialStoreDataDir = Split-Path -Path $Path -Parent + if (-not (Test-Path $PSCredentialStoreDataDir)) { + try { + New-Item -ItemType Directory -Path $PSCredentialStoreDataDir + } + catch { + Write-Error ("Could not create the parent data dir {0}" -f $PSCredentialDataDir) -ErrorAction Stop + } + } + try { + $Keys = Get-RandomKey -Size $KeySize + [io.file]::WriteAllBytes($Path, $Keys) + } + catch { + $_.Exception | Format-List -Force | Out-String | Write-ErrorErrorAction Stop + } +} diff --git a/src/Helper/Get-RandomKey.ps1 b/src/Helper/Get-RandomKey.ps1 new file mode 100644 index 0000000..b1ea7e3 --- /dev/null +++ b/src/Helper/Get-RandomKey.ps1 @@ -0,0 +1,47 @@ +function Get-RandomKey { + <# + .SYNOPSIS + Returns a random key + + .DESCRIPTION + You can use the key for further use with SecureStrings. + + .PARAMETER Size + Define the key size. You can choose between 16, 24 and 32 + + .INPUTS + [None] + + .OUTPUTS + Returns a Random key as [Byte[]] array. + + .EXAMPLE + .\Get-RandomKey -Size 24 + + .NOTES + File Name : Get-RandomKey.ps1 + Author : Marco Blessing - marco.blessing@googlemail.com + Requires : + + .LINK + https://github.com/OCram85/PSCredentialStore + #> + + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [ValidateSet(16, 24, 32)] + [string]$size + ) + # Init the vars + [Byte[]]$Key = @() + $i = 0 + + while ($i -ne $size) { + $element = Get-Random -Minimum 0 -Maximum 255 + Write-Debug ("The current element is {0}." -f $element) + $Key += $element + $i++ + } + $Key +}