#! /usr/bin/perl # a1-totp - Time-based One-time Password algorithm, as described in RFC 6283 # Author: Wessel Dankers # LICENSE: WTFPLv2 # # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # Version 2, December 2004 # Copyright (C) 2004 Sam Hocevar # # Everyone is permitted to copy and distribute verbatim or modified # copies of this license document, and changing it is allowed as long # as the name is changed. # # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION # # 0. You just DO WHAT THE FUCK YOU WANT TO. # # https://tools.ietf.org/html/rfc6238 # https://en.wikipedia.org/wiki/Time-based_One-time_Password_algorithm # Date: Wed, 14 Oct 2020 11:07:07 +0200 # From: Wessel Dankers # Subject: perl bijna-oneliner # Message-ID: <20201014090707.GU2003855@homsar.uvt.nl> # installation: sudo apt install libconvert-base32-perl # Usage: totp [] # # Interval is optioneel en andere intervallen dan de default worden toch # vrijwel nooit ondersteund. # сре 14 11:13 < Fruit> je krijgt van [de andere kant] een code # сре 14 11:13 < Fruit> die code voer je aan dat scriptje # сре 14 11:53 zo'n code ziet eruit als iets van icunwencwuinsaduiakdj # сре 14 11:54 oh eventueel met cijfertjes ook # сре 14 11:54 base32 # # сре 14 11:18 < joostvb> en t scriptje draai je op een air-gapped super secure # apparaat use strict; use warnings FATAL => 'all'; use Digest::SHA qw(hmac_sha1); use Convert::Base32; sub totp { my ($key, $interval) = @_; $interval ||= 30; my $time = pack('Q>', time / $interval); my $hmac = hmac_sha1($time, decode_base32($key)); my $offset = vec($hmac, length($hmac) * 2 - 2, 4); my ($value) = unpack('L>', substr($hmac, $offset, 4)); return sprintf("%u", ($value & 0x7FFFFFFF) % 1000000); } print totp(@ARGV), "\n";