github.com/devops-filetransfer/sshego@v7.0.4+incompatible/prompt.go (about) 1 package sshego 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "strings" 8 ) 9 10 // PromptForPassword ask 11 func PromptForPassword(username string) (pw string, err error) { 12 start := getNewPasswordStarter() 13 14 end := "" 15 const numTry = 3 16 for i := 0; i < numTry; i++ { 17 switch i { 18 case 1: 19 fmt.Printf("\n%s\n... no problem, try it again (attempt %v of %v):\n\n", err, i+1, numTry) 20 case numTry - 1: 21 fmt.Printf("\n%s\n... arg, still not right. One last try:\n\n", err) 22 } 23 fmt.Printf("adding user '%s'...\n\nThe first part of your new passphrase is '%s'. Add a memorable end to the sentence (between 3 - 100 characters) to complete it. For a strong passphrase, add five(5) or more words on top of the three we start you with\n\n%s", 24 username, start, start) 25 reader := bufio.NewReader(os.Stdin) 26 end, err = reader.ReadString('\n') 27 panicOn(err) 28 end = strings.Trim(end, "\n\r") 29 if len(end) < 3 { 30 err = fmt.Errorf("alert! completion of phrase too short, must be 3-100 characters") 31 continue 32 } 33 if len(end) > 100 { 34 err = fmt.Errorf("alert! completion of phrase too long, must be 3-100 characters") 35 continue 36 } 37 pw = start + end 38 fmt.Printf("\n Your new passphrase for account '%s' is '%s' (without the single quotes). Type the phrase in full to confirm you have it\n\n", username, pw) 39 40 var pw2 string 41 pw2, err = reader.ReadString('\n') 42 panicOn(err) 43 pw2 = strings.Trim(pw2, "\n\r") 44 if pw != pw2 { 45 err = fmt.Errorf("alert! passphrases don't match at position %v: '%s' versus '%s'", firstDiff(pw, pw2)+1, pw, pw2) 46 continue 47 } 48 49 fmt.Printf("\n Phrases match! Success!\n") 50 // success 51 err = nil 52 return 53 } 54 // fail 55 pw = "" 56 return 57 } 58 59 func firstDiff(a, b string) int { 60 if len(a) == 0 || len(b) == 0 { 61 return 0 62 } 63 i := 0 64 for ; i < len(a); i++ { 65 if i >= len(b) { 66 return i 67 } 68 if a[i] != b[i] { 69 return i 70 } 71 } 72 return i 73 }