3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 20:09:41 +01:00

move password handling into a single file

This commit is contained in:
Jeremy Latt 2014-03-01 15:10:04 -08:00
parent b421971b61
commit 9aa7debbfe
4 changed files with 45 additions and 20 deletions

View File

@ -1,24 +1,12 @@
package main package main
import ( import (
"code.google.com/p/go.crypto/bcrypt"
"encoding/base64"
"flag" "flag"
"fmt" "fmt"
"github.com/jlatt/ergonomadic/irc" "github.com/jlatt/ergonomadic/irc"
"log" "log"
"os"
) )
func genPasswd(passwd string) {
crypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
if err != nil {
log.Fatal(err)
}
encoded := base64.StdEncoding.EncodeToString(crypted)
fmt.Println(encoded)
}
func main() { func main() {
conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file") conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file")
initdb := flag.Bool("initdb", false, "initialize database") initdb := flag.Bool("initdb", false, "initialize database")
@ -26,7 +14,11 @@ func main() {
flag.Parse() flag.Parse()
if *passwd != "" { if *passwd != "" {
genPasswd(*passwd) encoded, err := irc.GenerateEncodedPassword(*passwd)
if err != nil {
log.Fatal(err)
}
fmt.Println(encoded)
return return
} }
@ -37,6 +29,7 @@ func main() {
if *initdb { if *initdb {
irc.InitDB(config.Database()) irc.InitDB(config.Database())
log.Println("database initialized: " + config.Database())
return return
} }

View File

@ -1,7 +1,6 @@
package irc package irc
import ( import (
"code.google.com/p/go.crypto/bcrypt"
"code.google.com/p/go.text/unicode/norm" "code.google.com/p/go.text/unicode/norm"
"errors" "errors"
"fmt" "fmt"
@ -214,7 +213,7 @@ func (cmd *PassCommand) CheckPassword() {
if cmd.hash == nil { if cmd.hash == nil {
return return
} }
cmd.err = bcrypt.CompareHashAndPassword(cmd.hash, cmd.password) cmd.err = ComparePassword(cmd.hash, cmd.password)
} }
func NewPassCommand(args []string) (editableCommand, error) { func NewPassCommand(args []string) (editableCommand, error) {

View File

@ -1,7 +1,6 @@
package irc package irc
import ( import (
"encoding/base64"
"encoding/json" "encoding/json"
"log" "log"
"os" "os"
@ -9,10 +8,7 @@ import (
) )
func decodePassword(password string) []byte { func decodePassword(password string) []byte {
if password == "" { bytes, err := DecodePassword(password)
return nil
}
bytes, err := base64.StdEncoding.DecodeString(password)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

37
irc/password.go Normal file
View File

@ -0,0 +1,37 @@
package irc
import (
"code.google.com/p/go.crypto/bcrypt"
"encoding/base64"
"errors"
)
var (
EmptyPasswordError = errors.New("empty password")
)
func GenerateEncodedPassword(passwd string) (encoded string, err error) {
if passwd == "" {
err = EmptyPasswordError
return
}
bcrypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
if err != nil {
return
}
encoded = base64.StdEncoding.EncodeToString(bcrypted)
return
}
func DecodePassword(encoded string) (decoded []byte, err error) {
if encoded == "" {
err = EmptyPasswordError
return
}
decoded, err = base64.StdEncoding.DecodeString(encoded)
return
}
func ComparePassword(hash, password []byte) error {
return bcrypt.CompareHashAndPassword(hash, password)
}