Skip to content
RFC 6920 ni:/// URI handler
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

URI::NI - RFC6920 Named Identifiers

This module implements the ni: URI scheme from RFC 6920.

require 'uri'
require 'uri-ni' # or 'uri/ni', if you prefer

ni = URI::NI.compute 'some data'
# => #<URI::NI ni:///sha-256;EweZDmulyhRes16ZGCqb7EZTG8VN32VqYCx4D6AkDe4>
# => "1307990e6ba5ca145eb35e99182a9bec46531bc54ddf656a602c780fa0240dee"

This of course corresponds to:

$ echo -n some data | sha256sum
1307990e6ba5ca145eb35e99182a9bec46531bc54ddf656a602c780fa0240dee  -

This works as expected:

ni = URI('ni:///sha-256;4wwup6dvg7fBqXXdwkKGtnXnFOu7xyzNXwQBcwIxq1c')
# => #<URI::NI ni:///sha-256;4wwup6dvg7fBqXXdwkKGtnXnFOu7xyzNXwQBcwIxq1c>

RFC 6920 specifies a registry for algorithm designators. Of that list, sha-256, sha-384 and sha-512 are implemented. Eventually I will get around to doing the SHA-3 digests as well as the truncated SHA-256 ones. Implemented but not in the registry are md5, sha-1 and rmd-160. Really these identifiers only matter when you are trying to compute a new digest. For instance you can do this:

ni = URI('ni:///lol;wut')
# => #<URI::NI ni:///lol;wut>

…and the parser won't complain. But, if you then tried to take this result and compute a new digest with it:

# => #<URI::NI ni:///lol;wut>
ni.compute 'derp'
# URI::InvalidComponentError: Can't resolve a Digest context for the algorithm lol.

The purpose of this configuration is so that the parser doesn't croak on unexpected input, but otherwise assumes you know what you're doing. As such, there is no attempt to measure or otherwise divine the representation of any updates to the digest component:

ni = URI::NI.compute 'some data'
# => #<URI::NI ni:///sha-256;EweZDmulyhRes16ZGCqb7EZTG8VN32VqYCx4D6AkDe4>
ni.digest = 'whatever'
# => "whatever"
# => #<URI::NI ni:///sha-256;d2hhdGV2ZXI>

In addition to computing new digest URIs, this module will return the interesting part of its contents in binary, hexadecimal, base64, and (with a soft dependency), base32. There are accessors and mutators for digest, hexdigest, b32digest, and b64digest.

Finally, this module will also reuse any extant Digest::Instance object as long as it is in the inventory, and furthermore the compute method takes a block:

ctx =
# => #<Digest::SHA256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855>
ctx << 'hello world'
# => #<Digest::SHA256: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9>
ni = URI::NI.compute ctx
# => #<URI::NI ni:///sha-256;uU0nuZNNPgilLlLX2n2r-sSE7-N6U4DukIj3rOLvzek>
# => "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
ni = URI::NI.compute do |ctx|
  ctx << 'hello world'
# => #<URI::NI ni:///sha-256;uU0nuZNNPgilLlLX2n2r-sSE7-N6U4DukIj3rOLvzek>


Generated and deposited in the usual place.


You know how to do this:

$ gem install uri-ni

Or, download it off


Bug reports and pull requests are welcome at the GitHub repository.

Copyright & License

©2019 Dorian Taylor

This software is provided under the Apache License, 2.0.

You can’t perform that action at this time.