From b701aa1710c4b36b4b8390e67481e36c45dc3b41 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Mon, 4 Dec 2017 08:40:17 +0100 Subject: [PATCH] Change Key.Value to Key.Checksum --- libfic/key.go | 25 +++++++++++++------------ libfic/team_my.go | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/libfic/key.go b/libfic/key.go index 1f3c43bb..2872ff1e 100644 --- a/libfic/key.go +++ b/libfic/key.go @@ -7,10 +7,10 @@ import ( ) type Key struct { - Id int64 `json:"id"` - IdExercice int64 `json:"idExercice"` - Type string `json:"type"` - Value [blake2b.KeySize]byte `json:"value"` + Id int64 `json:"id"` + IdExercice int64 `json:"idExercice"` + Type string `json:"type"` + Checksum []byte `json:"value"` } func (e Exercice) GetKeys() ([]Key, error) { @@ -24,7 +24,7 @@ func (e Exercice) GetKeys() ([]Key, error) { var k Key k.IdExercice = e.Id - if err := rows.Scan(&k.Id, &k.IdExercice, &k.Type, &k.Value); err != nil { + if err := rows.Scan(&k.Id, &k.IdExercice, &k.Type, &k.Checksum); err != nil { return nil, err } keys = append(keys, k) @@ -43,21 +43,22 @@ func getHashedKey(raw_value string) [blake2b.KeySize]byte { } func (e Exercice) AddRawKey(name string, raw_value string) (Key, error) { - return e.AddKey(name, getHashedKey(raw_value)) + hash := getHashedKey(raw_value) + return e.AddKey(name, hash[:]) } -func (e Exercice) AddKey(name string, value [blake2b.KeySize]byte) (Key, error) { - if res, err := DBExec("INSERT INTO exercice_keys (id_exercice, type, cksum) VALUES (?, ?, ?)", e.Id, name, value); err != nil { +func (e Exercice) AddKey(name string, checksum []byte) (Key, error) { + if res, err := DBExec("INSERT INTO exercice_keys (id_exercice, type, cksum) VALUES (?, ?, ?)", e.Id, name, checksum); err != nil { return Key{}, err } else if kid, err := res.LastInsertId(); err != nil { return Key{}, err } else { - return Key{kid, e.Id, name, value}, nil + return Key{kid, e.Id, name, checksum}, nil } } func (k Key) Update() (int64, error) { - if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Type, k.Value, k.Id); err != nil { + if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Type, k.Checksum, k.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err @@ -78,12 +79,12 @@ func (k Key) Delete() (int64, error) { func (k Key) Check(val string) bool { hash := getHashedKey(val) - if len(k.Value) != len(hash) { + if len(k.Checksum) != len(hash) { return false } for i := range hash { - if k.Value[i] != hash[i] { + if k.Checksum[i] != hash[i] { return false } } diff --git a/libfic/team_my.go b/libfic/team_my.go index c44c8ca0..8a21b753 100644 --- a/libfic/team_my.go +++ b/libfic/team_my.go @@ -130,7 +130,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) { } else { for _, k := range keys { if t == nil { - exercice.Keys = append(exercice.Keys, fmt.Sprintf("%x", k.Value)+k.Type) + exercice.Keys = append(exercice.Keys, fmt.Sprintf("%x", k.Checksum)+k.Type) } else { exercice.Keys = append(exercice.Keys, k.Type) if PartialValidation {