#!/usr/athena/perl

print "Auth! by amg\n";

# Auth takes as its argument a number. If the number is not
# present or invalid, it prompts for a username and password,
# and returns a valid number.

$now = int(time /600); # in ten-minute quanta
$keyin = $ARGV[0];

print "Your key was $keyin and the time is $now\n";

# We decompose the key into an id (which is a number between
# 000 and 999), a timestamp, and a validation number.

($then1, $id, $then2, $secure) = ($keyin =~ /(..)(...)(.....)(.*)/);
$then = $then1 . $then2;

if ($then > $now || $then < ($now - 2) || (($then*$id) % 171) != $secure)
{
  print "Authentication failed. Please enter your id:\n";
  $id = <STDIN>;
  print "Thank you. I assume you know your password.\n";
}

print "Lots of valid data here.\n";

printf "Your new authenticator is %02d%03d%05d%d\n",
	int($now/100000), $id, $now%100000, ($id*$now)%171;
