github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/common/password/password.go (about)

     1  /*
     2   * Copyright (C) 2018 The ontology Authors
     3   * This file is part of The ontology library.
     4   *
     5   * The ontology is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU Lesser General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * The ontology is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU Lesser General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU Lesser General Public License
    16   * along with The ontology.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  package password
    20  
    21  import (
    22  	"bytes"
    23  	"fmt"
    24  	"os"
    25  
    26  	"github.com/howeyc/gopass"
    27  )
    28  
    29  // GetPassword gets password from user input
    30  func GetPassword() ([]byte, error) {
    31  	fmt.Printf("Password:")
    32  	passwd, err := gopass.GetPasswd()
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return passwd, nil
    37  }
    38  
    39  // GetConfirmedPassword gets double confirmed password from user input
    40  func GetConfirmedPassword() ([]byte, error) {
    41  	fmt.Printf("input your password:")
    42  	first, err := gopass.GetPasswd()
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	if 0 == len(first) {
    47  		fmt.Println("User have to input password.")
    48  		os.Exit(1)
    49  	}
    50  
    51  	fmt.Printf("repeat to confirm your password:")
    52  	second, err := gopass.GetPasswd()
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	if 0 == len(second) {
    57  		fmt.Println("User have to input password.")
    58  		os.Exit(1)
    59  	}
    60  
    61  	if !bytes.Equal(first, second) {
    62  		fmt.Println("Unmatched Password")
    63  		os.Exit(1)
    64  	}
    65  	return first, nil
    66  }
    67  
    68  // GetPassword gets node's wallet password from command line or user input
    69  func GetAccountPassword() ([]byte, error) {
    70  	var passwd []byte
    71  	var err error
    72  	passwd, err = GetPassword()
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	return passwd, nil
    78  }