sync: fix hash computation by factorizing

This commit is contained in:
nemunaire 2019-10-12 13:37:24 +02:00
parent 0766fbe480
commit 97a3aa713f
3 changed files with 32 additions and 36 deletions

View File

@ -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))
}
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{
IdExercice: exercice.Id,
Label: flag.Label,

View File

@ -68,9 +68,25 @@ func (e Exercice) GetFlagKeyByLabel(label string) (k FlagKey, err error) {
}
// ComputeHashedFlag calculates the expected checksum for the given raw_value.
func ComputeHashedFlag(raw_value []byte) [blake2b.Size]byte {
hash := blake2b.Sum512(raw_value)
return hash
func ComputeHashedFlag(raw_value []byte, ignorecase bool, validator_regexp *string) (hash [blake2b.Size]byte, err error) {
if ignorecase {
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) {
@ -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.
func (e Exercice) AddRawFlagKey(name string, help string, ignorecase bool, validator_regexp *string, raw_value []byte, choicescost int64) (f FlagKey, err error) {
if ignorecase {
raw_value = bytes.ToLower(raw_value)
hash, errr := ComputeHashedFlag(raw_value, ignorecase, validator_regexp)
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{
Label: name,
Help: help,
@ -139,26 +147,10 @@ func (k FlagKey) Create(e Exercice) (Flag, error) {
}
}
// SetChecksumFromValue .
func (k FlagKey) ComputeChecksum(val []byte) (cksum []byte, err error) {
if k.IgnoreCase {
val = bytes.ToLower(val)
}
// 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
// ComputeChecksum calculates the checksum for a given value.
func (k FlagKey) ComputeChecksum(val []byte) ([]byte, error) {
cksum, err := ComputeHashedFlag(val, k.IgnoreCase, k.ValidatorRegexp)
return cksum[:], err
}
// Update applies modifications back to the database.

View File

@ -296,7 +296,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
}
if t == nil {
h := ComputeHashedFlag([]byte(soluce))
h, _ := ComputeHashedFlag([]byte(soluce), false, nil)
m.Soluce = hex.EncodeToString(h[:])
}