SRI encoder in pure Nix

I am fully aware that this was incredibly goofy to write, and I also fully expect that in like 1-2 versions from now they’re going to add something like builtins.hashString "sri" "XXXX" or something.

BUT

Here’s a rudimentary base16 → SRI encoder written in pure Nix. There’s a few other encoders peppered in there; but it lets you do this which is useful for dynamically handling hashes:

nix-repl> :lf github:aakropotkin/ak-nix
nix-repl> lib.hexToSri "4f868bd99cda20b90b18a6f1b2..."  # base16 SHA512
"sha512-T4aL2ZzaILkLGKbxssipYVRs8334PSR9FQzTGftZbc3jIPGkiXXS7qUCh8/q8UWFzxBZQ92dvR0v7+AM9wL2PA=="

nix-repl> lib.sriFile "sha512" ./foo
"sha512-T4aL2ZzaILkLGKbxssipYVRs8334PSR9FQzTGftZbc3jIPGkiXXS7qUCh8/q8UWFzxBZQ92dvR0v7+AM9wL2PA=="

nix-repl> lib.sri256File ./foo
...

You get the idea.
This certainly isn’t performant; but it might be just what you need for a niche situation, and this’ll save you about an hour or two of fiddling :sweat_smile:

2 Likes