TEXT 65
Google authenticator in perl Guest on 22nd May 2020 06:19:11 PM
  1. use Digest::HMAC_SHA1 qw(hmac_sha1);
  2.  
  3. # usage: ./otp.pl <80-bit secret key in hex>
  4. # e.g.: ./otp.pl 0123456789abcdef0123
  5. # note: you may need to convert the secret key from base32 notation to hex
  6.  
  7. $key_hex_string = shift @ARGV;
  8. $key_bin = pack( 'H2' x 10, unpack("(A2)*", $key_hex_string) );
  9.  
  10. $time = int(time/30);
  11. $b = pack("q>",$time);
  12. # on 32-bit systems, change q to L and uncomment the next line
  13. # $b = pack("L>",0) . $b;
  14. $hash = hmac_sha1($b,$key_bin);
  15. $offset = ord(substr($hash,length($hash)-1,1)) & 0x0F;
  16. $truncatedHash = substr($hash,$offset,4);
  17. $code = unpack("L>",$truncatedHash);
  18. $code = $code & 0x7FFFFFFF;
  19. $code %= 1000000;
  20. while(length($code) < 6){
  21.         $code="0$code";
  22. }
  23. print $code."\n";

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.