69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"code.google.com/p/go.crypto/bcrypt"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
"sync"
|
|
)
|
|
|
|
var dbMutex = sync.RWMutex{}
|
|
|
|
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)
|
|
}
|
|
for _, hash := range hashes {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(attempt))
|
|
if err == nil {
|
|
result = true
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func loadChildren(filename string) (children []Child) {
|
|
dbMutex.RLock()
|
|
defer dbMutex.RUnlock()
|
|
b, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
err = json.Unmarshal(b, &children)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return
|
|
}
|