Skip to content

Latest commit

 

History

History
58 lines (42 loc) · 1.23 KB

README.md

File metadata and controls

58 lines (42 loc) · 1.23 KB

totp

Yet another minimal TOTP generator.

Table of Contents

Dependency

Download

wget https://raw.githubusercontent.com/KevCui/totp/master/totp
chmod +x ./totp

Main Code

secret="${1^^}$(printf "%$(( (8-${#1}) % 8 ))s" | tr " " "=")"
key="$(base32 -d <<< "$secret" \
    | xxd -p \
    | tr -cd 0-9A-Fa-f)"
mac=$(printf "%016X" "$(( ($(date +%s)) / 30))" \
    | xxd -r -p \
    | openssl sha1 -binary -mac hmac -macopt "hexkey:$key" \
    | xxd -p)
offset="$(( 16#"${mac:39:1}" * 2))"
printf "%06d\n" "$(( (0x${mac:offset:8} & 0x7FFFFFFF) % 1000000 ))"

Example

  • Generate TOTP with secret string:
~$ ./totp ZYTYYE5FOAGW5ML7LRWUL4WTZLNJAMZS
388841
  • To better protect secret, combine with secret-vault to generate TOTP:
~$ ./totp "$(vault.sh -k totp_secret)"
300597

Credits

This project is heavily inspired by susam/mintotp and jakwings/bash-totp.