forked from khovratovich/equihash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ehpuzzle
executable file
·70 lines (61 loc) · 1.85 KB
/
ehpuzzle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
set -e
usage() {
echo "usage:"
echo "$ cat secret_key | $0 challenge n k /path/to/data >challenge"
echo "$ $0 solve challenge >solution"
echo "$ cat secret_key | $0 verify /path/to/challenge /path/to/solution"
exit 0
}
challenge() {
[ "${#}" -ne 3 ] && usage
secret_key="$(cat)"
n=$1
k=$2
data="$(cat "$3")"
ts="$(date +%s)"
sig=$(printf "%s %s %s %s" "$n" "$k" "$ts" "$data" | openssl dgst -sha256 -binary -hmac "$secret_key")
printf "%s%s %s %s %s" "$sig" "$n" "$k" "$ts" "$data"
}
solve() {
[ "${#}" -ne 1 ] && usage
puzzle="$1"
tail -c +33 "$puzzle" | {
read -r n k ts data || true
ehwait equihash solve -n "$n" -k "$k" -f "$puzzle" -s /dev/stdout
}
}
const_cmp() {
res=0
for i in $(seq 0 31); do
res=$((res | ($(printf "%d" "'${1:i:1}") ^ $(printf "%d" "'${2:i:1}") ) ))
done
return $res
}
#const_cmp "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" && echo ok || echo meh
#const_cmp "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAb" && echo ok || echo meh
#const_cmp "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" && echo ok || echo meh
verify() {
[ "${#}" -ne 2 ] && usage
secret_key="$(cat)"
puzzle="$1"
solution="$2"
sig="$(head -c 32 "$puzzle")"
msg="$(tail -c +33 "$puzzle")"
echo "$msg" | {
read -r n k ts data || true
sig2=$(printf "%s" "$msg" | openssl dgst -sha256 -binary -hmac "$secret_key")
const_cmp "$sig" "$sig2" || {
echo "error: invalid challenge"
exit 1
}
equihash verify -v -n "$n" -k "$k" -f "$puzzle" -s "$solution"
exit $?
}
}
case "$1" in
challenge) shift; challenge "${@}";;
solve) shift; solve "${@}";;
verify) shift; verify "${@}";;
*) usage;;
esac