*: Replace implicit hex hash encoding with Algorithm-based encoding #30
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The docs for
Algorithm
(nowalgorithm
) made it clear that the algorithm identifier was intended to cover both the hash and encoding algorithms. @stevvooe confirmed this interpretation in recent comments as well. The idea is that a future algorithm may chose a non-hex encoding like base 64.The previous implementation, on the other hand, baked the hex encoding into key locations (e.g. in
NewDigestFromBytes
andDigest.Validate
). This commit makes the encoding part ofAgorithm
instances, adding a newEncoding
interface and anAlgorithm.Encoding
method. In order support external algorithms with different encoding, I've defined anAlgorithm
interface with the public API and made renamed the local implementation of that interface toalgorithm
. And now thatAlgorithm
is a stand-alone interface, I've made the identifier-to-Algorithm
registry public by renamingalgorithms
toAlgorithm
.I've also split out the flag binding into its own
AlgorithmFlag
structure, because the oldAlgorithm.Set
pointer assignment no longer works now thatAlgorithm
is an interface. Having a separate interface for theflag.Value
also simplifies theAlgorithm
interface, which makes it easier to write externalAlgorithm
implementations while still benefiting from theflag.Value
helper.API changes:
Additions
Algorithms
, a newly-public registry of known algorithm identifiers.Algorithm.HashSize
, which allows us to avoid a previously-hardcoded hex assumption inDigest.Validate
.Algorithm.Encoding
, which allows us to implementNewDigester
and avoid a previously-hardcoded hex assumption inNewDigestFromBytes
.Digest.Hash
, as a better name forDigest.Hex
.Encoding
, a new interface backingAlgorithm.Encoding
.Hex
, the lowercase base 16Encoding
.NewDigester
, as a more flexible replacement forNewDigest
.NewDigestFromHash
, as a better name forNewDigestFromHex
.Adjustments
Algorithm.Hash
now returnsnil
on unavailable algorithms, to mirror the old API forAlgorithm.Digester
.Deprecations
NewDigest
, becauseNewDigester
is a more flexible form of the same thing that is just about as compact.NewDigestFromHex
, becauseNewDigestFromHash
is a better name for this function.Digest.Hex
, becauseHash
is a better name for this method.Removals
Algorithm.Set
, because it is not possible to support this now thatAlgorithm
is an interface.I also switched to black-box testing (with
digest_test
package names) because I was gettingHex
-undefined errors with white-box testing, and we don't actually need any white-box access in our test suite.The diff here is fairly large, so if maintainers want me to break this up into smaller chunks (e.g. one PR adding the
Encoding
interface, another adding anAlgorithm.Encoding
method, etc. or whatever), just let me know. If the adjustments and removals are not acceptable, we can avoid them with creative naming. But some compatibility issues may be inevitable with the change from structs to interfaces in theDigest.Algorithm()
return value.