Use byte slice instead of arrays

This commit is contained in:
nemunaire 2016-01-19 13:09:36 +01:00
parent 23b9d15a57
commit 5a2a950b1f
2 changed files with 20 additions and 7 deletions

View File

@ -34,7 +34,7 @@ func (t Theme) GetExercice(id int) (Exercice, error) {
}
func (t Theme) GetExercices() ([]Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, title, statement, hint, depend, gain, video_uri FROM teams WHERE id_theme = ?", t.Id); err != nil {
if rows, err := DBQuery("SELECT id_exercice, title, statement, hint, depend, gain, video_uri FROM exercices WHERE id_theme = ?", t.Id); err != nil {
return nil, err
} else {
defer rows.Close()

View File

@ -8,11 +8,11 @@ type Key struct {
Id int64 `json:"id"`
IdExercice int64 `json:"idExercice"`
Type string `json:"type"`
Value [64]byte `json:"value"`
Value []byte `json:"value"`
}
func (e Exercice) GetKeys() ([]Key, error) {
if rows, err := DBQuery("SELECT id_key, type, value FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil {
if rows, err := DBQuery("SELECT id_key, id_exercice, type, value FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil {
return nil, err
} else {
defer rows.Close()
@ -21,6 +21,7 @@ func (e Exercice) GetKeys() ([]Key, error) {
for rows.Next() {
var k Key
k.IdExercice = e.Id
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Type, &k.Value); err != nil {
return nil, err
}
@ -34,15 +35,16 @@ func (e Exercice) GetKeys() ([]Key, error) {
}
}
func getHashedKey(raw_value string) [64]byte {
return sha512.Sum512([]byte(raw_value))
func getHashedKey(raw_value string) []byte {
hash := sha512.Sum512([]byte(raw_value))
return hash[:]
}
func (e Exercice) AddRawKey(name string, raw_value string) (Key, error) {
return e.AddKey(name, getHashedKey(raw_value))
}
func (e Exercice) AddKey(name string, value [64]byte) (Key, error) {
func (e Exercice) AddKey(name string, value []byte) (Key, error) {
if res, err := DBExec("INSERT INTO exercice_keys (id_exercice, type, value) VALUES (?, ?, ?)", e.Id, name, value); err != nil {
return Key{}, err
} else if kid, err := res.LastInsertId(); err != nil {
@ -73,5 +75,16 @@ func (k Key) Delete() (int64, error) {
}
func (k Key) Check(val string) bool {
return k.Value == getHashedKey(val)
hash := getHashedKey(val)
if len(k.Value) != len(hash) {
return false
}
for i := range hash {
if k.Value[i] != hash[i] {
return false
}
}
return true
}