From 5a2a950b1f576106c7c174cd4d04bc34429fc1d8 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Tue, 19 Jan 2016 13:09:36 +0100 Subject: [PATCH] Use byte slice instead of arrays --- libfic/exercice.go | 2 +- libfic/key.go | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/libfic/exercice.go b/libfic/exercice.go index 8b00f4a5..77f1d5fe 100644 --- a/libfic/exercice.go +++ b/libfic/exercice.go @@ -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() diff --git a/libfic/key.go b/libfic/key.go index e661b488..ac78f5cd 100644 --- a/libfic/key.go +++ b/libfic/key.go @@ -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 }