-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cryptographic hash functions (SHA1 and SHA2 family, MD5, etc.) #1116
Comments
I like the idea. @nicowilliams- what say you? Unfortunately, it would mean either compiling in openssl/etc as a hard dependency or making it an optional feature during compilation. Either works, I suppose. Of course, we could decide we finally want executable library loading ( |
I was looking to do this today. I have an array of objects that I wanted to fingerprint. something like: $ echo '[{"x": {"a": "a"}}, {"x": {"b": 3}}, {"x": {"c": "c"}}]' | \
jq '.[] |= . + {xhashed: .x | tostring}'
[
{
"x": {
"a": "a"
},
"xhashed": "{\"a\":\"a\"}"
},
{
"x": {
"b": 3
},
"xhashed": "{\"b\":3}"
},
{
"x": {
"c": "c"
},
"xhashed": "{\"c\":\"c\"}"
}
] where One thing to keep in mind is that you would need to canonicalize the object representation into some standard way before hashing. https://github.com/substack/json-stable-stringify comes to mind. If we're worried about pulling in dependencies, we could just use a micro-lib from clibs or ccan such as https://github.com/jb55/sha256.c |
I also noticed that when you do |
Since I've opened this topic I've created a new branch with new "extensions", like one can see in the following examples and tests: https://github.com/cipriancraciun/jq/tree/patches/extensions/src/_extensions/_examples The branch is at: https://github.com/cipriancraciun/jq/tree/patches/extensions Now to answer @jb55 question: my crypto functions (MD5 and SHA family) come in two variants as seen in: https://github.com/cipriancraciun/jq/blob/patches/extensions/src/_extensions/jqe_crypto_builtins.h The While the See the following example:
|
jq allows a single name to be used for different defs with different arities. Why not just Btw, the prefix |
First I didn't want to "trash" the namespace, because perhaps some day these functions would have been introduced in And why the two different named functions (i.e.
I know, but I always like to put it just for "sake of completion", because (from a functional language point of view) it is equivalent to |
Yes, we should do this. Questions:
Also, we're going to need a base64 decoder. And, really, we need a binary "type" -- basically pretending that binary data is actually an array of small numbers (0..255, naturally). We don't want to be base64 coding all the time. |
For base64 decoder see #47 This works for me, but for my use case I need base64decode + md5 (calculating fingerprints of public ssh keys). |
This looks like a great idea, is it still planned? |
Can we try and implement this :) We have @base64 today which is pretty useful, some kind of md5 hash would be amazing Documentation and examples with @base64 are below https://stedolan.github.io/jq/manual/#example71 |
Hi, fq has support for some hash functions if you want to play around: $ echo -n hello | fq -rRs 'tomd5|tohex'
5d41402abc4b2a76b9719d911017c592
$ echo -n hello | md5
5d41402abc4b2a76b9719d911017c592 (the reason you also need |
I think #1931 is also related -- the use case I'm looking at is that I've got a JSON document with some base64 data in it and a sha256 digest of that (potentially binary/non-UTF8) data, and I'd love a way to validate the base64 matches the checksum without having to round trip outside So for my use case, I'd need either a solution to #1931 or direct-base64-consuming variants of the checksum functions. 😞 (For more about the use case, see the |
It would be useful to have as builtins some cryptographic hash functions (like say the SHA1 and SHA2 family, MD5, etc.) (and even some HMAC functions).
The main use-cases for such a feature would be for example:
jq
is used as a pre-processor for loading some JSON object streams in a document oriented database like CouchDB, MongoDB, etc.;group_by(.key | sha1 | [:2])
;I have quickly hacked a proof-of-concept based on the latest release (
1.5
): https://github.com/cipriancraciun/jq/tree/patches/sha1 , see also the diff at the link bellow:a5b5cbe...cipriancraciun:patches/sha1
If this is deemed useful I could provide the implementation for all these based on OpenSSL or GnuTLS.
The text was updated successfully, but these errors were encountered: