3 # Obtain the hidden service name from a tor hidden service RSA key
5 # Copyright (c) 2016 Peter Palfrader
7 # Permission is hereby granted, free of charge, to any person
8 # obtaining a copy of this software and associated documentation
9 # files (the "Software"), to deal in the Software without
10 # restriction, including without limitation the rights to use,
11 # copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the
13 # Software is furnished to do so, subject to the following
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 # OTHER DEALINGS IN THE SOFTWARE.
33 echo "$0 [-p] [hidden service RSA key]"
34 echo " Computes the Tor onion hostname from a given RSA public or private key."
35 echo " Use -p to indicate you are passing a public key. If none is given as an"
36 echo " argument, one is read from stdin."
42 if ! command -v openssl >/dev/null 2>&1 ; then
43 echo >&2 "This program needs the openssl command line tool".
50 if [ -n "$tempdir" ]; then
55 tempdir="$(mktemp -d)"
59 while getopts "ph" OPTION
75 shift $(($OPTIND - 1))
80 elif [ "$#" = 1 ]; then
88 if [ -z "$PUBIN" ]; then
90 if ! openssl rsa -pubout < "$KEY" 2>&1 > "$PKEY" | (grep -Fxv 'writing RSA key' >&2 || true); then
91 echo >&2 "Maybe you need to use -p for using a public key?"
97 mod="$(openssl rsa -pubin < "$KEY" -modulus -noout | cut -d= -f 2)"
98 exp="$(openssl rsa -pubin < "$KEY" -text -noout | awk '$1=="Exponent:" {print $2}')"
99 cat > "$tempdir/asn" << EOF
100 asn1=SEQUENCE:seq_sect
102 field1=INTEGER:0x$mod
106 openssl asn1parse -genconf "$tempdir/asn" -noout -out "$tempdir/blob"
108 python -c 'import base64, hashlib, sys; \
109 d = hashlib.sha1(sys.stdin.read()).digest()[0:10]; \
110 print "%s.onion"%(base64.b32encode(d).lower(),)
112 #if command -v base32 >/dev/null 2>&1 ; then
114 # perl -MDigest::SHA -e '
116 # $d=Digest::SHA::sha1(<>);
117 # $d=substr($d,0,10);
119 # ' < "$tempdir/blob" | base32 | tr A-Z a-z
122 # perl -MDigest::SHA -e '
123 # eval("use MIME::Base32 qw( RFC )");
125 # print STDERR "This program needs either the base32 command line tool or the MIME::Base32 perl module.\n";
129 # $d=Digest::SHA::sha1(<>);
130 # $d=substr($d,0,10);
131 # print lc(MIME::Base32::encode($d)), ".onion\n"
132 # ' < "$tempdir/blob"
137 # vim:set shiftwidth=4: