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
 package main
1
 package main
2
 
2
 
3
 import (
3
 import (
4
-	"code.google.com/p/go.crypto/bcrypt"
5
-	"encoding/base64"
6
 	"flag"
4
 	"flag"
7
 	"fmt"
5
 	"fmt"
8
 	"github.com/jlatt/ergonomadic/irc"
6
 	"github.com/jlatt/ergonomadic/irc"
9
 	"log"
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
 func main() {
10
 func main() {
23
 	conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file")
11
 	conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file")
24
 	initdb := flag.Bool("initdb", false, "initialize database")
12
 	initdb := flag.Bool("initdb", false, "initialize database")
26
 	flag.Parse()
14
 	flag.Parse()
27
 
15
 
28
 	if *passwd != "" {
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
 		return
22
 		return
31
 	}
23
 	}
32
 
24
 
37
 
29
 
38
 	if *initdb {
30
 	if *initdb {
39
 		irc.InitDB(config.Database())
31
 		irc.InitDB(config.Database())
32
+		log.Println("database initialized: " + config.Database())
40
 		return
33
 		return
41
 	}
34
 	}
42
 
35
 

+ 1
- 2
irc/commands.go View File

1
 package irc
1
 package irc
2
 
2
 
3
 import (
3
 import (
4
-	"code.google.com/p/go.crypto/bcrypt"
5
 	"code.google.com/p/go.text/unicode/norm"
4
 	"code.google.com/p/go.text/unicode/norm"
6
 	"errors"
5
 	"errors"
7
 	"fmt"
6
 	"fmt"
214
 	if cmd.hash == nil {
213
 	if cmd.hash == nil {
215
 		return
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
 func NewPassCommand(args []string) (editableCommand, error) {
219
 func NewPassCommand(args []string) (editableCommand, error) {

+ 1
- 5
irc/config.go View File

1
 package irc
1
 package irc
2
 
2
 
3
 import (
3
 import (
4
-	"encoding/base64"
5
 	"encoding/json"
4
 	"encoding/json"
6
 	"log"
5
 	"log"
7
 	"os"
6
 	"os"
9
 )
8
 )
10
 
9
 
11
 func decodePassword(password string) []byte {
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
 	if err != nil {
12
 	if err != nil {
17
 		log.Fatal(err)
13
 		log.Fatal(err)
18
 	}
14
 	}

+ 37
- 0
irc/password.go View File

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