mirror of
https://github.com/wneessen/apg-go.git
synced 2024-11-22 05:40:51 +01:00
Winni Neessen
704269d0b8
This commit primarily reorders the modes in the "mode_test.go" file for consistency. The order is now; ModeHumanReadable, ModeLowerCase, ModeNumber, ModeSpecial, ModeUpperCase. This now follows a logical order instead of the previous semi arbitrary one. The commit also involves the addition of a new test case 'TestModesFromFlags' in the "mode_test.go" file. This test case aims at increasing the code coverage and ensuring modes obtained from the flags are correct. Lastly, the 'ModesFromFlags' function in the "mode.go" has been slightly refactored for improved readability. This does not change the functionality and thus won't affect the rest of the codebase.
110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
package apg
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// Mode represents a mode of characters
|
|
type Mode uint8
|
|
|
|
// ModeMask represents a bitmask of character modes
|
|
type ModeMask uint8
|
|
|
|
const (
|
|
// ModeNumber sets the bitmask to include numbers in the generated passwords
|
|
ModeNumber = 1 << iota
|
|
// ModeLowerCase sets the bitmask to include lower case characters in the
|
|
// generated passwords
|
|
ModeLowerCase
|
|
// ModeUpperCase sets the bitmask to include upper case characters in the
|
|
// generated passwords
|
|
ModeUpperCase
|
|
// ModeSpecial sets the bitmask to include special characters in the
|
|
// generated passwords
|
|
ModeSpecial
|
|
// ModeHumanReadable sets the bitmask to generate human readable passwords
|
|
ModeHumanReadable
|
|
)
|
|
|
|
const (
|
|
// CharRangeAlphaLower represents all lower-case alphabetical characters
|
|
CharRangeAlphaLower = "abcdefghijklmnopqrstuvwxyz"
|
|
// CharRangeAlphaLowerHuman represents the human-readable lower-case alphabetical characters
|
|
CharRangeAlphaLowerHuman = "abcdefghjkmnpqrstuvwxyz"
|
|
// CharRangeAlphaUpper represents all upper-case alphabetical characters
|
|
CharRangeAlphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
// CharRangeAlphaUpperHuman represents the human-readable upper-case alphabetical characters
|
|
CharRangeAlphaUpperHuman = "ABCDEFGHJKMNPQRSTUVWXYZ"
|
|
// CharRangeNumber represents all numerical characters
|
|
CharRangeNumber = "1234567890"
|
|
// CharRangeNumberHuman represents all human-readable numerical characters
|
|
CharRangeNumberHuman = "23456789"
|
|
// CharRangeSpecial represents all special characters
|
|
CharRangeSpecial = `!\"#$%&'()*+,-./:;<=>?@[\\]^_{|}~`
|
|
// CharRangeSpecialHuman represents all human-readable special characters
|
|
CharRangeSpecialHuman = `#%*+-:;=`
|
|
)
|
|
|
|
// MaskSetMode sets a specific Mode to a given Mode bitmask
|
|
func MaskSetMode(ma ModeMask, mo Mode) ModeMask { return ModeMask(uint8(ma) | uint8(mo)) }
|
|
|
|
// MaskClearMode clears a specific Mode from a given Mode bitmask
|
|
func MaskClearMode(ma ModeMask, mo Mode) ModeMask { return ModeMask(uint8(ma) &^ uint8(mo)) }
|
|
|
|
// MaskToggleMode toggles a specific Mode in a given Mode bitmask
|
|
func MaskToggleMode(ma ModeMask, mo Mode) ModeMask { return ModeMask(uint8(ma) ^ uint8(mo)) }
|
|
|
|
// MaskHasMode returns true if a given Mode bitmask holds a specific Mode
|
|
func MaskHasMode(ma ModeMask, mo Mode) bool { return uint8(ma)&uint8(mo) != 0 }
|
|
|
|
func ModesFromFlags(ms string) ModeMask {
|
|
cl := strings.Split(ms, "")
|
|
var mm ModeMask
|
|
for _, m := range cl {
|
|
switch m {
|
|
case "C":
|
|
mm = MaskSetMode(mm, ModeLowerCase|ModeNumber|ModeSpecial|ModeUpperCase)
|
|
case "h":
|
|
mm = MaskClearMode(mm, ModeHumanReadable)
|
|
case "H":
|
|
mm = MaskSetMode(mm, ModeHumanReadable)
|
|
case "l":
|
|
mm = MaskClearMode(mm, ModeLowerCase)
|
|
case "L":
|
|
mm = MaskSetMode(mm, ModeLowerCase)
|
|
case "n":
|
|
mm = MaskClearMode(mm, ModeNumber)
|
|
case "N":
|
|
mm = MaskSetMode(mm, ModeNumber)
|
|
case "s":
|
|
mm = MaskClearMode(mm, ModeSpecial)
|
|
case "S":
|
|
mm = MaskSetMode(mm, ModeSpecial)
|
|
case "u":
|
|
mm = MaskClearMode(mm, ModeUpperCase)
|
|
case "U":
|
|
mm = MaskSetMode(mm, ModeUpperCase)
|
|
}
|
|
}
|
|
|
|
return mm
|
|
|
|
}
|
|
|
|
// String satisfies the fmt.Stringer interface for the Mode type
|
|
func (m Mode) String() string {
|
|
switch m {
|
|
case ModeHumanReadable:
|
|
return "Human-readable"
|
|
case ModeLowerCase:
|
|
return "Lower-case"
|
|
case ModeNumber:
|
|
return "Number"
|
|
case ModeSpecial:
|
|
return "Special"
|
|
case ModeUpperCase:
|
|
return "Upper-case"
|
|
default:
|
|
return "Unknown"
|
|
}
|
|
}
|