向md5(加密)函数提供外壳保护值 [英] shell-out value to md5 (crypto) function

查看:68
本文介绍了向md5(加密)函数提供外壳保护值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,其中我要构建JSON记录并需要在JQ中生成一些文本,但是将此文本通过管道传递给MD5 sum函数并将其用作键的值.

I'm looking for a solution where I'm building out a JSON record and need to generate some text in JQ but pipe this text to an MD5 sum function and use it as a value for a key.

echo '{"first": "John", "last": "Big"}' | jq '. | { id: (.first + .last) | md5 }'

通过查看手册和GH问题,我不知道如何执行此操作,因为函数无法调出外壳,并且没有内置提供类似哈希的功能的内置函数.

From looking at the manual and the GH issues I can't figure out how to do this since a function can't call out to a shell and there is not built in that provides a unique hash like functionality.

我正在寻找的一个更好的例子是这样的:

A better example what I'm looking for is this:

echo '{"first": "John", "last": "Big"}' | jq '. | {first, last, id: (.first + .last | md5) }'

输出:

{
  "first": "John",
  "last": "Big",
  "id": "cda5c2dd89a0ab28a598a6b22e5b88ce"
}

Edit2

和更多上下文.我正在创建 NDJson 文件以与esbulk一起使用.我需要为每个记录生成一个唯一的密钥.最初,我认为管道到外壳是最简单的解决方案,因此我可以轻松使用sha1sum或其他一些哈希函数,但这看起来比我想象的更具挑战性.

Edit2

and a little more context. I'm creating NDJson files for use with esbulk. I need to generate a unique key for each record. Initially, I thought piping out to the shell would be the simplest solution so I could either use sha1sum or some other hash function easily, but that is looking more challenging than I thought.

我正在寻找的一个更好的例子是这样的:

A better example what I'm looking for is this:

echo '[{"first": "John", "last": "Big"}, {"first": "Justin", "last": "Frozen"}]' | jq -c '.[] | {first, last, id: (.first + .last | md5) }'

输出:

{"first":"John","last":"Big","id":"cda5c2dd89a0ab28a598a6b22e5b88ce"}
{"first":"Justin","last":"Frozen","id":"af97f1bd8468e013c432208c32272668"}

推荐答案

使用tee允许使用管道,例如:

Using tee allows a pipeline to be used, e.g.:

echo '{"first": "John", "last": "Big"}' |
    tee >( jq -r '.first + .last' | md5 | jq -R '{id: .}') |
    jq -s add

输出:

{
  "first": "John",
  "last": "Big",
  "id": "cda5c2dd89a0ab28a598a6b22e5b88ce"
}

Edit2:

下面的代码使用while循环遍历数组的元素,但每次迭代均调用jq两次.对于在循环内根本不调用jq的解决方案,请参阅此页上的其他地方.

The following uses a while loop to iterate through the elements of the array, but it calls jq twice at each iteration. For a solution that does not call jq at all within the loop, see elsewhere on this page.

echo '[{"first": "John", "last": "Big"}, {"first": "Justin", "last": "Frozen"}]' |
jq -c .[] |
while read -r line ; do
    jq -r '[.[]]|add'  <<< "$line" | md5 |
        jq  --argjson line "$line" -R '$line + {id: .}'
done

这篇关于向md5(加密)函数提供外壳保护值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