Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
lagebj committed Nov 15, 2019
2 parents 5ca769d + f5b51f8 commit c9bc744
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 59 deletions.
34 changes: 14 additions & 20 deletions classes/Passphrase.Class.ps1
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
class Passphrase : Zxcvbn.Result {
[ValidateCount(
0,
3065)]
[System.Collections.Generic.List[string]]$Words = @()
class PassphraseObject : Zxcvbn.Result {
[ValidateCount(0, 3065)]
[System.Collections.Generic.List[string]] $Words = @()

[ValidateRange(
0,
9)]
[System.Collections.Generic.List[int]]$Numbers = @()
[ValidateRange(0, 9)]
[System.Collections.Generic.List[int]] $Numbers = @()

[ValidatePattern(
'[!"#$%&()*+,./:;<=>?@\^_{|}]')]
[System.Collections.Generic.List[char]]$Specials = @()
[ValidatePattern('[!"#$%&()*+,./:;<=>?@\^_{|}]')]
[System.Collections.Generic.List[char]] $Specials = @()

[ValidateNotNullOrEmpty()]
[char]$Separator
[char] $Separator

[bool]$IncludeUppercase = $false
[bool] $IncludeUppercase = $false

[int]$Length
[int] $Length

Passphrase(
[string[]]$Words) {
PassphraseObject([string[]] $Words) {
$Words | Get-Random -Count 3 | ForEach-Object {
$this.Words.Add($_)
}
Expand All @@ -44,7 +38,7 @@
$this.Suggestions = $Zxcvbn.suggestions
}

Passphrase([string] $Passphrase, [char] $Separator) {
PassphraseObject([string] $Passphrase, [char] $Separator) {
[string[]] $WordsArray = $Passphrase.Split($Separator)

[string[]] $WordsWithNumbers = $WordsArray -match '\d'
Expand Down Expand Up @@ -99,7 +93,7 @@
$this.Suggestions = $Zxcvbn.suggestions
}

Passphrase([string[]] $Words, [int] $AmountOfWords, [char] $Separator) {
PassphraseObject([string[]] $Words, [int] $AmountOfWords, [char] $Separator) {
$Words | Get-Random -Count $AmountOfWords | ForEach-Object {
$this.Words.Add($_)
}
Expand All @@ -122,7 +116,7 @@
$this.Suggestions = $Zxcvbn.suggestions
}

Passphrase([string[]] $Words, [int] $AmountOfWords, [char] $Separator, [int] $AmountOfNumbers, [int] $AmountOfSpecials, [bool] $IncludeUppercase) {
PassphraseObject([string[]] $Words, [int] $AmountOfWords, [char] $Separator, [int] $AmountOfNumbers, [int] $AmountOfSpecials, [bool] $IncludeUppercase) {
$Words | Get-Random -Count $AmountOfWords | ForEach-Object {
$this.Words.Add($_)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/howto.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,4 @@ System.Int32, System.Char, System.String

### Outputs

System.String, System.Management.Automation.PSObject
System.String, PassphraseObject
78 changes: 40 additions & 38 deletions scripts/public/New-Passphrase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,111 @@
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = 'Low',
DefaultParameterSetName = 'New')]
DefaultParameterSetName = 'New'
)]

[OutputType(
[string],
[psobject])]
[PassphraseObject]
)]

Param (
[Parameter(
ParameterSetName = 'New',
Mandatory = $false,
Position = 0,
HelpMessage = 'Amount of words to get')]
HelpMessage = 'Amount of words to get'
)]
[ValidateNotNullOrEmpty()]
[int]
$AmountOfWords = 3,
[int] $AmountOfWords = 3,

[Parameter(
Mandatory = $false,
Position = 3,
HelpMessage = 'Separator to use between words')]
HelpMessage = 'Separator to use between words'
)]
[ValidateNotNullOrEmpty()]
[char]
$Separator = " ",
[char] $Separator = ' ',

