Write docs!
This commit is contained in:
parent
c460bb7bf5
commit
bcc598ebd5
37 changed files with 478 additions and 188 deletions
|
|
@ -6,13 +6,19 @@ import (
|
|||
"golang.org/x/crypto/blake2b"
|
||||
)
|
||||
|
||||
|
||||
// Key represents a flag's challenge, stored as hash.
|
||||
type Key struct {
|
||||
Id int64 `json:"id"`
|
||||
// IdExercice is the identifier of the underlying challenge
|
||||
IdExercice int64 `json:"idExercice"`
|
||||
// Label is the title of the flag as displayed to players
|
||||
Label string `json:"label"`
|
||||
// Checksum is the expected hashed flag
|
||||
Checksum []byte `json:"value"`
|
||||
}
|
||||
|
||||
// GetKeys returns a list of flags comming with the challenge.
|
||||
func (e Exercice) GetKeys() ([]Key, error) {
|
||||
if rows, err := DBQuery("SELECT id_key, id_exercice, type, cksum FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -37,16 +43,19 @@ func (e Exercice) GetKeys() ([]Key, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// getHashedKey calculates the expected checksum for the given raw_value.
|
||||
func getHashedKey(raw_value string) [blake2b.Size]byte {
|
||||
hash := blake2b.Sum512([]byte(raw_value))
|
||||
return hash
|
||||
}
|
||||
|
||||
// AddRawKey creates and fills a new struct Key, from a non-hashed flag, and registers it into the database.
|
||||
func (e Exercice) AddRawKey(name string, raw_value string) (Key, error) {
|
||||
hash := getHashedKey(raw_value)
|
||||
return e.AddKey(name, hash[:])
|
||||
}
|
||||
|
||||
// AddKey creates and fills a new struct Key, from a hashed flag, and registers it into the database.
|
||||
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
|
||||
|
|
@ -57,6 +66,7 @@ func (e Exercice) AddKey(name string, checksum []byte) (Key, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Update applies modifications back to the database.
|
||||
func (k Key) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Label, k.Checksum, k.Id); err != nil {
|
||||
return 0, err
|
||||
|
|
@ -67,6 +77,7 @@ func (k Key) Update() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Delete the flag from the database.
|
||||
func (k Key) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM exercice_keys WHERE id_key = ?", k.Id); err != nil {
|
||||
return 0, err
|
||||
|
|
@ -77,6 +88,7 @@ func (k Key) Delete() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// WipeKeys deletes keys coming with the challenge.
|
||||
func (e Exercice) WipeKeys() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil {
|
||||
return 0, err
|
||||
|
|
@ -87,6 +99,7 @@ func (e Exercice) WipeKeys() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Check if the given val is the expected one for this flag.
|
||||
func (k Key) Check(val string) bool {
|
||||
hash := getHashedKey(val)
|
||||
if len(k.Checksum) != len(hash) {
|
||||
|
|
@ -102,6 +115,7 @@ func (k Key) Check(val string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// FoundBy registers in the database that the given Team solved the flag.
|
||||
func (k Key) FoundBy(t Team) {
|
||||
DBExec("INSERT INTO key_found (id_key, id_team, time) VALUES (?, ?, ?)", k.Id, t.Id, time.Now())
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue