Browse Source

move password handling into a single file

tags/v0.1.0
Jeremy Latt 10 years ago
parent
commit
9aa7debbfe
4 changed files with 45 additions and 20 deletions
  1. 6
    13
      ergonomadic.go
  2. 1
    2
      irc/commands.go
  3. 1
    5
      irc/config.go
  4. 37
    0
      irc/password.go

+ 6
- 13
ergonomadic.go View File

@@ -1,24 +1,12 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"code.google.com/p/go.crypto/bcrypt"
5
-	"encoding/base64"
6 4
 	"flag"
7 5
 	"fmt"
8 6
 	"github.com/jlatt/ergonomadic/irc"
9 7
 	"log"
10
-	"os"
11 8
 )
12 9
 
13
-func genPasswd(passwd string) {
14
-	crypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
15
-	if err != nil {
16
-		log.Fatal(err)
17
-	}
18
-	encoded := base64.StdEncoding.EncodeToString(crypted)
19
-	fmt.Println(encoded)
20
-}
21
-
22 10
 func main() {
23 11
 	conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file")
24 12
 	initdb := flag.Bool("initdb", false, "initialize database")
@@ -26,7 +14,11 @@ func main() {
26 14
 	flag.Parse()
27 15
 
28 16
 	if *passwd != "" {
29
-		genPasswd(*passwd)
17
+		encoded, err := irc.GenerateEncodedPassword(*passwd)
18
+		if err != nil {
19
+			log.Fatal(err)
20
+		}
21
+		fmt.Println(encoded)
30 22
 		return
31 23
 	}
32 24
 
@@ -37,6 +29,7 @@ func main() {
37 29
 
38 30
 	if *initdb {
39 31
 		irc.InitDB(config.Database())
32
+		log.Println("database initialized: " + config.Database())
40 33
 		return
41 34
 	}
42 35
 

+ 1
- 2
irc/commands.go View File

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

+ 1
- 5
irc/config.go View File

@@ -1,7 +1,6 @@
1 1
 package irc
2 2
 
3 3
 import (
4
-	"encoding/base64"
5 4
 	"encoding/json"
6 5
 	"log"
7 6
 	"os"
@@ -9,10 +8,7 @@ import (
9 8
 )
10 9
 
11 10
 func decodePassword(password string) []byte {
12
-	if password == "" {
13
-		return nil
14
-	}
15
-	bytes, err := base64.StdEncoding.DecodeString(password)
11
+	bytes, err := DecodePassword(password)
16 12
 	if err != nil {
17 13
 		log.Fatal(err)
18 14
 	}

+ 37
- 0
irc/password.go View File

@@ -0,0 +1,37 @@
1
+package irc
2
+
3
+import (
4
+	"code.google.com/p/go.crypto/bcrypt"
5
+	"encoding/base64"
6
+	"errors"
7
+)
8
+
9
+var (
10
+	EmptyPasswordError = errors.New("empty password")
11
+)
12
+
13
+func GenerateEncodedPassword(passwd string) (encoded string, err error) {
14
+	if passwd == "" {
15
+		err = EmptyPasswordError
16
+		return
17
+	}
18
+	bcrypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
19
+	if err != nil {
20
+		return
21
+	}
22
+	encoded = base64.StdEncoding.EncodeToString(bcrypted)
23
+	return
24
+}
25
+
26
+func DecodePassword(encoded string) (decoded []byte, err error) {
27
+	if encoded == "" {
28
+		err = EmptyPasswordError
29
+		return
30
+	}
31
+	decoded, err = base64.StdEncoding.DecodeString(encoded)
32
+	return
33
+}
34
+
35
+func ComparePassword(hash, password []byte) error {
36
+	return bcrypt.CompareHashAndPassword(hash, password)
37
+}

Loading…
Cancel
Save