[Parameter(
Mandatory = $false,
HelpMessage = 'Includes numbers')]
[switch]
$IncludeNumbers,
HelpMessage = 'Includes numbers'
)]
[switch] $IncludeNumbers,

[Parameter(
Mandatory = $false,
Position = 1,
HelpMessage = 'Amount of numbers to include')]
HelpMessage = 'Amount of numbers to include'
)]
[ValidateNotNullOrEmpty()]
[int]
$AmountOfNumbers = 1,
[int] $AmountOfNumbers = 1,

[Parameter(
Mandatory = $false,
HelpMessage = 'Include an uppercase word')]
[switch]
$IncludeUppercase,
HelpMessage = 'Include an uppercase word'
)]
[switch] $IncludeUppercase,

[Parameter(
Mandatory = $false,
HelpMessage = 'Include special characters')]
[switch]
$IncludeSpecials,
HelpMessage = 'Include special characters'
)]
[switch] $IncludeSpecials,

[Parameter(
Mandatory = $false,
Position = 2,
HelpMessage = 'Amount of special characters to include')]
[int]
$AmountOfSpecials = 1,
HelpMessage = 'Amount of special characters to include'
)]
[int] $AmountOfSpecials = 1,

[Parameter(
Mandatory = $false,
HelpMessage = 'Return passphrase as object')]
[switch]
$AsObject,
HelpMessage = 'Return passphrase as object'
)]
[switch] $AsObject,

[Parameter(
ParameterSetName = 'Custom',
Mandatory = $true,
ValueFromPipeline = $true,
HelpMessage = 'Custom string to build passphrase object from')]
[string]
$CustomString
HelpMessage = 'Custom string to build passphrase object from'
)]
[string] $CustomString
)
$ErrorActionPreference = 'Stop'
$InformationPreference = 'Continue'
if (-not $PSBoundParameters.ContainsKey('Verbose')) { $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference') }
if (-not $PSBoundParameters.ContainsKey('Confirm')) { $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference') }
if (-not $PSBoundParameters.ContainsKey('WhatIf')) { $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference') }
Write-Verbose ('[{0}] Confirm={1} ConfirmPreference={2} WhatIf={3} WhatIfPreference={4}' -f $MyInvocation.MyCommand, $Confirm, $ConfirmPreference, $WhatIf, $WhatIfPreference)
if ($PSCmdlet.ShouldProcess("Generates a new random passphrase")) {
if ($PSCmdlet.ShouldProcess("Generates a new random passphrase as a string or object")) {
try {
if ($PSBoundParameters.ContainsKey('CustomString')) {
$Passphrase = [Passphrase]::new($CustomString, $Separator)
$PassphraseObject = [PassphraseObject]::new($CustomString, $Separator)
} else {
[array]$Words = . (Join-Path (Split-Path $PSScriptroot) 'private\Passphraser.Words.ps1')
[array] $Words = . (Join-Path (Split-Path $PSScriptroot) 'private\Passphraser.Words.ps1')

$Passphrase = [Passphrase]::new($Words, $AmountOfWords, $Separator)
$PassphraseObject = [PassphraseObject]::new($Words, $AmountOfWords, $Separator)
}

if ($PSBoundParameters.ContainsKey('IncludeUppercase')) {
$Passphrase.AddUppercase()
$PassphraseObject.AddUppercase()
}

if ($PSBoundParameters.ContainsKey('IncludeNumbers')) {
$Passphrase.AddNumber($AmountOfNumbers)
$PassphraseObject.AddNumber($AmountOfNumbers)
}

if ($PSBoundParameters.ContainsKey('IncludeSpecials')) {
$Passphrase.AddSpecial($AmountOfSpecials)
$PassphraseObject.AddSpecial($AmountOfSpecials)
}

if ($PSBoundParameters.ContainsKey('AsObject')) {
return $Passphrase
return $PassphraseObject
} else {
return $Passphrase.ToString()
return $PassphraseObject.ToString()
}
} catch {
$PSCmdlet.throwTerminatingError($PSItem)
Expand Down

0 comments on commit c9bc744

Please sign in to comment.