From f49eb3ed46ad47c416985c18874fc9a071691ef4 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 15 Sep 2023 12:34:31 -0600 Subject: [PATCH] =?UTF-8?q?Change=20answer=20hash=20algorithm=20to=20SHA1?= =?UTF-8?q?=E2=82=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++-- pkg/transpile/puzzle.go | 6 +++--- pkg/transpile/puzzle_test.go | 6 ++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fc8422..5fd80c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [v4.6.0] - unreleased ### Changed -- We are now using djb2xor instead of sha256 to hash puzzle answers -- Lots of work on the built-in theme +- Answer hashes are now the first 4 characters of the hex-encoded SHA1 digest +- Reworked the built-in theme - [moth.mjs](theme/moth.mjs) is now the standard MOTH library for ECMAScript - Devel mode no longer accepts an empty team ID diff --git a/pkg/transpile/puzzle.go b/pkg/transpile/puzzle.go index e4045ce..23d8a90 100644 --- a/pkg/transpile/puzzle.go +++ b/pkg/transpile/puzzle.go @@ -4,7 +4,7 @@ import ( "bufio" "bytes" "context" - "crypto/sha256" + "crypto/sha1" "encoding/json" "errors" "fmt" @@ -85,9 +85,9 @@ func (puzzle *Puzzle) computeAnswerHashes() { } puzzle.AnswerHashes = make([]string, len(puzzle.Answers)) for i, answer := range puzzle.Answers { - sum := sha256.Sum256([]byte(answer)) + sum := sha1.Sum([]byte(answer)) hexsum := fmt.Sprintf("%x", sum) - puzzle.AnswerHashes[i] = hexsum + puzzle.AnswerHashes[i] = hexsum[:4] } } diff --git a/pkg/transpile/puzzle_test.go b/pkg/transpile/puzzle_test.go index 47db867..d6b9022 100644 --- a/pkg/transpile/puzzle_test.go +++ b/pkg/transpile/puzzle_test.go @@ -23,6 +23,12 @@ func TestPuzzle(t *testing.T) { if (len(p.Answers) == 0) || (p.Answers[0] != "YAML answer") { t.Error("Answers are wrong", p.Answers) } + if len(p.Answers) != len(p.AnswerHashes) { + t.Error("Answer hashes length does not match answers length") + } + if len(p.AnswerHashes[0]) != 4 { + t.Error("Answer hash is wrong length") + } if (len(p.Authors) != 3) || (p.Authors[1] != "Buster") { t.Error("Authors are wrong", p.Authors) }