@@ -13,7 +13,9 @@ import (
13
13
"golang.org/x/crypto/pbkdf2"
14
14
"io"
15
15
"math/big"
16
+ "math/rand"
16
17
"os"
18
+ "time"
17
19
"unsafe"
18
20
19
21
crand "crypto/rand"
@@ -435,10 +437,10 @@ func decryptFileXChachaPoly(encDbPath string, password string) error {
435
437
}
436
438
437
439
// Generate a random password - for adding listings
438
- func generateRandomPassword (length int ) (error , string ) {
440
+ func generatePassword (length int ) (error , string ) {
439
441
440
442
var data []byte
441
- const source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?)(/%#!?)= "
443
+ const source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=+_()$#@!~:/% "
442
444
443
445
data = make ([]byte , length )
444
446
@@ -453,3 +455,97 @@ func generateRandomPassword(length int) (error, string) {
453
455
454
456
return nil , string (data )
455
457
}
458
+
459
+ // Generate a "strong" password
460
+ // A strong password is defined as,
461
+ // A mix of upper and lower case alphabets
462
+ // at least one number [0-9]
463
+ // at least one upper case alphabet [A-Z]
464
+ // at least one punctuation character
465
+ // at least length 12
466
+ func generateStrongPassword () (error , string ) {
467
+
468
+ var data []byte
469
+ var length int
470
+
471
+ const sourceAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
472
+ const sourceLargeAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
473
+ const sourceNum = "0123456789"
474
+ const sourcePunct = "=+_()$#@!~:/%"
475
+ const source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=+_()$#@!~:/%"
476
+
477
+ // Generate in range 12 - 16
478
+ rand .Seed (time .Now ().UnixNano ())
479
+
480
+ length = rand .Intn (4 ) + 12
481
+
482
+ data = make ([]byte , length )
483
+
484
+ var lengthAlpha int
485
+ var i , j , k , l int
486
+
487
+ // Alpha chars is at least length 3-5
488
+ lengthAlpha = rand .Intn (2 ) + 3
489
+
490
+ for i = 0 ; i < lengthAlpha ; i ++ {
491
+ num , err := crand .Int (crand .Reader , big .NewInt (int64 (len (sourceAlpha ))))
492
+ if err != nil {
493
+ return err , ""
494
+ }
495
+
496
+ data [i ] = sourceAlpha [num .Int64 ()]
497
+ }
498
+
499
+ // Add in numbers 1 or 2
500
+ var lengthNum int
501
+
502
+ lengthNum = rand .Intn (2 ) + 1
503
+
504
+ for j = i ; j < i + lengthNum ; j ++ {
505
+ num , err := crand .Int (crand .Reader , big .NewInt (int64 (len (sourceNum ))))
506
+ if err != nil {
507
+ return err , ""
508
+ }
509
+
510
+ data [j ] = sourceNum [num .Int64 ()]
511
+ }
512
+
513
+ // Add in punctuations 1 or 2
514
+ var lengthPunc int
515
+
516
+ lengthPunc = rand .Intn (2 ) + 1
517
+
518
+ for k = j ; k < j + lengthPunc ; k ++ {
519
+ num , err := crand .Int (crand .Reader , big .NewInt (int64 (len (sourcePunct ))))
520
+ if err != nil {
521
+ return err , ""
522
+ }
523
+
524
+ data [k ] = sourcePunct [num .Int64 ()]
525
+ }
526
+
527
+ // Fill in the rest
528
+ var lengthRem int
529
+
530
+ lengthRem = length - k
531
+
532
+ if lengthRem > 0 {
533
+ for l = k ; l < k + lengthRem ; l ++ {
534
+ num , err := crand .Int (crand .Reader , big .NewInt (int64 (len (source ))))
535
+ if err != nil {
536
+ return err , ""
537
+ }
538
+
539
+ data [l ] = source [num .Int64 ()]
540
+ }
541
+ }
542
+
543
+ // Shuffle a few times
544
+ for i = 0 ; i < 5 ; i ++ {
545
+ rand .Shuffle (len (data ), func (i , j int ) {
546
+ data [i ], data [j ] = data [j ], data [i ]
547
+ })
548
+ }
549
+
550
+ return nil , string (data )
551
+ }
0 commit comments