Playground
crc32demo.gno
// Package crc32demo is a small gnoweb demo of the CRC-32 checksum provided by
// the [p/moul/x/daily/crc32](/p/nym-moultest123/x/daily/crc32/v0) library: it checksums a
// few strings and shows that a one-character change moves the result
// completely.
//
// It contains no checksum logic of its own. Stateless, so Render is
// deterministic.
package crc32demo
import (
"strings"
"gno.land/p/nym-moultest123/x/daily/crc32/v0"
)
var samples = []string{"", "a", "abc", "123456789", "hello world", "hello worle"}
// Render renders the demo for gnoweb.
//
// Render("") / Render("/") -> the samples table
// Render("/<text>") -> checksum that text
func Render(path string) string {
var b strings.Builder
b.WriteString("# CRC-32\n\n")
b.WriteString("The IEEE checksum behind zip, gzip and PNG, demoing the ")
b.WriteString("[`p/moul/x/daily/crc32`](/p/nym-moultest123/x/daily/crc32/v0) library.\n\n")
if in := parseArg(path); in != "" {
b.WriteString("## `")
b.WriteString(in)
b.WriteString("`\n\n`")
b.WriteString(crc32.ChecksumHex(in))
b.WriteString("`\n")
return b.String()
}
b.WriteString("| input | crc32 |\n|---|---|\n")
for _, s := range samples {
b.WriteString("| ")
if s == "" {
b.WriteString("_(empty)_")
} else {
b.WriteString("`" + s + "`")
}
b.WriteString(" | `")
b.WriteString(crc32.ChecksumHex(s))
b.WriteString("` |\n")
}
b.WriteString("\n`123456789` → `cbf43926` is the standard CRC-32 check value, ")
b.WriteString("so this table doubles as a conformance test.\n\n")
b.WriteString("> The last two rows differ by one letter and share no digits: ")
b.WriteString("that avalanche is the point.\n\n")
b.WriteString("> A CRC detects **accidents**, not tampering — it is linear, so a ")
b.WriteString("matching message is easy to craft. Never authenticate with it.\n")
return b.String()
}
func parseArg(path string) string {
s := strings.TrimSpace(path)
s = strings.TrimPrefix(s, "/")
if i := strings.IndexByte(s, '/'); i >= 0 {
s = s[:i]
}
return s
}
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/crc32demo/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186410 max_deposit = "5000000ugnot"
render_example_test.gno
package crc32demo
// ExampleRender pins the realm's root Render output as a testable example.
// The `123456789` row is the standard CRC-32 check value, so this example
// doubles as a conformance test for the library.
func ExampleRender() {
print(Render(""))
// Output:
// # CRC-32
//
// The IEEE checksum behind zip, gzip and PNG, demoing the [`p/moul/x/daily/crc32`](/p/nym-moultest123/x/daily/crc32/v0) library.
//
// | input | crc32 |
// |---|---|
// | _(empty)_ | `00000000` |
// | `a` | `e8b7be43` |
// | `abc` | `352441c2` |
// | `123456789` | `cbf43926` |
// | `hello world` | `0d4a1185` |
// | `hello worle` | `7a4d2113` |
//
// `123456789` → `cbf43926` is the standard CRC-32 check value, so this table doubles as a conformance test.
//
// > The last two rows differ by one letter and share no digits: that avalanche is the point.
//
// > A CRC detects **accidents**, not tampering — it is linear, so a matching message is easy to craft. Never authenticate with it.
}