sha1demo.gno
4.74 Kb · 123 lines
1// Package sha1demo is a small gnoweb demo of the SHA-1 implementation provided
2// by the [p/moul/x/daily/sha1](/p/moul/x/daily/sha1/v0) library: the published
3// test vectors, the git object ids anyone can check with `git hash-object`,
4// HMAC answers led by RFC 2202 test case 2, and a path that hashes whatever
5// you give it.
6//
7// It contains no hashing logic of its own. Stateless, so Render is
8// deterministic.
9package sha1demo
10
11import (
12 "strconv"
13 "strings"
14
15 "gno.land/p/moul/kit/ui/v0"
16 "gno.land/p/moul/md/v0"
17 "gno.land/p/moul/x/daily/sha1/v0"
18)
19
20// vectors are the published FIPS 180-1 / RFC 3174 answers, so the page doubles
21// as a conformance test.
22var vectors = []string{"", "a", "abc", "message digest", "hello world", "hello worle"}
23
24// blobs are checkable from a terminal: `printf '%s' <content> | git hash-object --stdin`.
25var blobs = []string{"", "hello world\n", "Hello World\n"}
26
27// macs are RFC 2202 HMAC-SHA1 test case 2, the one with a key that prints,
28// followed by two representative answers that are not from the RFC: the
29// quick-brown-fox example and the empty key over the empty message.
30var macs = []struct{ key, msg string }{
31 {"Jefe", "what do ya want for nothing?"},
32 {"key", "The quick brown fox jumps over the lazy dog"},
33 {"", ""},
34}
35
36// Render renders the demo for gnoweb.
37//
38// Render("") / Render("/") -> the vector tables
39// Render("/<text>") -> hash that text
40func Render(path string) string {
41 var b strings.Builder
42 b.WriteString("# SHA-1\n\n")
43 b.WriteString("Forty hex digits from any amount of input, demoing the ")
44 b.WriteString("[`p/moul/x/daily/sha1`](/p/moul/x/daily/sha1/v0) library.\n\n")
45
46 if in := parseArg(path); in != "" {
47 b.WriteString("## " + ui.Inline(in) + "\n\n")
48 t := ui.NewTable("what", "value")
49 t.Row("sha1", md.InlineCode(sha1.SumHex(in)))
50 t.Row("git blob id", md.InlineCode(sha1.GitBlobID(in)))
51 t.Row("bytes", md.InlineCode(strconv.Itoa(len(in))))
52 b.WriteString(t.String() + "\n")
53 b.WriteString("The git id is not the hash of the content: it is the hash ")
54 b.WriteString("of `blob <len>\\x00<content>`, which is why the two rows ")
55 b.WriteString("never agree.\n")
56 return b.String()
57 }
58
59 b.WriteString("## Published vectors\n\n")
60 vt := ui.NewTable("input", "sha1")
61 for _, s := range vectors {
62 label := "_(empty)_"
63 if s != "" {
64 label = md.InlineCode(s)
65 }
66 vt.Row(label, md.InlineCode(sha1.SumHex(s)))
67 }
68 b.WriteString(vt.String() + "\n")
69 b.WriteString("The empty string and `abc` are the FIPS 180-1 answers every ")
70 b.WriteString("implementation is checked against. The last two rows differ ")
71 b.WriteString("by one letter and share no prefix: that avalanche is the ")
72 b.WriteString("property worth seeing.\n\n")
73
74 b.WriteString("## git object ids\n\n")
75 bt := ui.NewTable("content", "git hash-object")
76 for _, s := range blobs {
77 label := "_(empty file)_"
78 if s != "" {
79 // Show the trailing newline as an escape rather than the byte:
80 // a real newline would fold into the row above it.
81 label = md.InlineCode(strings.TrimSuffix(s, "\n") + "\\n")
82 }
83 bt.Row(label, md.InlineCode(sha1.GitBlobID(s)))
84 }
85 b.WriteString(bt.String() + "\n")
86 b.WriteString("`e69de29b` is the id of the empty file, the one hash a git ")
87 b.WriteString("user can recognise on sight. Every row matches ")
88 b.WriteString("`git hash-object`, because the header git prepends is the ")
89 b.WriteString("whole difference between a blob id and a content hash.\n\n")
90
91 b.WriteString("## HMAC-SHA1\n\n")
92 mt := ui.NewTable("key", "message", "hmac")
93 for _, m := range macs {
94 key, msg := "_(empty)_", "_(empty)_"
95 if m.key != "" {
96 key = md.InlineCode(m.key)
97 }
98 if m.msg != "" {
99 msg = md.InlineCode(m.msg)
100 }
101 mt.Row(key, msg, md.InlineCode(sha1.HMACHex(m.key, m.msg)))
102 }
103 b.WriteString(mt.String() + "\n")
104 b.WriteString("Row one is RFC 2202 test case 2. A key longer than the ")
105 b.WriteString("64-byte block is hashed down first, so a long key and its ")
106 b.WriteString("own digest are the same key.\n\n")
107
108 b.WriteString("## Hash your own\n\n")
109 b.WriteString("`/r/moul/x/daily/sha1demo/v0:your text here`\n\n")
110 b.WriteString("> ⚠️ **SHA-1 is broken as a collision-resistant hash.** ")
111 b.WriteString("SHAttered produced two different PDFs with the same digest ")
112 b.WriteString("in 2017, and a chosen-prefix collision followed in 2020. ")
113 b.WriteString("Never use it to decide that two documents are the same ")
114 b.WriteString("document. It is still the only correct answer where a format ")
115 b.WriteString("specifies it: git ids, HMAC-SHA1 in TOTP, legacy interop.\n")
116 return b.String()
117}
118
119// parseArg strips only the routing slash. The realm has no subpaths, so a
120// slash or a space in the rest is input to hash: a file path always has one.
121func parseArg(path string) string {
122 return strings.TrimPrefix(path, "/")
123}