fast-digest 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +51 -0
- data/README.md +136 -0
- data/lib/fast/digest/errors.rb +56 -0
- data/lib/fast/digest/fields.rb +499 -0
- data/lib/fast/digest/registry.rb +123 -0
- data/lib/fast/digest/version.rb +7 -0
- data/lib/fast/digest.rb +76 -0
- metadata +74 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b9d784e528a8398c648f1e773fbb27c70971b87e49abae0bb31217096878cad1
|
|
4
|
+
data.tar.gz: 45d2c4587e8f98adab6df6ca3fa03116d92dc476b7cfe5d9a588733a5b217f2d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 41060360216c4d836b71cc65a05a78905fc5c35fe49f4b7d76488adb115bebcab705376354a9173233ad2826c45d8fea1f23e7b6b7949c6c31da8cf77bfd144b
|
|
7
|
+
data.tar.gz: a42bcbdae98fbe6f953126207f4b13ac1a665b8b90653e5222e19dd269bcc8c749ea6ea37f251ac67107cdc1b3980ac5e530d4e987c891641c133eed60e17c34
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-11-26)
|
|
4
|
+
|
|
5
|
+
- Fields: `Fast::Digest::ContentDigest`, `ReprDigest`, `WantContentDigest` and
|
|
6
|
+
`WantReprDigest` — the four RFC 9530 fields as frozen value objects over
|
|
7
|
+
fast-sfv, each with the family mechanics: `parse` fail-closed (a malformed
|
|
8
|
+
field, or any member violating the field's constraint, is the entire field
|
|
9
|
+
ignored per RFC 9651 §2.2 — the empty value, the same answer an absent
|
|
10
|
+
field gives), `parse!` positioned strict truth (the wrapped Dictionary
|
|
11
|
+
failure, then the first offender raised at its member's VALUE offset in
|
|
12
|
+
the combined field value), `valid?` the boolean mirror, and strict
|
|
13
|
+
construction (byte-sequence values must be Strings tagged ASCII-8BIT,
|
|
14
|
+
preference values Integers 0..10, keys Structured Fields keys) with
|
|
15
|
+
canonical `to_s`, class-checked equality and the reader set (`[]` answering
|
|
16
|
+
the bare bytes so `parsed["sha-256"] == Digest::SHA256.digest(body)` holds
|
|
17
|
+
bytewise).
|
|
18
|
+
- The content-versus-representation distinction in the types (the RFC 3230
|
|
19
|
+
lesson of §1.3 and Appendix E): `ContentDigest.of` hashes content,
|
|
20
|
+
`ReprDigest.of` hashes representation data, and a ContentDigest never
|
|
21
|
+
equals a ReprDigest; the preference fields carry no compute helper
|
|
22
|
+
(§4's preferences are a hint, the selection policy is the caller's).
|
|
23
|
+
- Registry: `Fast::Digest::ALGORITHMS` — RFC 9530 §7.2 Table 2's eight
|
|
24
|
+
entries as frozen data (sha-512 and sha-256 Active, the other six
|
|
25
|
+
Deprecated, descriptions verbatim), with `registered?`/`status`/`active?`/
|
|
26
|
+
`deprecated?`/`computable?` lookups and unknown-key passthrough.
|
|
27
|
+
- Compute: `Fast::Digest.compute` — exactly the stdlib `Digest::SHA256`/
|
|
28
|
+
`SHA512`/`MD5`/`SHA1` bytes as frozen ASCII-8BIT Strings, hashed bytewise
|
|
29
|
+
whatever the input encoding; the four checksum entries (unixsum,
|
|
30
|
+
unixcksum, adler, crc32c) and unknown keys raise `Fast::Digest::Error`
|
|
31
|
+
naming the key — carried bytewise through the fields, never computed or
|
|
32
|
+
verified locally.
|
|
33
|
+
- Trailer support: the String-or-Array-of-lines entry points (RFC 9651
|
|
34
|
+
§4.2 combining, duplicates last-wins) plus the §6.4/§6.3 cautions in the
|
|
35
|
+
README (intermediaries may drop trailers; combining mangles what a
|
|
36
|
+
signature covers).
|
|
37
|
+
- Budgets: the allocation tests for the eight paths (integrity parse,
|
|
38
|
+
fail-closed, preference parse, emit, compute, `.of`, strict parse,
|
|
39
|
+
`valid?`), measured and frozen in the file's comment.
|
|
40
|
+
- Scaffold: the gemspec (one runtime dependency, fast-sfv), the README
|
|
41
|
+
front door, and this changelog. The documentation pass and this 0.1.0
|
|
42
|
+
release land together (docs are part of the product — the family's
|
|
43
|
+
release rule).
|
|
44
|
+
- Docs: the diátaxis docs tree under `docs/` (tutorial, four how-to
|
|
45
|
+
guides, two reference pages, four explanations), the README front door
|
|
46
|
+
with the documentation index, and the both-mode performance page from
|
|
47
|
+
one measurement session (interpreter and YJIT side by side, the
|
|
48
|
+
benchmark kit alongside). Every fenced ruby example was executed as
|
|
49
|
+
printed and every RFC citation checked against the RFC text by the
|
|
50
|
+
pipeline's independent review. The gemspec metadata completes with
|
|
51
|
+
`documentation_uri` pointing at the docs tree.
|
data/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# fast-digest
|
|
2
|
+
|
|
3
|
+
fast-digest is the RFC 9530 Digest Fields implementation for Ruby: Content-Digest, Repr-Digest, Want-Content-Digest and Want-Repr-Digest, built on fast-sfv.
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
|
|
7
|
+
Implements RFC 9530's four fields as RFC 9651 Dictionaries: the two Integrity fields — `Fast::Digest::ContentDigest` (§2, "a hashing algorithm applied to the actual message content") and `Fast::Digest::ReprDigest` (§3, "applied to the entire selected representation data") — as algorithm key => digest Byte Sequence, and the two preference fields — `WantContentDigest` and `WantReprDigest` (§4) — as algorithm key => Integer 0..10 (`lib/fast/digest.rb:19-30`; `rfc9530.txt:282-294,332-357,458-466`).
|
|
8
|
+
|
|
9
|
+
The hash-algorithm registry of §7.2 Table 2 pinned as frozen data (`Fast::Digest::ALGORITHMS`), unknown keys passing through; `Fast::Digest.compute` answers exactly the stdlib's bytes for sha-256, sha-512, md5 and sha (`lib/fast/digest/registry.rb:24-52,100-110`).
|
|
10
|
+
|
|
11
|
+
The lenient parse (`parse`) answers a recipient's view and never raises on field content; the strict parse (`parse!`) tells the truth with byte offsets; `to_s` and construction are always strict; results are frozen value objects; one runtime dependency (fast-sfv) plus the stdlib digest library (`lib/fast/digest.rb:32-58`; `fast-digest.gemspec:50`).
|
|
12
|
+
|
|
13
|
+
Pure Ruby, no OpenSSL, no zlib; the class names carry the content-versus-representation distinction (`lib/fast/digest.rb:26-30,71-73`).
|
|
14
|
+
|
|
15
|
+
MIT license; source at `https://github.com/jetpks/fast-http-fields` (`fast-digest.gemspec:21-23`).
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
gem install fast-digest
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
In a Gemfile:
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
gem "fast-digest"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Require path is `fast/digest`:
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
require "fast/digest"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Requires Ruby >= 3.4 and RubyGems >= 3.5.3; the only runtime requirement is fast-sfv — the stdlib digest library is a default gem, no dependency line (`fast-digest.gemspec:25-26,47-50`).
|
|
36
|
+
|
|
37
|
+
## Quickstart
|
|
38
|
+
|
|
39
|
+
The day-one byte-sequence comparison:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
require "fast/digest"
|
|
43
|
+
|
|
44
|
+
body = "{\"hello\": \"world\"}\n"
|
|
45
|
+
field = Fast::Digest::ContentDigest.parse("sha-256=:RK/0qy18MlBSVnWgjwz6lZEWjP/lF5HF9bvEF8FabDg=:")
|
|
46
|
+
|
|
47
|
+
field["sha-256"] == Digest::SHA256.digest(body) # => true
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`[]` answers the bare digest bytes as a frozen ASCII-8BIT String, so the bytewise comparison with the stdlib holds on day one (`lib/fast/digest/fields.rb:283-285`; `lib/fast/digest.rb:51-53`).
|
|
51
|
+
|
|
52
|
+
Compute and emit, one line each direction:
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
repr = Fast::Digest::ReprDigest.of("sha-256", body)
|
|
56
|
+
repr.to_s # => "sha-256=:RK/0qy18MlBSVnWgjwz6lZEWjP/lF5HF9bvEF8FabDg=:"
|
|
57
|
+
|
|
58
|
+
content = Fast::Digest::ContentDigest.new("sha-256" => Digest::SHA256.digest(body))
|
|
59
|
+
content.to_s # => "sha-256=:RK/0qy18MlBSVnWgjwz6lZEWjP/lF5HF9bvEF8FabDg=:"
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The preference field, §4's own example:
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
want = Fast::Digest::WantReprDigest.parse("sha-512=3, sha-256=10, unixsum=0")
|
|
66
|
+
want["sha-256"] # => 10
|
|
67
|
+
want["unixsum"] # => 0
|
|
68
|
+
want.to_s # => "sha-512=3, sha-256=10, unixsum=0"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Next: the [tutorial](docs/tutorials/digest-fields.md) for the guided path.
|
|
72
|
+
|
|
73
|
+
## Content or representation data — the class is the answer
|
|
74
|
+
|
|
75
|
+
RFC 9530 exists because RFC 3230's "instance" was read two ways (§1.3; `rfc9530.txt:219-226`; Appendix E: "Using content for the Digest field is an error that leads to interoperability problems" at `rfc9530.txt:1601-1604`).
|
|
76
|
+
|
|
77
|
+
- `ContentDigest` (§2) — content: the bytes the message conveys, what Content-Encoding chose.
|
|
78
|
+
- `ReprDigest` (§3) — selected representation data: what a range request does not deliver, the empty string when no representation data was sent (`rfc9530.txt:345-348`).
|
|
79
|
+
|
|
80
|
+
`ContentDigest.of` hashes content, `ReprDigest.of` hashes representation data, and a ContentDigest never equals a ReprDigest whatever their bytes — `==` is class-checked on top of the canonical wire form (`lib/fast/digest/fields.rb:318-326,420-422,454-456`). See [Content or representation data](docs/explanation/content-or-representation.md).
|
|
81
|
+
|
|
82
|
+
## The fail-closed parse
|
|
83
|
+
|
|
84
|
+
`parse` never raises on field content: a malformed field, or a field whose Dictionary carries ANY member violating the field's constraint (an Integrity member that is not a Byte Sequence, a preference member that is not an Integer 0..10), is the entire field ignored (RFC 9651 §2.2 — "When field-specific constraints are violated, the entire field is also ignored, unless the field definition defines other handling requirements", RFC 9530 defines none), answering the empty value object — the same answer an absent field gives. No member-level filtering: a valid member beside a bad one drops with the field (`rfc9651.txt:279-288`; `lib/fast/digest/fields.rb:158-176`).
|
|
85
|
+
|
|
86
|
+
"A recipient MAY ignore any or all digests" (§2/§3, `rfc9530.txt:315-317,379-381`) is caller policy, never the parse's.
|
|
87
|
+
|
|
88
|
+
`parse!` is the strict truth: the Dictionary failure wrapped with its message and byte offset, the first constraint offender raised at that member's VALUE offset in the combined field value; `valid?` mirrors it with a boolean (`lib/fast/digest/fields.rb:188-205`). See [Validate an upload's Content-Digest in Rack](docs/how-to-guides/validate-an-upload-content-digest.md).
|
|
89
|
+
|
|
90
|
+
## The registry as data
|
|
91
|
+
|
|
92
|
+
`Fast::Digest::ALGORITHMS` carries Table 2's eight entries in order with their statuses — sha-512 and sha-256 Active, md5, sha, unixsum, unixcksum, adler and crc32c Deprecated — and `registered?`/`status`/`active?`/`deprecated?`/`computable?` answer with unknown keys passing through: a future registration never breaks an old gem version (`lib/fast/digest/registry.rb:24-52,59-89`). The status is advisory (§5: a Deprecated algorithm "MAY be used to preserve integrity against corruption, but MUST NOT be used in a potentially adversarial setting", `rfc9530.txt:502-504`) — md5 and sha parse, emit and compute exactly like the Active ones.
|
|
93
|
+
|
|
94
|
+
`Fast::Digest.compute` answers the stdlib's exact bytes for the four computable algorithms; the four checksum entries are registered but never computed or verified here — their field values pass through bytewise, and `compute` raises `Fast::Digest::Error` naming the key (`lib/fast/digest/registry.rb:46-52,100-110`). See [The hash algorithm registry](docs/reference/algorithms.md) and [Algorithm agility and the registry's statuses](docs/explanation/algorithm-agility.md).
|
|
95
|
+
|
|
96
|
+
## Trailer fields and combining
|
|
97
|
+
|
|
98
|
+
All four fields can arrive as trailers (§2/§3: "MAY be merged into the header section", `rfc9530.txt:326-328,386-392`), and an HTTP message can carry the same field on several lines — the entry points accept an Array of field lines, combined with ", " per RFC 9651 §4.2 ("parsers MUST combine all field lines in the field name into one comma-separated field-value", `rfc9651.txt:1154-1156`), duplicates last-wins (`lib/fast/digest/fields.rb:209-214,269-278`):
|
|
99
|
+
|
|
100
|
+
```ruby
|
|
101
|
+
field = Fast::Digest::ContentDigest.parse(["sha-256=:AAAA:", "md5=:BBBB:"])
|
|
102
|
+
field.keys # => ["sha-256", "md5"]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Read §6.4 and §6.3 before trusting a trailer — intermediaries are allowed to drop trailers, eager processing of content before the trailer arrives prevents digest validation, and a signature that covers the field sees what the combining produced (`rfc9530.txt:627-645,621-625`). See [Emit digests while streaming](docs/how-to-guides/emit-digests-while-streaming.md) and [What digests do and do not protect](docs/explanation/security-posture.md).
|
|
106
|
+
|
|
107
|
+
## The error hierarchy
|
|
108
|
+
|
|
109
|
+
`Fast::Digest::Error` → `ParseError`/`SerializeError`, both carrying `#offset` — the family shape. The base `Error` is raised in its own right by `compute` and the `.of` helpers: a usage failure, neither a parse nor a serialization defect (`lib/fast/digest/errors.rb:5-16,28-35,47-54`). See [API reference](docs/reference/api.md).
|
|
110
|
+
|
|
111
|
+
## Documentation
|
|
112
|
+
|
|
113
|
+
Current release 0.1.0; changelog at [CHANGELOG.md](CHANGELOG.md).
|
|
114
|
+
|
|
115
|
+
### Tutorial
|
|
116
|
+
|
|
117
|
+
- [Your first Digest field](docs/tutorials/digest-fields.md) — One guided path: parse a Repr-Digest and verify it against the stdlib, compute and emit one, read a preference field, and meet the fail-closed empty value and the strict error.
|
|
118
|
+
|
|
119
|
+
### How-to guides
|
|
120
|
+
|
|
121
|
+
- [Validate an upload's Content-Digest in Rack](docs/how-to-guides/validate-an-upload-content-digest.md) — Read an upload's Content-Digest in a Rack-shaped env, parse it, compare against the received bytes, and handle absence, malformed input, and multiple lines.
|
|
122
|
+
- [Emit digests while streaming](docs/how-to-guides/emit-digests-while-streaming.md) — Hash content as it streams with the stdlib, build the field, send it as a header or a trailer, and know the trailer cautions.
|
|
123
|
+
- [Ask for digests with Want-Content-Digest and Want-Repr-Digest](docs/how-to-guides/request-digest-preferences.md) — Build and emit Want-Content-Digest / Want-Repr-Digest, read a peer's preferences, and run your own selection policy.
|
|
124
|
+
- [Choose hashing algorithms](docs/how-to-guides/choose-algorithms.md) — Choose algorithms with the registry lookups, emit multi-algorithm fields for transitions, and know what compute answers and refuses.
|
|
125
|
+
|
|
126
|
+
### Reference
|
|
127
|
+
|
|
128
|
+
- [API reference](docs/reference/api.md) — The complete surface: the four field classes, the parsing entry points, the value object, construction, the registry functions, and the errors.
|
|
129
|
+
- [The hash algorithm registry](docs/reference/algorithms.md) — The hash-algorithm registry: RFC 9530 §7.2 Table 2 as data, the lookups, computability, unknown-key passthrough, and how the registry is pinned.
|
|
130
|
+
|
|
131
|
+
### Explanation
|
|
132
|
+
|
|
133
|
+
- [Content or representation data — the RFC 3230 lesson](docs/explanation/content-or-representation.md) — Why content and selected representation are different digest inputs — the RFC 3230 lesson, the Appendix A/B worked examples, and what the type system enforces.
|
|
134
|
+
- [Algorithm agility and the registry's statuses](docs/explanation/algorithm-agility.md) — Why agility is designed in, what Active and Deprecated mean (and do not), the weakest-algorithm rule, and why the registry is pinned data.
|
|
135
|
+
- [What digests do and do not protect](docs/explanation/security-posture.md) — What digests do and do not protect: the on-path substitution attack, end-to-end integrity for data but not metadata, signatures, and trailers.
|
|
136
|
+
- [Performance and allocation budgets](docs/explanation/performance.md) — How the paths stay allocation-lean; the both-mode benchmark tables and the allocation budgets, with methodology and caveats.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module Digest
|
|
5
|
+
# The family root: every Fast::Digest failure descends from Error,
|
|
6
|
+
# so a caller can guard the whole surface with one rescue. Error is
|
|
7
|
+
# also raised in its own right, by the compute helpers
|
|
8
|
+
# (Fast::Digest.compute and the two .of constructors): a checksum
|
|
9
|
+
# algorithm the gem does not compute locally, or a key that is not
|
|
10
|
+
# in the RFC 9530 hash algorithm registry, is a USAGE failure —
|
|
11
|
+
# neither a parse defect (the field never parsed) nor a
|
|
12
|
+
# serialization defect (nothing was built) — so it answers the base
|
|
13
|
+
# class, and the message names the key and the reason. A wrong input
|
|
14
|
+
# type is a caller bug, not field content: it raises plain TypeError
|
|
15
|
+
# and never this class.
|
|
16
|
+
class Error < StandardError; end
|
|
17
|
+
|
|
18
|
+
# A Digest field this gem cannot read. Two shapes meet here: the
|
|
19
|
+
# underlying RFC 9651 Dictionary failed to parse — the failure is
|
|
20
|
+
# wrapped with its message and byte offset preserved — or, under the
|
|
21
|
+
# strict parse, a member broke the field's RFC 9530 constraint (an
|
|
22
|
+
# Integrity member that is not a Byte Sequence, a preference member
|
|
23
|
+
# that is not an Integer 0..10), and #offset is the byte position of
|
|
24
|
+
# that member's VALUE in the combined field value — the last
|
|
25
|
+
# occurrence of the key, the member Fast::SFV's last-wins kept. The
|
|
26
|
+
# lenient parse never raises it: a recipient that cannot read the
|
|
27
|
+
# field ignores it entirely (RFC 9651 §2.2).
|
|
28
|
+
class ParseError < Error
|
|
29
|
+
attr_reader :offset
|
|
30
|
+
|
|
31
|
+
def initialize(message, offset)
|
|
32
|
+
super(message)
|
|
33
|
+
@offset = offset
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# A value cannot go on the wire: the field under construction breaks
|
|
38
|
+
# a bound RFC 9530 states — a member key that is not a Structured
|
|
39
|
+
# Fields key (§1.4's Algorithm Keys are lowercase Dictionary keys),
|
|
40
|
+
# an Integrity value that is not a String tagged ASCII-8BIT (it
|
|
41
|
+
# would emit as a quoted sf-string, not a Byte Sequence), or a
|
|
42
|
+
# preference value that is not an Integer 0..10 (§4). #offset is the
|
|
43
|
+
# byte position in the output built so far — 0, because the check
|
|
44
|
+
# runs before anything is emitted; the whole emit path raises this
|
|
45
|
+
# class and never TypeError: on emit the wrongness is in the value
|
|
46
|
+
# being sent, which is field content, not a caller protocol bug.
|
|
47
|
+
class SerializeError < Error
|
|
48
|
+
attr_reader :offset
|
|
49
|
+
|
|
50
|
+
def initialize(message, offset)
|
|
51
|
+
super(message)
|
|
52
|
+
@offset = offset
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fast
|
|
4
|
+
module Digest
|
|
5
|
+
# The RFC 9651 value shapes a Digest field holds: every member of
|
|
6
|
+
# the Dictionary is a Fast::SFV::Item carrying the algorithm key's
|
|
7
|
+
# value and its parameters. Aliased here — private to this gem — so
|
|
8
|
+
# the checks and the construction walk read it by the short name.
|
|
9
|
+
Item = SFV::Item
|
|
10
|
+
private_constant :Item
|
|
11
|
+
|
|
12
|
+
# A Token is a String, and the Token class is its wire-type tag: the
|
|
13
|
+
# byte-sequence constraint has to refuse it by class or a
|
|
14
|
+
# binary-tagged Token would slip through the encoding check and emit
|
|
15
|
+
# as a token, not a Byte Sequence.
|
|
16
|
+
Token = SFV::Token
|
|
17
|
+
private_constant :Token
|
|
18
|
+
|
|
19
|
+
# The Byte Sequence tag: a String tagged ASCII-8BIT IS the Byte
|
|
20
|
+
# Sequence (RFC 9651 §3.3.5 — the encoding is the wire-type tag), so
|
|
21
|
+
# both the member constraint and the construction read the same
|
|
22
|
+
# fact, and a UTF-8 String would emit as a quoted sf-string.
|
|
23
|
+
BINARY = Encoding::ASCII_8BIT
|
|
24
|
+
private_constant :BINARY
|
|
25
|
+
|
|
26
|
+
# Items this gem builds carry no parameters, so they all share the
|
|
27
|
+
# one frozen empty Hash — the parse path's own shape.
|
|
28
|
+
EMPTY_PARAMS = {}.freeze
|
|
29
|
+
private_constant :EMPTY_PARAMS
|
|
30
|
+
|
|
31
|
+
# The members a fail-closed parse holds: none — the same answer an
|
|
32
|
+
# absent field gives (RFC 9651 §2.2, RFC 9530 §2).
|
|
33
|
+
EMPTY_MEMBERS = {}.freeze
|
|
34
|
+
private_constant :EMPTY_MEMBERS
|
|
35
|
+
|
|
36
|
+
# The strict parse's positioning walk: where each member's VALUE
|
|
37
|
+
# sits in the combined field value — the byte offset a semantic
|
|
38
|
+
# ParseError points at. The field value already parsed when this
|
|
39
|
+
# runs, so the walk only tracks the boundaries: members split on
|
|
40
|
+
# commas at inner-list depth zero outside quoted strings (RFC 9651
|
|
41
|
+
# §4.2's separators), a quoted string's commas and parens do not
|
|
42
|
+
# count (§4.2.5's escapes are skipped two bytes), and a member
|
|
43
|
+
# without "=" — the implicit Boolean true — offsets at its key's
|
|
44
|
+
# end, where its value would be written. Every key's offset is
|
|
45
|
+
# recorded, a later occurrence overwriting an earlier one, because
|
|
46
|
+
# Fast::SFV applies §4.2's last-wins before the checks read these
|
|
47
|
+
# offsets — the effective member is the one its value came from.
|
|
48
|
+
class Scan
|
|
49
|
+
# A 256-entry byte class: true for every byte of +spec+ — the
|
|
50
|
+
# byte-table shape Fast::SFV's own scanners walk with.
|
|
51
|
+
def self.byte_class(spec)
|
|
52
|
+
table = Array.new(256, false)
|
|
53
|
+
spec.each_byte { |byte| table[byte] = true }
|
|
54
|
+
table.freeze
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# A Dictionary key's first byte: lcalpha or "*" (§4.2.3.3 step 1).
|
|
58
|
+
KEY_FIRST = byte_class("abcdefghijklmnopqrstuvwxyz*")
|
|
59
|
+
# A key's continuation: lcalpha, DIGIT, "_", "-", "." or "*"
|
|
60
|
+
# (§4.2.3.3) — the class the walk reads a whole key with, the
|
|
61
|
+
# first byte being a member of it.
|
|
62
|
+
KEY_BODY = byte_class("abcdefghijklmnopqrstuvwxyz0123456789_-.*")
|
|
63
|
+
# The optional whitespace around a member separator (§4.2.1).
|
|
64
|
+
OWS = byte_class(" \t")
|
|
65
|
+
|
|
66
|
+
COMMA = 0x2C
|
|
67
|
+
EQUALS = 0x3D
|
|
68
|
+
LPAREN = 0x28
|
|
69
|
+
RPAREN = 0x29
|
|
70
|
+
DQUOTE = 0x22
|
|
71
|
+
BACKSLASH = 0x5C
|
|
72
|
+
|
|
73
|
+
def initialize(combined)
|
|
74
|
+
@combined = combined
|
|
75
|
+
@size = combined.bytesize
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# One walk answers the value offset of every key the Dictionary
|
|
79
|
+
# parsed — the LAST one seen for each, since the effective member
|
|
80
|
+
# is the last one Fast::SFV kept.
|
|
81
|
+
def offsets
|
|
82
|
+
@offsets = {}
|
|
83
|
+
index = 0
|
|
84
|
+
index = member_at(index) while index < @size
|
|
85
|
+
@offsets
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
# Walks one member from +index+ and answers the index the next
|
|
91
|
+
# member starts at: leading OWS, the key, the value — its offset
|
|
92
|
+
# recorded — then the separator. A member that is not followed by
|
|
93
|
+
# a comma ends the walk.
|
|
94
|
+
def member_at(index)
|
|
95
|
+
index += 1 while index < @size && OWS[@combined.getbyte(index)]
|
|
96
|
+
key_start = index
|
|
97
|
+
index += 1 while index < @size && KEY_BODY[@combined.getbyte(index)]
|
|
98
|
+
key_end = index
|
|
99
|
+
value_offset = index
|
|
100
|
+
if @combined.getbyte(index) == EQUALS
|
|
101
|
+
index += 1
|
|
102
|
+
value_offset = index
|
|
103
|
+
index = value_end(index)
|
|
104
|
+
end
|
|
105
|
+
record(key_start, key_end - key_start, value_offset)
|
|
106
|
+
index += 1 while index < @size && OWS[@combined.getbyte(index)]
|
|
107
|
+
return @size unless index < @size && @combined.getbyte(index) == COMMA
|
|
108
|
+
|
|
109
|
+
index + 1
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Records one member's value offset under its key — the bytes the
|
|
113
|
+
# Dictionary itself parsed the same key from, so an equal key
|
|
114
|
+
# overwrites (the last-wins member).
|
|
115
|
+
def record(key_start, key_size, value_offset)
|
|
116
|
+
@offsets[@combined.byteslice(key_start, key_size)] = value_offset
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# The index just past the member VALUE starting at +index+:
|
|
120
|
+
# quoted strings (§4.2.5 — a DQUOTE opens, a backslash escapes the
|
|
121
|
+
# next byte, a DQUOTE closes) and inner-list depth (§4.2.1.1's
|
|
122
|
+
# parens) are tracked, and a comma at depth zero outside a string
|
|
123
|
+
# — the §4.2 member boundary — stops the walk.
|
|
124
|
+
def value_end(index)
|
|
125
|
+
depth = 0
|
|
126
|
+
quoted = false
|
|
127
|
+
while index < @size
|
|
128
|
+
byte = @combined.getbyte(index)
|
|
129
|
+
if quoted
|
|
130
|
+
index += byte == BACKSLASH ? 2 : 1
|
|
131
|
+
quoted = false if byte == DQUOTE
|
|
132
|
+
else
|
|
133
|
+
case byte
|
|
134
|
+
when DQUOTE then quoted = true
|
|
135
|
+
when LPAREN then depth += 1
|
|
136
|
+
when RPAREN then depth -= 1
|
|
137
|
+
when COMMA then return index if depth.zero?
|
|
138
|
+
end
|
|
139
|
+
index += 1
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
index
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
private_constant :Scan
|
|
146
|
+
|
|
147
|
+
# The shared machinery of the four Digest fields: one RFC 9651
|
|
148
|
+
# Dictionary parse through Fast::SFV, the member constraint the
|
|
149
|
+
# field's own RFC 9530 section states, and the frozen value object
|
|
150
|
+
# that holds what parsed. The four subclasses each declare their
|
|
151
|
+
# field name's constraint — a Byte Sequence for the two Integrity
|
|
152
|
+
# fields (§2/§3), an Integer 0..10 for the two preference fields
|
|
153
|
+
# (§4) — through value_ok? and value_rule; everything else (the
|
|
154
|
+
# recipient semantics, the strict truth, the readers, the canonical
|
|
155
|
+
# emit) is this class.
|
|
156
|
+
class Field
|
|
157
|
+
class << self
|
|
158
|
+
# RFC 9530 §2/§3/§4, the recipient semantics — never raises on
|
|
159
|
+
# field content. A malformed field, or a field whose Dictionary
|
|
160
|
+
# carries ANY member violating the field's constraint, is the
|
|
161
|
+
# ENTIRE field ignored (RFC 9651 §2.2: "When field-specific
|
|
162
|
+
# constraints are violated, the entire field is also ignored,
|
|
163
|
+
# unless the field definition defines other handling
|
|
164
|
+
# requirements" — RFC 9530 defines none): the empty value
|
|
165
|
+
# object, the same answer an absent field gives. There is no
|
|
166
|
+
# member-level filtering: a valid member beside a bad one drops
|
|
167
|
+
# with the field. "A recipient MAY ignore any or all digests"
|
|
168
|
+
# (§2/§3) is CALLER policy, never the parse's. A String is one
|
|
169
|
+
# field value; an Array is the field's lines, combined with ",
|
|
170
|
+
# " (RFC 9651 §4.2). A wrong input type is a caller bug and
|
|
171
|
+
# raises TypeError.
|
|
172
|
+
def parse(input)
|
|
173
|
+
parse!(input)
|
|
174
|
+
rescue ParseError
|
|
175
|
+
from_members(EMPTY_MEMBERS)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Strict truth. The RFC 9651 Dictionary failure is wrapped with
|
|
179
|
+
# its message and byte offset preserved; then the member
|
|
180
|
+
# constraint applied to the effective (last-wins) Dictionary —
|
|
181
|
+
# the first offender in Dictionary order raises ParseError
|
|
182
|
+
# naming the key and pointing at that member's VALUE in the
|
|
183
|
+
# combined field value (a member without "=", the implicit
|
|
184
|
+
# Boolean true, at its key's end). Unknown algorithm keys are
|
|
185
|
+
# valid members and pass through untouched, parameters riding
|
|
186
|
+
# along (§2.3 of RFC 9651). TypeError for a wrong input type,
|
|
187
|
+
# as parse.
|
|
188
|
+
def parse!(input)
|
|
189
|
+
dict = SFV.parse_dict(input)
|
|
190
|
+
check_members!(dict, combined(input))
|
|
191
|
+
from_members(dict)
|
|
192
|
+
rescue SFV::ParseError => e
|
|
193
|
+
raise ParseError.new(e.message, e.offset)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# The boolean mirror: true exactly when parse! returns — false
|
|
197
|
+
# for malformed content, for constraint violations, and for a
|
|
198
|
+
# wrong input type alike (a caller bug is not field content
|
|
199
|
+
# either). The empty Dictionary parses, so "" is true.
|
|
200
|
+
def valid?(input)
|
|
201
|
+
parse!(input)
|
|
202
|
+
true
|
|
203
|
+
rescue TypeError, ParseError
|
|
204
|
+
false
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
private
|
|
208
|
+
|
|
209
|
+
# The field value the scan positions into: the lines joined with
|
|
210
|
+
# ", " — the same RFC 9651 §4.2 combining Fast::SFV.parse_dict
|
|
211
|
+
# applies — a String as given.
|
|
212
|
+
def combined(input)
|
|
213
|
+
input.is_a?(Array) ? input.join(", ") : input
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# The member constraint over a parsed Dictionary: every member
|
|
217
|
+
# an Item whose value satisfies the field's shape — §2/§3's Byte
|
|
218
|
+
# Sequence, §4's Integer 0..10 — the "field-specific
|
|
219
|
+
# constraints" whose violation ignores the whole field under
|
|
220
|
+
# parse. The first offender in Dictionary order is raised at its
|
|
221
|
+
# value's offset.
|
|
222
|
+
def check_members!(dict, field_value)
|
|
223
|
+
return if dict.empty?
|
|
224
|
+
|
|
225
|
+
offsets = Scan.new(field_value).offsets
|
|
226
|
+
dict.each do |key, member|
|
|
227
|
+
next if member.is_a?(Item) && value_ok?(member.value)
|
|
228
|
+
|
|
229
|
+
raise ParseError.new("#{key}: #{value_rule} (got #{shown(member)})", offsets[key])
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# What a semantic error shows of the offending member: the bare
|
|
234
|
+
# value when it is an Item (wrong type, out of range), the
|
|
235
|
+
# member's class when it is not an Item at all — an inner list.
|
|
236
|
+
def shown(member)
|
|
237
|
+
member.is_a?(Item) ? member.value : member.class
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# The value object behind a Dictionary that already passed the
|
|
241
|
+
# checks: the members held as parsed, no re-validation, the
|
|
242
|
+
# Items' parameters preserved.
|
|
243
|
+
def from_members(members)
|
|
244
|
+
field = allocate
|
|
245
|
+
field.send(:hold, members)
|
|
246
|
+
field
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# The held members: the algorithm key => Fast::SFV::Item view,
|
|
251
|
+
# frozen and ordered — the parse's order, or the construction
|
|
252
|
+
# Hash's. The bare values are the #[] view.
|
|
253
|
+
attr_reader :members
|
|
254
|
+
|
|
255
|
+
# The algorithm keys in held order, frozen.
|
|
256
|
+
attr_reader :keys
|
|
257
|
+
|
|
258
|
+
# Strict construction — a sender never puts garbage on the wire.
|
|
259
|
+
# +members+ is a Hash of algorithm key => bare value: a String
|
|
260
|
+
# tagged ASCII-8BIT for the Integrity fields (the encoding IS the
|
|
261
|
+
# Byte Sequence tag — a UTF-8 String would emit as a quoted
|
|
262
|
+
# sf-string, so it is refused), an Integer 0..10 for the
|
|
263
|
+
# preference fields. Keys are Structured Fields keys (§1.4: the
|
|
264
|
+
# Algorithm Keys are lowercase Dictionary keys, so "SHA-256" is
|
|
265
|
+
# refused). Value Strings are frozen IN PLACE as part of the value
|
|
266
|
+
# object — pass a copy if the buffer is reused. Anything else
|
|
267
|
+
# raises SerializeError before a member is built. The empty Hash
|
|
268
|
+
# builds the empty value: a sender omits the field.
|
|
269
|
+
def initialize(members = {})
|
|
270
|
+
raise SerializeError.new("members are a Hash (got #{members.class})", 0) unless members.is_a?(Hash)
|
|
271
|
+
|
|
272
|
+
held = {}
|
|
273
|
+
members.each do |key, value|
|
|
274
|
+
check_member(key, value)
|
|
275
|
+
held[key] = Item.new(value.freeze, EMPTY_PARAMS)
|
|
276
|
+
end
|
|
277
|
+
hold(held)
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# The bare value of one algorithm key — the digest bytes, or the
|
|
281
|
+
# preference Integer; nil for an absent key. The day-one idiom
|
|
282
|
+
# holds bytewise: parsed["sha-256"] == Digest::SHA256.digest(body).
|
|
283
|
+
def [](key)
|
|
284
|
+
@members[key]&.value
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Does the field hold a member under +key+?
|
|
288
|
+
def key?(key)
|
|
289
|
+
@members.key?(key)
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
# How many members the field holds.
|
|
293
|
+
def size
|
|
294
|
+
@members.size
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# No members held — an absent or fail-closed field.
|
|
298
|
+
def empty?
|
|
299
|
+
@members.empty?
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Yields each held algorithm key and its bare value.
|
|
303
|
+
def each
|
|
304
|
+
@members.each { |key, item| yield key, item.value }
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# The canonical wire form: the RFC 9651 §4.1.2 Dictionary
|
|
308
|
+
# serialization of the held members, "" when none are held — a
|
|
309
|
+
# sender omits the field entirely, the same reason
|
|
310
|
+
# Fast::SFV.serialize_dict answers nil for the empty Dictionary.
|
|
311
|
+
# Never raises: construction validated everything, and a parsed
|
|
312
|
+
# value re-emits its canonical form (non-canonical spacing
|
|
313
|
+
# normalizes, the erratum's doubled padding re-emits single).
|
|
314
|
+
def to_s
|
|
315
|
+
SFV.serialize_dict(@members) || ""
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# Canonical-wire-form equality WITHIN the field class:
|
|
319
|
+
# other.instance_of?(self.class) && to_s == other.to_s. A
|
|
320
|
+
# ContentDigest never equals a ReprDigest, whatever their bytes —
|
|
321
|
+
# the class is the content-versus-representation distinction the
|
|
322
|
+
# RFC 3230 history demands (§1.3, Appendix E).
|
|
323
|
+
def ==(other)
|
|
324
|
+
other.instance_of?(self.class) && to_s == other.to_s
|
|
325
|
+
end
|
|
326
|
+
alias eql? ==
|
|
327
|
+
|
|
328
|
+
# Consistent with eql? — the canonical form's hash.
|
|
329
|
+
def hash
|
|
330
|
+
to_s.hash
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
def inspect
|
|
334
|
+
wire = to_s
|
|
335
|
+
wire.empty? ? "#<#{self.class.name}>" : "#<#{self.class.name} #{wire}>"
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
private
|
|
339
|
+
|
|
340
|
+
# The one place the held state is written, then frozen: the
|
|
341
|
+
# members Hash (frozen already on the parse path) and the keys
|
|
342
|
+
# view derived from it.
|
|
343
|
+
def hold(members)
|
|
344
|
+
@members = members.freeze
|
|
345
|
+
@keys = members.keys.freeze
|
|
346
|
+
freeze
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
# Construction's member check: the key is a Structured Fields key,
|
|
350
|
+
# the value the field's shape — the same constraint the parse
|
|
351
|
+
# applies, read at the boundary so no unvalidated value reaches
|
|
352
|
+
# the wire.
|
|
353
|
+
def check_member(key, value)
|
|
354
|
+
check_key(key)
|
|
355
|
+
return if self.class.send(:value_ok?, value)
|
|
356
|
+
|
|
357
|
+
raise SerializeError.new("#{key}: #{self.class.send(:value_rule)} (got #{value.inspect})", 0)
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
# The Algorithm Key grammar, RFC 9651 §4.2.3.3 read from the value
|
|
361
|
+
# side: a String whose first byte is lcalpha or "*" and whose
|
|
362
|
+
# bytes continue in lcalpha, DIGIT, "_", "-", "." or "*".
|
|
363
|
+
def check_key(key)
|
|
364
|
+
return if valid_key?(key)
|
|
365
|
+
|
|
366
|
+
raise SerializeError.new("#{key.inspect} is not a Structured Fields key", 0)
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# The walk behind check_key — manual and allocation-free, the
|
|
370
|
+
# shape the substrate's own key parse uses.
|
|
371
|
+
def valid_key?(key)
|
|
372
|
+
return false unless key.is_a?(String)
|
|
373
|
+
|
|
374
|
+
size = key.bytesize
|
|
375
|
+
return false if size.zero? || !Scan::KEY_FIRST[key.getbyte(0)]
|
|
376
|
+
|
|
377
|
+
index = 1
|
|
378
|
+
while index < size
|
|
379
|
+
return false unless Scan::KEY_BODY[key.getbyte(index)]
|
|
380
|
+
|
|
381
|
+
index += 1
|
|
382
|
+
end
|
|
383
|
+
true
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
private_constant :Field
|
|
387
|
+
|
|
388
|
+
# RFC 9530 §2, the Content-Digest field: a Dictionary whose "value
|
|
389
|
+
# is a Byte Sequence that conveys an encoded version of the byte
|
|
390
|
+
# output produced by the digest calculation" — a digest "calculated
|
|
391
|
+
# using a hashing algorithm applied to the actual message content".
|
|
392
|
+
# The whole RFC 3230 lesson lives in this class's name (§1.3,
|
|
393
|
+
# Appendix E): content is what this field digests, and nothing else.
|
|
394
|
+
# Content-Digest can be sent in a trailer (§2: "In this case,
|
|
395
|
+
# Content-Digest MAY be merged into the header section") — the Array
|
|
396
|
+
# entry point is the combining contract, and the caller owns what
|
|
397
|
+
# §6.3's signature cautions then cover.
|
|
398
|
+
class ContentDigest < Field
|
|
399
|
+
# §2's member constraint: a Byte Sequence — a String tagged
|
|
400
|
+
# ASCII-8BIT and not a Token (the class is the token's tag), the
|
|
401
|
+
# exact shape Fast::SFV emits as ":base64:".
|
|
402
|
+
def self.value_ok?(value)
|
|
403
|
+
value.is_a?(String) && !value.is_a?(Token) && value.encoding == BINARY
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
# The wording a ParseError or SerializeError carries for a member
|
|
407
|
+
# that breaks the constraint.
|
|
408
|
+
def self.value_rule
|
|
409
|
+
"a Content-Digest value is a Byte Sequence: a String tagged Encoding::ASCII_8BIT"
|
|
410
|
+
end
|
|
411
|
+
private_class_method :value_ok?, :value_rule
|
|
412
|
+
|
|
413
|
+
# Computes a digest of the CONTENT bytes and constructs the field
|
|
414
|
+
# from it — §2's "actual message content", what Content-Encoding
|
|
415
|
+
# chose. .of on the two Integrity fields is the anti-confusion
|
|
416
|
+
# naming: this one hashes content, ReprDigest.of hashes
|
|
417
|
+
# representation data, and the class-checked equality keeps the
|
|
418
|
+
# two apart even with identical bytes. Raises Error for a key
|
|
419
|
+
# Fast::Digest.compute refuses, TypeError for a non-String input.
|
|
420
|
+
def self.of(algorithm, content)
|
|
421
|
+
new(algorithm => Fast::Digest.compute(algorithm, content))
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# RFC 9530 §3, the Repr-Digest field: the same Dictionary shape,
|
|
426
|
+
# "applied to the entire selected representation data" — what a
|
|
427
|
+
# range request does not deliver, what the selected representation
|
|
428
|
+
# is after Content-Encoding, the empty string when no representation
|
|
429
|
+
# data was sent (§3, §6.3). Repr-Digest can be sent in a trailer
|
|
430
|
+
# (§3: "In this case, Repr-Digest MAY be merged into the header
|
|
431
|
+
# section").
|
|
432
|
+
class ReprDigest < Field
|
|
433
|
+
# §3's member constraint: a Byte Sequence — a String tagged
|
|
434
|
+
# ASCII-8BIT and not a Token, the exact shape Fast::SFV emits as
|
|
435
|
+
# ":base64:".
|
|
436
|
+
def self.value_ok?(value)
|
|
437
|
+
value.is_a?(String) && !value.is_a?(Token) && value.encoding == BINARY
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
# The wording a ParseError or SerializeError carries for a member
|
|
441
|
+
# that breaks the constraint.
|
|
442
|
+
def self.value_rule
|
|
443
|
+
"a Repr-Digest value is a Byte Sequence: a String tagged Encoding::ASCII_8BIT"
|
|
444
|
+
end
|
|
445
|
+
private_class_method :value_ok?, :value_rule
|
|
446
|
+
|
|
447
|
+
# Computes a digest of the REPRESENTATION DATA bytes and
|
|
448
|
+
# constructs the field from it — §3's "entire selected
|
|
449
|
+
# representation data", what the RFC 3230 "instance" always meant
|
|
450
|
+
# (§1.3, Appendix E: "Using content for the Digest field is an
|
|
451
|
+
# error that leads to interoperability problems"). Raises Error
|
|
452
|
+
# for a key Fast::Digest.compute refuses, TypeError for a
|
|
453
|
+
# non-String input.
|
|
454
|
+
def self.of(algorithm, representation_data)
|
|
455
|
+
new(algorithm => Fast::Digest.compute(algorithm, representation_data))
|
|
456
|
+
end
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
# RFC 9530 §4, the Want-Content-Digest field: a Dictionary whose
|
|
460
|
+
# "value is an Integer that conveys an ascending, relative, weighted
|
|
461
|
+
# preference. It must be in the range 0 to 10 inclusive. 1 is the
|
|
462
|
+
# least preferred, 10 is the most preferred, and a value of 0 means
|
|
463
|
+
# 'not acceptable'." A hint only — "It is not a protocol error if
|
|
464
|
+
# preferences are ignored" — so this class carries no selection
|
|
465
|
+
# helper and no compute: the selection policy is the caller's.
|
|
466
|
+
class WantContentDigest < Field
|
|
467
|
+
# §4's member constraint: an Integer 0..10.
|
|
468
|
+
def self.value_ok?(value)
|
|
469
|
+
value.is_a?(Integer) && value.between?(0, 10)
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
# The wording a ParseError or SerializeError carries for a member
|
|
473
|
+
# that breaks the constraint.
|
|
474
|
+
def self.value_rule
|
|
475
|
+
"a Want-Content-Digest value is an Integer 0..10"
|
|
476
|
+
end
|
|
477
|
+
private_class_method :value_ok?, :value_rule
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
# RFC 9530 §4, the Want-Repr-Digest field: the same Integer 0..10
|
|
481
|
+
# preference Dictionary, soliciting the Repr-Digest field — "the
|
|
482
|
+
# sender would like to receive (via the Repr-Digest field) a
|
|
483
|
+
# representation digest on messages associated with the request URI
|
|
484
|
+
# and representation metadata."
|
|
485
|
+
class WantReprDigest < Field
|
|
486
|
+
# §4's member constraint: an Integer 0..10.
|
|
487
|
+
def self.value_ok?(value)
|
|
488
|
+
value.is_a?(Integer) && value.between?(0, 10)
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
# The wording a ParseError or SerializeError carries for a member
|
|
492
|
+
# that breaks the constraint.
|
|
493
|
+
def self.value_rule
|
|
494
|
+
"a Want-Repr-Digest value is an Integer 0..10"
|
|
495
|
+
end
|
|
496
|
+
private_class_method :value_ok?, :value_rule
|
|
497
|
+
end
|
|
498
|
+
end
|
|
499
|
+
end
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The stdlib digest library: the second oracle of this gem, beside RFC
|
|
4
|
+
# 9530. Required here, at the top, because the module below is named
|
|
5
|
+
# Fast::Digest — inside it a bare Digest resolves to Fast::Digest, so
|
|
6
|
+
# every stdlib reference in this file spells the leading scope operator
|
|
7
|
+
# ::Digest::SHA256.
|
|
8
|
+
require "digest"
|
|
9
|
+
|
|
10
|
+
module Fast
|
|
11
|
+
module Digest
|
|
12
|
+
# RFC 9530 §7.2 Table 2 — the initial "Hash Algorithms for HTTP
|
|
13
|
+
# Digest Fields" registry, pinned as data: a gem that pins its RFC's
|
|
14
|
+
# registry at publication passes unknown values through, so a future
|
|
15
|
+
# registration never breaks an old gem version. Each value is a
|
|
16
|
+
# frozen {"status" => ..., "description" => ...}; the descriptions
|
|
17
|
+
# are Table 2's own text, its bracket citation keys pointing into
|
|
18
|
+
# RFC 9530 §8's reference list. §5: the status "is intended to
|
|
19
|
+
# provide an aid to implementation selection" — Active for sha-512
|
|
20
|
+
# and sha-256, Deprecated for the other six ("MAY be used to
|
|
21
|
+
# preserve integrity against corruption, but MUST NOT be used in a
|
|
22
|
+
# potentially adversarial setting") — and it never gates a parse, an
|
|
23
|
+
# emit or a computation here.
|
|
24
|
+
ALGORITHMS = {
|
|
25
|
+
"sha-512" => { "status" => "Active", "description" => "The SHA-512 algorithm." }.freeze,
|
|
26
|
+
"sha-256" => { "status" => "Active", "description" => "The SHA-256 algorithm." }.freeze,
|
|
27
|
+
"md5" => {
|
|
28
|
+
"status" => "Deprecated",
|
|
29
|
+
"description" => "The MD5 algorithm. It is vulnerable to collision attacks; see [NO-MD5] and [CMU-836068]"
|
|
30
|
+
}.freeze,
|
|
31
|
+
"sha" => {
|
|
32
|
+
"status" => "Deprecated",
|
|
33
|
+
"description" => "The SHA-1 algorithm. It is vulnerable to collision attacks; see [NO-SHA] and [IACR-2020-014]"
|
|
34
|
+
}.freeze,
|
|
35
|
+
"unixsum" => {
|
|
36
|
+
"status" => "Deprecated", "description" => 'The algorithm used by the UNIX "sum" command.'
|
|
37
|
+
}.freeze,
|
|
38
|
+
"unixcksum" => {
|
|
39
|
+
"status" => "Deprecated",
|
|
40
|
+
"description" => 'The algorithm used by the UNIX "cksum" command.'
|
|
41
|
+
}.freeze,
|
|
42
|
+
"adler" => { "status" => "Deprecated", "description" => "The ADLER32 algorithm." }.freeze,
|
|
43
|
+
"crc32c" => { "status" => "Deprecated", "description" => "The CRC32c algorithm." }.freeze
|
|
44
|
+
}.freeze
|
|
45
|
+
|
|
46
|
+
# The keys Fast::Digest.compute answers: the four algorithms the
|
|
47
|
+
# stdlib digest library computes. Table 2's four checksum entries
|
|
48
|
+
# (unixsum, unixcksum, adler, crc32c) are registered but carry no
|
|
49
|
+
# local implementation, so their field values pass through bytewise
|
|
50
|
+
# while compute refuses them, naming the key: the RFC 9530 registry
|
|
51
|
+
# is the only source of their digest values.
|
|
52
|
+
COMPUTABLE = %w[sha-256 sha-512 md5 sha].freeze
|
|
53
|
+
private_constant :COMPUTABLE
|
|
54
|
+
|
|
55
|
+
module_function
|
|
56
|
+
|
|
57
|
+
# Is +key+ one of Table 2's eight Algorithm Keys? An unknown key
|
|
58
|
+
# answers false, and passes through the fields untouched either way.
|
|
59
|
+
def registered?(key)
|
|
60
|
+
ALGORITHMS.key?(key)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Table 2's Status for +key+ — "Active" or "Deprecated" — or nil for
|
|
64
|
+
# a key the registry does not carry.
|
|
65
|
+
def status(key)
|
|
66
|
+
entry = ALGORITHMS[key]
|
|
67
|
+
entry && entry["status"]
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# §5: "Algorithms with a status value of 'Active' are suitable for
|
|
71
|
+
# many purposes and it is RECOMMENDED that applications use these
|
|
72
|
+
# algorithms." False for unknown keys.
|
|
73
|
+
def active?(key)
|
|
74
|
+
status(key) == "Active"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# §5: a Deprecated algorithm "MAY be used to preserve integrity
|
|
78
|
+
# against corruption, but MUST NOT be used in a potentially
|
|
79
|
+
# adversarial setting." False for unknown keys.
|
|
80
|
+
def deprecated?(key)
|
|
81
|
+
status(key) == "Deprecated"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Can Fast::Digest.compute answer +key+? True for the four stdlib
|
|
85
|
+
# algorithms, false for the four checksum entries and for unknown
|
|
86
|
+
# keys.
|
|
87
|
+
def computable?(key)
|
|
88
|
+
COMPUTABLE.include?(key)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# The digest of +input+ under +key+ — exactly the stdlib's bytes,
|
|
92
|
+
# as a frozen ASCII-8BIT String (the Byte Sequence tag: the day-one
|
|
93
|
+
# comparison parsed["sha-256"] == Digest::SHA256.digest(body) holds
|
|
94
|
+
# bytewise). The input is hashed bytewise whatever its encoding; a
|
|
95
|
+
# non-String input is a caller bug and raises TypeError. A
|
|
96
|
+
# registered checksum entry (unixsum, unixcksum, adler, crc32c) or
|
|
97
|
+
# an unregistered key raises Fast::Digest::Error naming the key and
|
|
98
|
+
# the reason: a usage failure, neither a parse nor a serialization
|
|
99
|
+
# defect.
|
|
100
|
+
def compute(key, input)
|
|
101
|
+
raise TypeError, "a String is required to digest (got #{input.class})" unless input.is_a?(String)
|
|
102
|
+
|
|
103
|
+
case key
|
|
104
|
+
when "sha-256" then ::Digest::SHA256.digest(input).freeze
|
|
105
|
+
when "sha-512" then ::Digest::SHA512.digest(input).freeze
|
|
106
|
+
when "md5" then ::Digest::MD5.digest(input).freeze
|
|
107
|
+
when "sha" then ::Digest::SHA1.digest(input).freeze
|
|
108
|
+
else raise Error, uncomputable_reason(key)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Why compute cannot answer +key+: a Table 2 checksum entry this gem
|
|
113
|
+
# deliberately does not compute locally, or a key RFC 9530's
|
|
114
|
+
# registry does not carry.
|
|
115
|
+
def uncomputable_reason(key)
|
|
116
|
+
return "#{key} is not in the RFC 9530 hash algorithm registry" unless ALGORITHMS.key?(key)
|
|
117
|
+
|
|
118
|
+
"#{key} is registered but not computable here: construct the field with its explicit digest value"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
private_class_method :uncomputable_reason
|
|
122
|
+
end
|
|
123
|
+
end
|
data/lib/fast/digest.rb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The substrate: RFC 9530's four fields are RFC 9651 Dictionaries, so
|
|
4
|
+
# this gem is a fast-sfv consumer, never a parser of its own. The
|
|
5
|
+
# stdlib digest library is the second half of the foundation (required
|
|
6
|
+
# by lib/fast/digest/registry.rb).
|
|
7
|
+
require "fast/sfv"
|
|
8
|
+
|
|
9
|
+
require_relative "digest/version"
|
|
10
|
+
require_relative "digest/errors"
|
|
11
|
+
require_relative "digest/registry"
|
|
12
|
+
require_relative "digest/fields"
|
|
13
|
+
|
|
14
|
+
module Fast
|
|
15
|
+
# RFC 9530 Digest Fields: the four HTTP integrity fields, the
|
|
16
|
+
# hash-algorithm registry as data, and digest computing over the
|
|
17
|
+
# stdlib digest library.
|
|
18
|
+
#
|
|
19
|
+
# The two Integrity fields are Dictionaries of algorithm key =>
|
|
20
|
+
# digest Byte Sequence — ContentDigest (§2: "a hashing algorithm
|
|
21
|
+
# applied to the actual message content") and ReprDigest (§3:
|
|
22
|
+
# "applied to the entire selected representation data"). The two
|
|
23
|
+
# preference fields are Dictionaries of algorithm key => Integer
|
|
24
|
+
# 0..10 — WantContentDigest and WantReprDigest (§4: "1 is the least
|
|
25
|
+
# preferred, 10 is the most preferred, and a value of 0 means 'not
|
|
26
|
+
# acceptable'"). The class names carry the content-versus-
|
|
27
|
+
# representation distinction the RFC 3230 history demands (§1.3,
|
|
28
|
+
# Appendix E), the .of helpers name it again on the compute side, and
|
|
29
|
+
# class-checked equality keeps a ContentDigest from ever equaling a
|
|
30
|
+
# ReprDigest.
|
|
31
|
+
#
|
|
32
|
+
# parse is the recipient's view and never raises on field content: a
|
|
33
|
+
# malformed field, or a field whose Dictionary carries any member
|
|
34
|
+
# violating the field's constraint, is the ENTIRE field ignored (RFC
|
|
35
|
+
# 9651 §2.2 — RFC 9530 defines no other handling), answering the
|
|
36
|
+
# empty value object, the same answer an absent field gives. No
|
|
37
|
+
# member-level filtering: a valid member beside a bad one drops with
|
|
38
|
+
# the field. Unknown algorithm keys pass through as parsed, SFV
|
|
39
|
+
# parameters ride along, and "" is the valid empty Dictionary. A
|
|
40
|
+
# String is one field value; an Array is the field's lines, combined
|
|
41
|
+
# with ", " (RFC 9651 §4.2) — the trailer-fields entry point, §2/§3's
|
|
42
|
+
# "MAY be merged into the header section". A wrong input type is a
|
|
43
|
+
# caller bug and raises TypeError.
|
|
44
|
+
#
|
|
45
|
+
# parse! is strict truth: the Dictionary failure wrapped with its
|
|
46
|
+
# message and byte offset, then the member constraints — the first
|
|
47
|
+
# offender raises ParseError positioned at that member's VALUE in the
|
|
48
|
+
# combined field value. valid? answers the same question with a
|
|
49
|
+
# boolean and never raises.
|
|
50
|
+
#
|
|
51
|
+
# The answer is a frozen value object: members (the Fast::SFV::Item
|
|
52
|
+
# view), [] (the bare value — the digest bytes, so the day-one idiom
|
|
53
|
+
# parsed["sha-256"] == Digest::SHA256.digest(body) holds bytewise),
|
|
54
|
+
# the canonical to_s ("" when nothing is held), canonical-wire-form
|
|
55
|
+
# equality within the class. to_s and construction are ALWAYS strict,
|
|
56
|
+
# in every family gem: a sender never puts garbage on the wire —
|
|
57
|
+
# byte-sequence values must be Strings tagged ASCII-8BIT, preference
|
|
58
|
+
# values Integers 0..10, keys Structured Fields keys.
|
|
59
|
+
#
|
|
60
|
+
# The registry is Fast::Digest::ALGORITHMS — RFC 9530 §7.2 Table 2's
|
|
61
|
+
# eight entries pinned as frozen data (sha-512 and sha-256 Active, the
|
|
62
|
+
# other six Deprecated, the status advisory and never a gate), with
|
|
63
|
+
# unknown keys passing through: future registrations never break old
|
|
64
|
+
# gem versions. Fast::Digest.compute answers exactly the stdlib's
|
|
65
|
+
# bytes for sha-256, sha-512, md5 and sha; the four checksum entries
|
|
66
|
+
# (unixsum, unixcksum, adler, crc32c) and unknown keys raise
|
|
67
|
+
# Fast::Digest::Error naming the key — their digest values pass
|
|
68
|
+
# through the fields bytewise, carried on the wire, never computed or
|
|
69
|
+
# verified locally.
|
|
70
|
+
#
|
|
71
|
+
# Inside this module a bare Digest resolves to Fast::Digest, so every
|
|
72
|
+
# stdlib reference spells the leading scope operator ::Digest::SHA256
|
|
73
|
+
# (lib/fast/digest/registry.rb).
|
|
74
|
+
module Digest
|
|
75
|
+
end
|
|
76
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: fast-digest
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Eric Jacobs
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: fast-sfv
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
description: |
|
|
27
|
+
Fast::Digest implements RFC 9530 Digest Fields — the four integrity
|
|
28
|
+
fields as frozen value objects over fast-sfv: ContentDigest and
|
|
29
|
+
ReprDigest (algorithm key => digest Byte Sequence) with the
|
|
30
|
+
fail-closed recipient semantics of RFC 9651 2.2, and
|
|
31
|
+
WantContentDigest and WantReprDigest (algorithm key => Integer
|
|
32
|
+
0..10 preference). The hash-algorithm registry of RFC 9530 7.2
|
|
33
|
+
Table 2 as frozen data with unknown-key passthrough, and digest
|
|
34
|
+
computing over the stdlib digest library for sha-256, sha-512, md5
|
|
35
|
+
and sha. One runtime dependency, fast-sfv.
|
|
36
|
+
executables: []
|
|
37
|
+
extensions: []
|
|
38
|
+
extra_rdoc_files: []
|
|
39
|
+
files:
|
|
40
|
+
- CHANGELOG.md
|
|
41
|
+
- README.md
|
|
42
|
+
- lib/fast/digest.rb
|
|
43
|
+
- lib/fast/digest/errors.rb
|
|
44
|
+
- lib/fast/digest/fields.rb
|
|
45
|
+
- lib/fast/digest/registry.rb
|
|
46
|
+
- lib/fast/digest/version.rb
|
|
47
|
+
homepage: https://github.com/jetpks/fast-http-fields
|
|
48
|
+
licenses:
|
|
49
|
+
- MIT
|
|
50
|
+
metadata:
|
|
51
|
+
homepage_uri: https://github.com/jetpks/fast-http-fields
|
|
52
|
+
source_code_uri: https://github.com/jetpks/fast-http-fields
|
|
53
|
+
changelog_uri: https://github.com/jetpks/fast-http-fields/blob/main/gems/digest/CHANGELOG.md
|
|
54
|
+
documentation_uri: https://github.com/jetpks/fast-http-fields/tree/main/gems/digest/docs
|
|
55
|
+
rubygems_mfa_required: 'true'
|
|
56
|
+
rdoc_options: []
|
|
57
|
+
require_paths:
|
|
58
|
+
- lib
|
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '3.4'
|
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 3.5.3
|
|
69
|
+
requirements: []
|
|
70
|
+
rubygems_version: 4.0.20
|
|
71
|
+
specification_version: 4
|
|
72
|
+
summary: 'RFC 9530 Digest Fields: Content-Digest, Repr-Digest, Want-Content-Digest
|
|
73
|
+
and Want-Repr-Digest.'
|
|
74
|
+
test_files: []
|