# -*- Mode: Python -*-
#
# FakeMAC example in Python
# Copyright (C) 2021 Jean Zee (a.k.a. CmpZ)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# Documentation is at http://cybertiggyr.com/fakemac.html
#

#
# CAVEAT
#
# I am not a frequent Python programmer.  I'm sure that I've made
# maybe Python noob mistakes.  I'm vaguely aware that there's a
# Python code formatting style; I did not follow it & probably have
# violated it.  Sorry about that that, but no, I probably won't be
# motivated to change this code.
#

import sys              # https://docs.python.org/3/library/sys.html
import hashlib          # https://docs.python.org/3/library/hashlib.html

########################################################################
#
# If you use Fake_MAC in your own server-side Python code, you'll
# need the next 2 functions.
#

def sha1_string( s ):
    bs = bytes( s, "utf-8" )            # string as octets
    digester = hashlib.sha1( bs )
    hex = digester.hexdigest()
    return hex

def Fake_MAC (secret, message):
    o_pad = sha1_string( secret + "ooo" )
    i_pad = sha1_string( secret + "iii" )
    return sha1_string( o_pad + sha1_string( i_pad + message ) )

########################################################################
#
# The rest is scaffolding to demonstrate the 2 functions above.
#

def main():
    secret = sys.argv[1]
    message = sys.argv[2]
    print( sha1_string( message ) )
    fakemac = Fake_MAC( secret, message )
    print( fakemac )

if __name__ == "__main__":
    # execute only if run as a script
    main()

# end of file
