pulled out sqlite3 for json
This commit is contained in:
@@ -2,26 +2,44 @@ package main
|
||||
|
||||
import (
|
||||
"code.google.com/p/go.crypto/bcrypt"
|
||||
"github.com/kuroneko/gosqlite3"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var dbMutex sync.Mutex
|
||||
|
||||
func check_password(attempt string) (result bool) {
|
||||
db, err := sqlite3.Open(*db_file)
|
||||
defer db.Close()
|
||||
func get_passes(filename string) (cur_passes []string, err error) {
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = json.Unmarshal(b, &cur_passes)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func add_password(filename, new_pw string) (err error) {
|
||||
cur_passes, err := get_passes(filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
hpass, err := bcrypt.GenerateFromPassword(
|
||||
[]byte(*add_pw), bcrypt.DefaultCost)
|
||||
cur_passes = append(cur_passes, string(hpass))
|
||||
b, err := json.Marshal(cur_passes)
|
||||
err = ioutil.WriteFile(filename, b, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func check_password(filename, attempt string) (result bool) {
|
||||
hashes, err := get_passes(filename)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// this feels ultra hokey ... I guess I could take it from 2N to N by |= ...
|
||||
hashes := []string{}
|
||||
cmd := "SELECT hash FROM passes;"
|
||||
db.Execute(cmd, func(s *sqlite3.Statement, values ...interface{}) {
|
||||
cur_hash := values[0].(string)
|
||||
hashes = append(hashes, cur_hash)
|
||||
})
|
||||
for _, hash := range hashes {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(attempt))
|
||||
if err == nil {
|
||||
|
||||
Reference in New Issue
Block a user