summaryrefslogtreecommitdiff
path: root/bash/denominate-function.bash
diff options
context:
space:
mode:
Diffstat (limited to 'bash/denominate-function.bash')
-rw-r--r--bash/denominate-function.bash82
1 files changed, 82 insertions, 0 deletions
diff --git a/bash/denominate-function.bash b/bash/denominate-function.bash
new file mode 100644
index 0000000..9e5cb31
--- /dev/null
+++ b/bash/denominate-function.bash
@@ -0,0 +1,82 @@
1#!/bin/bash
2denominate_function()
3{
4 declare -n func_name="$1"
5 local func_output_b64encoded output_hash
6 read -r -d '' \
7 func_output_b64encoded \
8 < <(base64) || [ "${func_output_b64encoded}" ]
9 read output_hash _ \
10 < <(base64 -d <<< "$func_output_b64encoded" | sha256sum)
11 [ ${#output_hash} = 64 ] || return
12 func_name=SHA256_${output_hash:0:32}
13 source <(cat <<END
14${func_name} ()
15{
16base64 -d <<.
17${func_output_b64encoded}
18.
19}
20END
21)
22}
23
24reverse_hash()
25{
26 case "$#.$1" in
27 2.SHA256 )
28 ;;
29 1.SHA256_???????????????????????????????? )
30 set -- SHA256 "${1#SHA256_}"
31 ;;
32 * )
33 return 1
34 ;;
35 esac
36 case ${#2} in
37 32 | 64 )
38 set -- "${1}_${2: 0 : 32}"
39 ;;
40 * )
41 return 1
42 ;;
43 esac
44 (
45 set -o pipefail
46 set -e
47 declare -f "$1" |
48 sed -n -e '/^[ ]*base64 -d <<[.]$/,/^[.]$/ { p }' |
49 sed -e '1 d ; $ d' |
50 base64 -d
51 )
52}
53
54example_input()
55{
56 sed -n -e '/^reverse_hash *()/ , /^}$/ p' <"$0"
57}
58
59set -e
60set -o pipefail
61
62if [ -t 0 ]
63then
64 if [ $# = 0 ]
65 then
66 denominate_function f < <(example_input)
67 else
68 denominate_function f <<< "$*"
69 fi
70else
71 denominate_function f
72fi
73
74read -r -d '' < <( reverse_hash "$f" | base64) || [ "$REPLY" ]
75if read s _ < <( base64 -d <<< "$REPLY" | sha256sum ) &&
76 [ ${#s} -eq 64 -a "${s: 0 : 32}" = "${f: -32 : 32}" ]
77then
78 declare -f "$f" | grep . &&
79 printf '%s\n' "$f"
80else
81 false
82fi