Created
May 18, 2017 14:54
-
-
Save mateusza/03eee4307e5744f103a5cbb2671bc7c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# (C) 2017, Mateusz Adamowski | |
# public domain | |
# | |
# (compare with reference JS implementation: http://jsfiddle.net/russau/ch8PK/ ) | |
SECRET32="jbswY3DPEHPK3PXP" | |
base32_decode(){ | |
arg="$1" | |
result=0 | |
for value in $( echo -n "$arg" | tr a-z A-Z | xxd -ps | tr a-z A-Z | sed -e 's/../\0 /g' ) | |
do | |
value2=$( echo "$value" | sed -e ' | |
s/41/00/g; s/42/01/g; s/43/02/g; s/44/03/g; | |
s/45/04/g; s/46/05/g; s/47/06/g; s/48/07/g; | |
s/49/08/g; s/4A/09/g; s/4B/0A/g; s/4C/0B/g; | |
s/4D/0C/g; s/4E/0D/g; s/4F/0E/g; s/50/0F/g; | |
s/51/10/g; s/52/11/g; s/53/12/g; s/54/13/g; | |
s/55/14/g; s/56/15/g; s/57/16/g; s/58/17/g; | |
s/59/18/g; s/5A/19/g; s/32/1A/g; s/33/1B/g; | |
s/34/1C/g; s/35/1D/g; s/36/1E/g; s/37/1F/g;' ) | |
result=$( echo "32 $result * 16 i $value2 + p" | dc ) | |
done | |
resulthex=$( echo "$result 16 o p" | dc ) | |
echo $resulthex | |
} | |
secrethex=$( echo -n 00000000000000000000$( base32_decode "$SECRET32" ) | tail -c 20 ) | |
T=$( echo -n 0000000000000000$( echo "$( date +%s ) 30 / 16 o p" | dc ) | tail -c 16 ) | |
HMAC=$( echo -n "$T" | xxd -ps -r | openssl dgst -sha1 -mac HMAC -macopt "hexkey:$secrethex" -binary | xxd -ps ) | |
offsethex=$( echo -n $HMAC | tail -c 1 ) | |
offsetdec=$( echo "10 o 16 i $( echo -n $offsethex | tr a-f A-F ) p" | dc ) | |
offsetdouble=$(( $offsetdec * 2 )) | |
codebasehex=$( echo $HMAC | sed -e "s/^.\{$offsetdouble\}\(........\).*$/\1/" | tr a-f A-F ) | |
codebasedec=$( echo "10 o 16 i $codebasehex p" | dc ) | |
code=$(( ( 0x$codebasehex & 0x7FFFFFFF ) % 1000000 )) | |
#code2=$( echo "$codebasedec 2147483648 % 1000000 % p" | dc ) | |
echo "Secret: $SECRET32" | |
echo "Secrethex: $secrethex" | |
echo "Time: $T" | |
echo "HMAC: $HMAC" | |
echo "offsethex: $offsethex" | |
echo "offsetdec: $offsetdec" | |
echo "codebasehex: $codebasehex" | |
echo "codebasedec: $codebasedec" | |
echo "code: $code" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment