"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.10.5.2/adminclient/LinOTPAdminClientCLI/src/linotpadminclientcli/pytoken.template.py" (13 May 2019, 2834 Bytes) of package /linux/misc/LinOTP-release-2.10.5.2.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.

    1 #!/usr/bin/env 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 
   35 class HmacOtp:
   36     def __init__(self, key, counter=0, digits=6):
   37         self.key = key
   38         self.counter = counter
   39         self.digits = digits
   40 
   41     def hmac(self, key=None, counter=None):
   42         key = key or self.key
   43         counter = counter or self.counter
   44         digest = hmac.new(key, struct.pack(">Q", counter), sha1)
   45         return digest.digest()
   46 
   47     def truncate(self, digest):
   48         offset = ord(digest[-1:]) & 0x0f
   49 
   50         binary = (ord(digest[offset + 0]) & 0x7f) << 24
   51         binary |= (ord(digest[offset + 1]) & 0xff) << 16
   52         binary |= (ord(digest[offset + 2]) & 0xff) << 8
   53         binary |= (ord(digest[offset + 3]) & 0xff)
   54 
   55         return binary % (10 ** self.digits)
   56 
   57     def generate(self, key=None, counter=None):
   58         key = key or self.key
   59         counter = counter or self.counter
   60         otp = self.truncate(self.hmac(key, counter))
   61         self.counter = counter + 1
   62         return otp
   63 
   64 
   65 def main():
   66     counter_file = ""
   67 
   68     system = platform.system()
   69     if system == "Linux":
   70         counter_file = os.path.join(os.getenv("HOME"), ".pytoken-counter")
   71     elif system == "Windows":
   72         counter_file = os.path.join(os.getenv("HOMEDRIVE"), os.getenv("HOMEPATH"), "\pytoken-counter")
   73     else:
   74         print "I do not know your operating system"
   75         sys.exit(1)
   76 
   77 
   78     if os.path.exists(counter_file):
   79         counter = int(file(counter_file).read().strip()) + 1
   80     else:
   81         counter = 0
   82 
   83     hexkey = "<put_your_hmac_here>"
   84 
   85     key = binascii.a2b_hex(hexkey)
   86     otp = HmacOtp(key, counter=counter).generate()
   87 
   88     print "Your OTP with number %d is %06d." % (counter, otp)
   89     print "Happy Authenticating!"
   90 
   91     file(counter_file, 'w').write(str(counter))
   92 
   93 if __name__ == '__main__':
   94     main()
   95