"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.11/adminclient/LinOTPAdminClientCLI/src/linotpadminclientcli/pytoken-totp.py" (12 Nov 2019, 2886 Bytes) of package /linux/misc/LinOTP-release-2.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "pytoken-totp.py" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/python2
    2 # -*- coding: utf-8 -*-
    3 #
    4 #    LinOTP - the open source solution for two factor authentication
    5 #    Copyright (C) 2010 - 2019 KeyIdentity GmbH
    6 #
    7 #    This file is part of LinOTP admin clients.
    8 #
    9 #    This program is free software: you can redistribute it and/or
   10 #    modify it under the terms of the GNU Affero General Public
   11 #    License, version 3, as published by the Free Software Foundation.
   12 #
   13 #    This program is distributed in the hope that it will be useful,
   14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16 #    GNU Affero General Public License for more details.
   17 #
   18 #    You should have received a copy of the
   19 #               GNU Affero General Public License
   20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   21 #
   22 #
   23 #    E-mail: linotp@keyidentity.com
   24 #    Contact: www.linotp.org
   25 #    Support: www.keyidentity.com
   26 #
   27 " HMAC-OTP Software Token "
   28 
   29 import os, sys, platform
   30 import binascii
   31 import hmac
   32 from hashlib import sha1
   33 import struct
   34 from getopt import getopt, GetoptError
   35 
   36 
   37 class HmacOtp:
   38     def __init__(self, key, counter=0, digits=6):
   39         self.key = key
   40         self.counter = counter
   41         self.digits = digits
   42 
   43     def hmac(self, key=None, counter=None):
   44         key = key or self.key
   45         counter = counter or self.counter
   46         digest = hmac.new(key, struct.pack(">Q", counter), sha1)
   47         return digest.digest()
   48 
   49     def truncate(self, digest):
   50         offset = ord(digest[-1:]) & 0x0f
   51 
   52         binary = (ord(digest[offset + 0]) & 0x7f) << 24
   53         binary |= (ord(digest[offset + 1]) & 0xff) << 16
   54         binary |= (ord(digest[offset + 2]) & 0xff) << 8
   55         binary |= (ord(digest[offset + 3]) & 0xff)
   56 
   57         return binary % (10 ** self.digits)
   58 
   59     def generate(self, key=None, counter=None):
   60         key = key or self.key
   61         counter = counter or self.counter
   62         otp = self.truncate(self.hmac(key, counter))
   63         self.counter = counter + 1
   64         return otp
   65 
   66 
   67 def main():
   68 
   69     HEXKEY = "400edad7f3e8939c7ffa2d57d1bed94695bfd46c"
   70     TIMESTEP = 60
   71     OFFSET = 0
   72 
   73     def usage():
   74        print "o, offset=      tokenoffset in seconds"
   75 
   76     try:
   77         opts, args = getopt(sys.argv[1:], "o:",
   78                 ['offset=', '--help'])
   79 
   80     except GetoptError:
   81         print "There is an error in your parameter syntax:"
   82         usage()
   83         sys.exit(1)
   84 
   85     for opt, arg in opts:
   86         if opt in ('o', '--offset'):
   87             print "setting offset : ", arg
   88             OFFSET = int(arg)
   89 
   90 
   91 
   92 
   93     from time import time
   94     counter = int((time() + OFFSET) / TIMESTEP + 0.5)
   95 
   96     key = binascii.a2b_hex(HEXKEY)
   97     otp = HmacOtp(key, counter=counter).generate()
   98 
   99     print "Your OTP with number %d is %06d." % (counter, otp)
  100     print "Happy Authenticating!"
  101 
  102 
  103 if __name__ == '__main__':
  104     main()
  105