├── LICENSE
├── pwd-cli
├── pwd-cli.gif
├── Screen Shot 2022-06-28 at 00.50.08.png
├── Screen Shot 2022-07-03 at 13.08.54.png
├── main.go
├── go.mod
├── go.sum
├── README.md
└── cmd
└── root.go
/LICENSE:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/pwd-cli:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/borakasmer/pwd-cli/HEAD/pwd-cli
--------------------------------------------------------------------------------
/pwd-cli.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/borakasmer/pwd-cli/HEAD/pwd-cli.gif
--------------------------------------------------------------------------------
/Screen Shot 2022-06-28 at 00.50.08.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/borakasmer/pwd-cli/HEAD/Screen Shot 2022-06-28 at 00.50.08.png
--------------------------------------------------------------------------------
/Screen Shot 2022-07-03 at 13.08.54.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/borakasmer/pwd-cli/HEAD/Screen Shot 2022-07-03 at 13.08.54.png
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2022 NAME HERE bora@borakasmer.com
3 |
4 | */
5 | package main
6 |
7 | import "github.com/borakasmer/pwd-cli/cmd"
8 |
9 | func main() {
10 | cmd.Execute()
11 | }
12 |
--------------------------------------------------------------------------------
/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/borakasmer/pwd-cli
2 |
3 | go 1.18
4 |
5 | require (
6 | github.com/inconshreveable/mousetrap v1.0.0 // indirect
7 | github.com/spf13/cobra v1.5.0 // indirect
8 | github.com/spf13/pflag v1.0.5 // indirect
9 | )
10 |
--------------------------------------------------------------------------------
/go.sum:
--------------------------------------------------------------------------------
1 | github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2 | github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
3 | github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
4 | github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5 | github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
6 | github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
7 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
8 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
9 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # pwd-cli
2 | With this CLI Tool, you can generate your own password with specific parameters. You can get custom password with "-s 7, -n 3, -c -x" flags.
3 | 🍎This is Custom Password CLI. In every request, generated a new random password. If you don't set any parameters, you will get 8 length lowercase string password.
4 |
5 | 
6 |
7 | Flags:
8 |
| :tv: | pwd-cli |
| pwd-cli -s 8 -n 3 -c -x |
9 |
10 | - -s, --str int Length of string characters in Password
11 | - -n, --num int Length of number characters in Password
12 | - -h, --help Help for pwd-cli
13 | - -c, --capital bool Is there any capital letter ?
14 | - -x, --symbol bool Is there any symbol letter ? It's Length is allways => "len(str)/3 + 1"
15 |
16 |
17 |
18 | Default Command: "pwd-cli"
19 | Example Usage:
20 |
21 | Usage:
22 | pwd-cli [flags]
23 |
24 | - "pwd-cli -s 8 -n 3 -c"
25 | - "pwd-cli -s 10 -c -x"
26 | - "pwd-cli -n 6"
27 |
28 | - "pwd-cli --str 6 --num 2 --capital --symbol"
29 | - "pwd-cli" => default: 'pwd-cli -s 8 -n 0 -x 0 -c 0'
30 |
31 | **********************************************************************************************************
32 |
33 | -c flag gets "true", if there is "-c" flag. If there is no -c flag, -c flag gets "false" value.
34 | -s flag gets default 8 value.
-x flag means put symbol in Password. Default symbol length formule is => "len(string)/3+1"
35 |
36 | How to install pwd-cli (Random Password Generator):
37 |
38 |  |
39 | go install github.com/borakasmer/pwd-cli@latest |
40 |
41 | 🔴Important: You need Go program package to install pwd-cli => Go Downloads
42 |
43 | - "go env" With the command "GOPATH" and "GOROOT" folders of GO can be seen.
44 | - On Mac after "go install ..." the "go/bin/pwd-cli" file under GOPATH => should be copied under "go/bin/" folder under GOROOT.
45 | - In Windows, there is no need to further action :white_check_mark:
46 |
47 |
48 | :green_book: Extra Detail
49 | --------------------------------------------------------------------------------------------------------------
50 |
51 | - "-s 10" The Length of String Characters in Password is 10 and none of them is Capital. The default value is 8.
52 | - "-n 7" => The Length of Number Characters in Password is 7
53 | - "-s 5 -c" => The String Length of Password is 5 and some of them are Capital!
54 | - "-s 6 -x" => The String Length of Password is 6 and (6/3+1) =>3 characters are Symbol! Total length of Password is 9
55 | - "pwd-cli" command => By default it means "pwd-cli --str 8 --num 0". And there is no Capital letter in the Password.
56 |
--------------------------------------------------------------------------------
/cmd/root.go:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2022 NAME HERE bora@borakasmer.com
3 |
4 | */
5 | package cmd
6 |
7 | import (
8 | "fmt"
9 | "math/big"
10 | "unicode"
11 |
12 | //"math/rand"
13 | "crypto/rand"
14 | "github.com/spf13/cobra"
15 | "os"
16 | "strconv"
17 | )
18 |
19 | // rootCmd represents the base command when called without any subcommands
20 | var rootCmd = &cobra.Command{
21 | Use: "pwd-cli",
22 | Short: "You can generate a password with string and number length parameters, isCaption parameter and finally has symbol parameter.",
23 | Long: `
24 | You can generate specific password with parameters like
25 | "pwd-cli -s 7 -n 3 -c"
26 | "pwd-cli -s9 -n2 -x"
27 | "pwd-cli"
28 | "pwd-cli -n 10 -x"
29 | "pwd-cli -s 10"
30 | *** Password Length = s(string) + n(number) total length ***
31 | ------------------
32 | Example:
33 | "-s 7" Length of string characters of the new password parameter.
34 | "-n 3" => Length of number characters of the new password parameter.
35 | "-c" => Has password capital letters parameter.
36 | "-x" => Has password symbol letters parameter.
37 | "pwd-cli" command => Default "pwd-cli --string 8 --number 0 --capital 0 --symbol 0"`,
38 | // Uncomment the following line if your bare application
39 | // has an action associated with it:
40 | Run: func(cmd *cobra.Command, args []string) {
41 | isCapital, err := cmd.Flags().GetBool("capital")
42 | if err != nil {
43 | fmt.Println("Error: " + err.Error())
44 | }
45 | isSymbol, err := cmd.Flags().GetBool("symbol")
46 | if err != nil {
47 | fmt.Println("Error: " + err.Error())
48 | }
49 | str, err := cmd.Flags().GetInt("str")
50 | if err != nil {
51 | fmt.Println("Error: " + err.Error())
52 | }
53 | num, err := cmd.Flags().GetInt("num")
54 | if err != nil {
55 | fmt.Println("Error: " + err.Error())
56 | }
57 | generatePassword(PasswordParams{isCapital: isCapital, strLength: str, numLength: num, isSymbol: isSymbol})
58 | },
59 | }
60 |
61 | // Execute adds all child commands to the root command and sets flags appropriately.
62 | // This is called by main.main(). It only needs to happen once to the rootCmd.
63 | func Execute() {
64 | err := rootCmd.Execute()
65 | if err != nil {
66 | os.Exit(1)
67 | }
68 | }
69 |
70 | func init() {
71 | // Here you will define your flags and configuration settings.
72 | // Cobra supports persistent flags, which, if defined here,
73 | // will be global for your application.
74 |
75 | // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.pwd-cli.yaml)")
76 |
77 | // Cobra also supports local flags, which will only run
78 | // when this action is called directly.
79 | //rootCmd.Flags().IntP("str", "s", 8, "Length of string") //03.07.2022 (If only set "-n6", password has only 6 digit numbers)
80 | rootCmd.Flags().IntP("str", "s", 0, "Length of string")
81 | rootCmd.Flags().IntP("num", "n", 0, "Length of number")
82 | rootCmd.Flags().BoolP("capital", "c", false, "Is there any capital letter ?")
83 | rootCmd.Flags().BoolP("symbol", "x", false, "Is there any symbol ?")
84 | rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
85 | }
86 |
87 | type PasswordParams struct {
88 | strLength int
89 | numLength int
90 | isCapital bool
91 | isSymbol bool
92 | }
93 |
94 | /*
95 | func generatePassword(params PasswordParams) {
96 | //fmt.Println(params)
97 | if params.strLength == 0 && params.numLength == 0 {
98 | params.strLength = 8
99 | }
100 | if params.numLength == 0 {
101 | fmt.Println(getRandomString(params.strLength, params.isCapital))
102 | } else if params.numLength > 0 {
103 | password := ""
104 | numberList := make(map[int]string)
105 | passwordLength := params.numLength + params.strLength
106 | for i := 0; i < params.numLength; i++ {
107 | for {
108 | index := getRandomNumber(passwordLength)
109 | if _, ok := numberList[index]; ok {
110 |
111 | } else {
112 | numberList[index] = strconv.Itoa(getRandomNumber(10))
113 | break
114 | }
115 | }
116 | }
117 |
118 | for i2 := 0; i2 < passwordLength; i2++ {
119 | if _, ok := numberList[i2]; ok {
120 | password += numberList[i2]
121 | } else {
122 | password += getRandomString(1, params.isCapital)
123 | }
124 | }
125 | fmt.Println(password)
126 | }
127 | }
128 | */
129 |
130 | func generatePassword(params PasswordParams) {
131 | symbolList := make(map[int]string)
132 | password := ""
133 | //If number and string length not set.
134 | if params.strLength == 0 && params.numLength == 0 {
135 | params.strLength = 8
136 | }
137 | passwordLength := params.numLength + params.strLength
138 |
139 | //Set Symbol Characters of the Password if IsSymbol==true
140 | if params.isSymbol {
141 | passwordLength += params.strLength/3 + 1
142 | for i := 0; i < params.strLength/3+1; i++ {
143 | for {
144 | index := getRandomNumber(int64(passwordLength))
145 | //If you not get same RandomIndex for symbol character you will set RandomNumber for RandomIndex
146 | if _, ok := symbolList[int(index)]; ok {
147 |
148 | } else {
149 | symbolList[int(index)] = getRandomSymbol()
150 | break
151 | }
152 | }
153 | }
154 | }
155 | //Set Symbols Index Finish----------
156 |
157 | if params.numLength == 0 {
158 | //Set String Characters of the Password
159 | if params.isSymbol {
160 | for {
161 | for i2 := 0; i2 < passwordLength; i2++ {
162 | if _, ok := symbolList[i2]; ok {
163 | password += symbolList[i2]
164 | } else {
165 | password += getRandomString(1, params.isCapital)
166 | }
167 | }
168 | //If isCapital flag false or there is at least one capital letter in password, we will break the loop
169 | //If not set String Characters again until has a capital letter!
170 | if !params.isCapital || checkHasCapital(password) {
171 | break
172 | } else {
173 | password = ""
174 | }
175 | }
176 | } else {
177 | password = getRandomString(params.strLength, params.isCapital)
178 | if params.isCapital {
179 | //If isCapital flag true, we will check is there any capital letter in password.
180 | //If not set again until has a capital letter!
181 | for {
182 | if checkHasCapital(password) {
183 | break
184 | } else {
185 | password = getRandomString(params.strLength, params.isCapital)
186 | }
187 | }
188 | }
189 | }
190 | /*var password = getRandomString(params.strLength, params.isCapital)
191 | if params.isCapital {
192 | //If isCapital flag true, we will check is there any capital letter in password.
193 | //If not set again until has a capital letter!
194 | for {
195 | if checkHasCapital(password) {
196 | break
197 | } else {
198 | password = getRandomString(params.strLength, params.isCapital)
199 | }
200 | }
201 | }*/
202 | fmt.Println(password)
203 | } else if params.numLength > 0 {
204 | password := ""
205 | numberList := make(map[int]string)
206 | passwordLength := params.numLength + params.strLength
207 |
208 | if params.isSymbol {
209 | passwordLength += params.strLength/3 + 1
210 | }
211 | //Set Number Characters of the Password
212 | for i := 0; i < params.numLength; i++ {
213 | for {
214 | index := getRandomNumber(int64(passwordLength))
215 | //If you not get same RandomIndex for number character you will set RandomNumber for RandomIndex
216 | if _, ok := numberList[int(index)]; ok {
217 |
218 | } else if _, ok := symbolList[int(index)]; ok { //If this is not in one of the symbolIndex
219 |
220 | } else {
221 | numberList[int(index)] = strconv.Itoa(int(getRandomNumber(int64(10))))
222 | break
223 | }
224 | }
225 | }
226 |
227 | //Set String Characters of the Password
228 | //03.07.2022
229 | if params.strLength > 0 { //Classic Password => String + Number + Symbol
230 | for {
231 | for i2 := 0; i2 < passwordLength; i2++ {
232 | if _, ok := numberList[i2]; ok {
233 | password += numberList[i2]
234 | } else if _, ok := symbolList[i2]; ok {
235 | password += symbolList[i2]
236 | } else {
237 | password += getRandomString(1, params.isCapital)
238 | }
239 | }
240 | //If isCapital flag false or there is at least one capital letter in password, we will break the loop
241 | //If not set String Characters again until has a capital letter!
242 | if !params.isCapital || checkHasCapital(password) {
243 | break
244 | } else {
245 | password = ""
246 | }
247 | }
248 | } else { //Password has only Numbers and Symbols 03.07.2022
249 | //if setLength not set but numLength set => Password has only Number or NumberAndSymbol
250 | for i2 := 0; i2 < passwordLength; i2++ {
251 | if _, ok := numberList[i2]; ok {
252 | password += numberList[i2]
253 | } else if _, ok := symbolList[i2]; ok {
254 | password += symbolList[i2]
255 | }
256 | }
257 | }
258 | fmt.Println(password)
259 | }
260 | }
261 |
262 | /*func getRandomNumber(length int) int {
263 | rand.Seed(time.Now().UnixNano())
264 | return rand.Intn(length)
265 | }*/
266 |
267 | func getRandomNumber(length int64) int64 {
268 | result, _ := rand.Int(rand.Reader, big.NewInt(length))
269 | return result.Int64()
270 | }
271 |
272 | var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
273 | var letterRunes2 = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
274 | var symbolRunes = []rune("~=+%^*/()[]{}/!@#$?|")
275 |
276 | /*
277 | func getRandomString(length int, isCapital bool) string {
278 | rand.Seed(time.Now().UnixNano())
279 | b := make([]rune, length)
280 | for i := range b {
281 | if isCapital {
282 | b[i] = letterRunes2[rand.Intn(len(letterRunes2))]
283 | } else {
284 | b[i] = letterRunes[rand.Intn(len(letterRunes))]
285 | }
286 | }
287 | return string(b)
288 | }
289 | */
290 |
291 | func getRandomSymbol() string {
292 | result, _ := rand.Int(rand.Reader, big.NewInt(int64(len(symbolRunes))))
293 | return string(symbolRunes[result.Int64()])
294 | }
295 |
296 | func getRandomString(length int, isCapital bool) string {
297 | b := make([]rune, length)
298 | for i := range b {
299 | if isCapital {
300 | result, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letterRunes2))))
301 | b[i] = letterRunes2[result.Int64()]
302 | } else {
303 | result, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letterRunes))))
304 | b[i] = letterRunes[result.Int64()]
305 | }
306 | }
307 | return string(b)
308 | }
309 |
310 | func checkHasCapital(password string) bool {
311 | for i := 0; i < len(password); i++ {
312 | if unicode.IsUpper(rune(password[i])) {
313 | return true
314 | }
315 | }
316 | return false
317 | }
318 |
--------------------------------------------------------------------------------