-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mode.go
113 lines (100 loc) · 3.64 KB
/
mode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-FileCopyrightText: 2021-2024 Winni Neessen <[email protected]>
//
// SPDX-License-Identifier: MIT
package apg
import (
"strings"
)
// Mode represents a mode of characters
type Mode uint8
// ModeMask represents a bitmask of character modes
type ModeMask uint8
const (
// ModeNumeric sets the bitmask to include numeric in the generated passwords
ModeNumeric = 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"
// CharRangeNumeric represents all numerical characters
CharRangeNumeric = "1234567890"
// CharRangeNumericHuman represents all human-readable numerical characters
CharRangeNumericHuman = "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(mask ModeMask, mode Mode) ModeMask { return ModeMask(uint8(mask) | uint8(mode)) }
// MaskClearMode clears a specific Mode from a given Mode bitmask
func MaskClearMode(mask ModeMask, mode Mode) ModeMask { return ModeMask(uint8(mask) &^ uint8(mode)) }
// MaskToggleMode toggles a specific Mode in a given Mode bitmask
func MaskToggleMode(mask ModeMask, mode Mode) ModeMask { return ModeMask(uint8(mask) ^ uint8(mode)) }
// MaskHasMode returns true if a given Mode bitmask holds a specific Mode
func MaskHasMode(mask ModeMask, mode Mode) bool { return uint8(mask)&uint8(mode) != 0 }
func ModesFromFlags(maskString string) ModeMask {
cl := strings.Split(maskString, "")
var modeMask ModeMask
for _, m := range cl {
switch m {
case "C":
modeMask = MaskSetMode(modeMask, ModeLowerCase|ModeNumeric|ModeSpecial|ModeUpperCase)
case "h":
modeMask = MaskClearMode(modeMask, ModeHumanReadable)
case "H":
modeMask = MaskSetMode(modeMask, ModeHumanReadable)
case "l":
modeMask = MaskClearMode(modeMask, ModeLowerCase)
case "L":
modeMask = MaskSetMode(modeMask, ModeLowerCase)
case "n":
modeMask = MaskClearMode(modeMask, ModeNumeric)
case "N":
modeMask = MaskSetMode(modeMask, ModeNumeric)
case "s":
modeMask = MaskClearMode(modeMask, ModeSpecial)
case "S":
modeMask = MaskSetMode(modeMask, ModeSpecial)
case "u":
modeMask = MaskClearMode(modeMask, ModeUpperCase)
case "U":
modeMask = MaskSetMode(modeMask, ModeUpperCase)
}
}
return modeMask
}
// 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 ModeNumeric:
return "Numeric"
case ModeSpecial:
return "Special"
case ModeUpperCase:
return "Upper-case"
default:
return "Unknown"
}
}