sync: fix hash computation by factorizing
This commit is contained in:
parent
0766fbe480
commit
97a3aa713f
3 changed files with 32 additions and 36 deletions
|
@ -110,7 +110,11 @@ func buildKeyFlag(exercice fic.Exercice, flag ExerciceFlag, flagline int, defaul
|
||||||
errs = append(errs, fmt.Sprintf("%q: WARNING flag #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), flagline))
|
errs = append(errs, fmt.Sprintf("%q: WARNING flag #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), flagline))
|
||||||
}
|
}
|
||||||
|
|
||||||
hashedFlag := fic.ComputeHashedFlag([]byte(raw))
|
hashedFlag, err := fic.ComputeHashedFlag([]byte(raw), !flag.CaseSensitive, validatorRegexp(flag.ValidatorRe))
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
fl := fic.Flag(fic.FlagKey{
|
fl := fic.Flag(fic.FlagKey{
|
||||||
IdExercice: exercice.Id,
|
IdExercice: exercice.Id,
|
||||||
Label: flag.Label,
|
Label: flag.Label,
|
||||||
|
|
|
@ -68,9 +68,25 @@ func (e Exercice) GetFlagKeyByLabel(label string) (k FlagKey, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ComputeHashedFlag calculates the expected checksum for the given raw_value.
|
// ComputeHashedFlag calculates the expected checksum for the given raw_value.
|
||||||
func ComputeHashedFlag(raw_value []byte) [blake2b.Size]byte {
|
func ComputeHashedFlag(raw_value []byte, ignorecase bool, validator_regexp *string) (hash [blake2b.Size]byte, err error) {
|
||||||
hash := blake2b.Sum512(raw_value)
|
if ignorecase {
|
||||||
return hash
|
raw_value = bytes.ToLower(raw_value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that raw value passes through the regexp
|
||||||
|
if validator_regexp != nil {
|
||||||
|
if raw_value, err = ExecValidatorRegexp(*validator_regexp, raw_value, ignorecase); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the value is not empty
|
||||||
|
if len(raw_value) == 0 {
|
||||||
|
err = errors.New("Empty flag after applying filters")
|
||||||
|
}
|
||||||
|
|
||||||
|
hash = blake2b.Sum512(raw_value)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExecValidatorRegexp(vre string, val []byte, ignorecase bool) ([]byte, error) {
|
func ExecValidatorRegexp(vre string, val []byte, ignorecase bool) ([]byte, error) {
|
||||||
|
@ -88,19 +104,11 @@ func ExecValidatorRegexp(vre string, val []byte, ignorecase bool) ([]byte, error
|
||||||
|
|
||||||
// AddRawFlagKey creates and fills a new struct FlagKey, from a non-hashed flag, and registers it into the database.
|
// AddRawFlagKey creates and fills a new struct FlagKey, from a non-hashed flag, and registers it into the database.
|
||||||
func (e Exercice) AddRawFlagKey(name string, help string, ignorecase bool, validator_regexp *string, raw_value []byte, choicescost int64) (f FlagKey, err error) {
|
func (e Exercice) AddRawFlagKey(name string, help string, ignorecase bool, validator_regexp *string, raw_value []byte, choicescost int64) (f FlagKey, err error) {
|
||||||
if ignorecase {
|
hash, errr := ComputeHashedFlag(raw_value, ignorecase, validator_regexp)
|
||||||
raw_value = bytes.ToLower(raw_value)
|
if errr != nil {
|
||||||
|
return f, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that raw value passes through the regexp
|
|
||||||
if validator_regexp != nil {
|
|
||||||
if raw_value, err = ExecValidatorRegexp(*validator_regexp, raw_value, ignorecase); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
hash := ComputeHashedFlag(raw_value)
|
|
||||||
|
|
||||||
f = FlagKey{
|
f = FlagKey{
|
||||||
Label: name,
|
Label: name,
|
||||||
Help: help,
|
Help: help,
|
||||||
|
@ -139,26 +147,10 @@ func (k FlagKey) Create(e Exercice) (Flag, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetChecksumFromValue .
|
// ComputeChecksum calculates the checksum for a given value.
|
||||||
func (k FlagKey) ComputeChecksum(val []byte) (cksum []byte, err error) {
|
func (k FlagKey) ComputeChecksum(val []byte) ([]byte, error) {
|
||||||
if k.IgnoreCase {
|
cksum, err := ComputeHashedFlag(val, k.IgnoreCase, k.ValidatorRegexp)
|
||||||
val = bytes.ToLower(val)
|
return cksum[:], err
|
||||||
}
|
|
||||||
|
|
||||||
// Check that raw value passes through the regexp
|
|
||||||
if k.ValidatorRegexp != nil {
|
|
||||||
if val, err = ExecValidatorRegexp(*k.ValidatorRegexp, val, k.IgnoreCase); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that the value is not empty
|
|
||||||
if len(val) == 0 {
|
|
||||||
err = errors.New("Empty flag after applying filters")
|
|
||||||
}
|
|
||||||
|
|
||||||
hash := ComputeHashedFlag(val)
|
|
||||||
return hash[:], err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update applies modifications back to the database.
|
// Update applies modifications back to the database.
|
||||||
|
|
|
@ -296,7 +296,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if t == nil {
|
if t == nil {
|
||||||
h := ComputeHashedFlag([]byte(soluce))
|
h, _ := ComputeHashedFlag([]byte(soluce), false, nil)
|
||||||
m.Soluce = hex.EncodeToString(h[:])
|
m.Soluce = hex.EncodeToString(h[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue