switch pow to argon2id

This commit is contained in:
2026-11-22 13:52:29 -05:00
parent 80ad8a62ae
commit 8730b968b5
8 changed files with 41 additions and 16 deletions
+9
View File
@@ -18,3 +18,12 @@ MINIBIN_NORMALIZE_MIME_TYPES=true
# prevents uploads with the specified mime types, separated by commas # prevents uploads with the specified mime types, separated by commas
# this example is pretty long, but it is majority (if not all) of the executable types github.com/gabriel-vasile/mimetype detects # this example is pretty long, but it is majority (if not all) of the executable types github.com/gabriel-vasile/mimetype detects
MINIBIN_BANNED_MIME_TYPES="application/vnd.microsoft.portable-executable,application/java-archive,application/x-executable,application/vnd.android.package-archive,application/x-ms-installer,application/x-elf,application/vnd.debian.binary-package" MINIBIN_BANNED_MIME_TYPES="application/vnd.microsoft.portable-executable,application/java-archive,application/x-executable,application/vnd.android.package-archive,application/x-ms-installer,application/x-elf,application/vnd.debian.binary-package"
# this is the amount of leading zero bits required for proof of work completion
# 8-12 is a good range, 8 being quickest, while 12 is the slowest
# with my computer 12 can take around 10-20 seconds
# whereas 8 takes around a few seconds or less
MINIBIN_POW_DIFFICULTY=10
# comma separated options the user can select when reporting
# options show in the order they are written
# leading spaces WILL be included, so make sure you dont include them
MINIBIN_REPORT_OPTIONS="Malware (including links),CSAM/CSEM,Copyright Infringement,Fraud"
+3 -2
View File
@@ -7,6 +7,7 @@ require (
github.com/gabriel-vasile/mimetype v1.4.16 github.com/gabriel-vasile/mimetype v1.4.16
github.com/glebarez/sqlite v1.11.0 github.com/glebarez/sqlite v1.11.0
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
golang.org/x/crypto v0.59.0
gorm.io/gorm v1.31.2 gorm.io/gorm v1.31.2
) )
@@ -17,8 +18,8 @@ require (
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-isatty v0.0.17 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/sys v0.7.0 // indirect golang.org/x/sys v0.50.0 // indirect
golang.org/x/text v0.41.0 // indirect golang.org/x/text v0.44.0 // indirect
modernc.org/libc v1.22.5 // indirect modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect modernc.org/memory v1.5.0 // indirect
+6 -4
View File
@@ -23,11 +23,13 @@ github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxU
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
golang.org/x/crypto v0.59.0 h1:xS2jhqYbzNrpvkWP2YTDf4TJwBy3ykR03i8vFDxEc7c=
golang.org/x/crypto v0.59.0/go.mod h1:veDyUwATGs3c+vN3VfTbzViMGXeTBk3WyhBOkn+F/4U=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= golang.org/x/text v0.44.0 h1:YItdp/mQNGnsmrgpcdA5yQ3bnRxCfo/Tj/iByGW9GGo=
golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= golang.org/x/text v0.44.0/go.mod h1:S4FR1waQ9LbjQTls97SyRjDaUUVkaM+cXwwuG/G+MwY=
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8= gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo= gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo=
+1 -3
View File
@@ -60,7 +60,7 @@ func LoadConfig() (Config, error) {
expiries = append(expiries, expiry) expiries = append(expiries, expiry)
} }
powDifficultySetting := envOrDefault("MINIBIN_POW_DIFFICULTY", "18") powDifficultySetting := envOrDefault("MINIBIN_POW_DIFFICULTY", "10")
powDifficulty, err := strconv.Atoi(powDifficultySetting) powDifficulty, err := strconv.Atoi(powDifficultySetting)
if err != nil { if err != nil {
return Config{}, fmt.Errorf("failed to parse MINIBIN_POW_DIFFICULTY") return Config{}, fmt.Errorf("failed to parse MINIBIN_POW_DIFFICULTY")
@@ -71,8 +71,6 @@ func LoadConfig() (Config, error) {
return Config{}, fmt.Errorf("no valid report options set") return Config{}, fmt.Errorf("no valid report options set")
} }
log.Println(len(reportOptions))
return Config{ return Config{
UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"), UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"),
MaxPasteBytes: maxPasteKiB * 1024, MaxPasteBytes: maxPasteKiB * 1024,
+6 -3
View File
@@ -2,8 +2,8 @@ package reports
import ( import (
"crypto/rand" "crypto/rand"
"crypto/sha256"
"crypto/subtle" "crypto/subtle"
"encoding/binary"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log" "log"
@@ -11,6 +11,7 @@ import (
"time" "time"
"git.whoswhip.dev/whoswhip/minibin/internal/storage" "git.whoswhip.dev/whoswhip/minibin/internal/storage"
"golang.org/x/crypto/argon2"
) )
type Challenge struct { type Challenge struct {
@@ -94,8 +95,10 @@ func (store *ChallengeStore) Verify(id string, result string, nonce int) (client
if !hasLeadingZeroBits(hashBytes, challenge.Difficulty) { if !hasLeadingZeroBits(hashBytes, challenge.Difficulty) {
return true, fmt.Errorf("proof-of-work is not complete") return true, fmt.Errorf("proof-of-work is not complete")
} }
input := fmt.Appendf(nil, "%s%d", hex.EncodeToString(challenge.Data), nonce) input := []byte(hex.EncodeToString(challenge.Data))
localResult := sha256.Sum256(input) salt := make([]byte, 8)
binary.LittleEndian.PutUint64(salt, uint64(nonce))
localResult := argon2.IDKey(input, salt, 2, 4096, 1, 32)
if subtle.ConstantTimeCompare(localResult[:], hashBytes) != 1 { if subtle.ConstantTimeCompare(localResult[:], hashBytes) != 1 {
return true, fmt.Errorf("results do not match") return true, fmt.Errorf("results do not match")
} }
+1 -1
View File
@@ -45,7 +45,7 @@ goals:
- [ ] simplify rendering & serving pages (too much duplicate code rn) - [ ] simplify rendering & serving pages (too much duplicate code rn)
- [x] reports - [x] reports
- [x] PoW challenges/captchas - [x] PoW challenges/captchas
- [ ] switch to argon2id instead of sha256 - [x] switch to argon2id instead of sha256
- javascript required - javascript required
- [ ] ASN blocking via geolite2 - [ ] ASN blocking via geolite2
+14 -2
View File
@@ -1,3 +1,5 @@
import { argon2id } from "https://esm.sh/hash-wasm@4.12.0";
function hasLeadingZeroBits(bytes, requiredBits) { function hasLeadingZeroBits(bytes, requiredBits) {
const whole = Math.floor(requiredBits / 8); const whole = Math.floor(requiredBits / 8);
const remaining = requiredBits % 8; const remaining = requiredBits % 8;
@@ -15,9 +17,19 @@ self.onmessage = async ({ data }) => {
console.log(`[PoW] Difficulty: ${difficulty}, Starting Nonce: ${startNonce}, Challenge: ${challenge}`) console.log(`[PoW] Difficulty: ${difficulty}, Starting Nonce: ${startNonce}, Challenge: ${challenge}`)
const startTime = performance.now(); const startTime = performance.now();
const encoder = new TextEncoder(); const encoder = new TextEncoder();
const input = encoder.encode(challenge);
for (let nonce = startNonce; ; nonce++) { for (let nonce = startNonce; ; nonce++) {
const input = encoder.encode(`${challenge}${nonce}`); const salt = new Uint8Array(8);
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", input)); new DataView(salt.buffer).setBigUint64(0, BigInt(nonce), true);
const digest = await argon2id({
password: input,
salt,
parallelism: 1,
iterations: 2,
memorySize: 4096,
hashLength: 32,
outputType: "binary"
});
if (hasLeadingZeroBits(digest, difficulty)) { if (hasLeadingZeroBits(digest, difficulty)) {
const hashHex = Array.from(digest).map((b) => b.toString(16).padStart(2, "0")).join(""); const hashHex = Array.from(digest).map((b) => b.toString(16).padStart(2, "0")).join("");
const elapsed = performance.now() - startTime; const elapsed = performance.now() - startTime;
+1 -1
View File
@@ -1,4 +1,4 @@
const worker = new Worker("/js/pow-worker.js"); const worker = new Worker("/js/pow-worker.js", { type: "module" });
const reportForm = document.getElementById("report-form"); const reportForm = document.getElementById("report-form");
const submitBtn = document.getElementById("submit-btn"); const submitBtn = document.getElementById("submit-btn");
const powLabel = document.getElementById("pow-label"); const powLabel = document.getElementById("pow-label");