Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write this program in python. Login security One important aspect of security in

ID: 3782811 • Letter: W

Question

write this program in python.

Login security One important aspect of security in computer science is the concept of hashing: taking some text, and somehow converting it to a number. This is needed because many security algorithms work through math, so numbers are needed Another important aspect is the use of the modulo operator You've seen this it returns the remainder portion of a division. This is useful because unlike most other math operators, modulo is one-way. That is, I can tell you that I'm thinking of a number x, and when I mod it by 5, I get 3, but from this information alone, you don't know whether x is 3 or 8 or 13 or 18, or In this problem, we'll create a login screen, where the user must enter a password in order to see a secret message. We will give the user 3 chances to get the password right, and either print the secret message or a failure message (after 3 chances).

Explanation / Answer

from flask import Flask, session, redirect, url_for, escape, request, render_template

from hashlib import md5

import MySQLdb

app = Flask(__name__)

#######################

#   DATABASE CONFIG   #

#######################

db = MySQLdb.connect(host="localhost", user="root", passwd="", db="test")

cur = db.cursor()

@app.route('/')

def index():

    if 'username' in session:

        username_session = escape(session['username']).capitalize()

        return render_template('index.html', session_user_name=username_session)

    return redirect(url_for('login'))

@app.route('/login', methods=['GET', 'POST'])

def login():

    error = None

    if 'username' in session:

        return redirect(url_for('index'))

    if request.method == 'POST':

        username_form = request.form['username']

        password_form = request.form['password']

        cur.execute("SELECT COUNT(1) FROM users WHERE name = %s;", [username_form]) # CHECKS IF USERNAME EXSIST

        if cur.fetchone()[0]:

            cur.execute("SELECT pass FROM users WHERE name = %s;", [username_form]) # FETCH THE HASHED PASSWORD

            for row in cur.fetchall():

                if md5(password_form).hexdigest() == row[0]:

                    session['username'] = request.form['username']

                    return redirect(url_for('index'))

                else:

                    error = "Invalid Credential"

        else:

            error = "Invalid Credential"

    return render_template('login.html', error=error)

@app.route('/logout')

def logout():

    session.pop('username', None)

    return redirect(url_for('index'))

app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

if __name__ == '__main__':

    app.run(debug=True)

login HTML        

<!DOCTYPE html>

<html>

<head>

    <title>Login</title>

    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bootstrap.min.css') }}">

    <script src="{{url_for('static', filename='js/bootstrap.min.js')}}"></script>

</head>

<body>

    <div class="container">

        <div class="row">

            <div class="col-md-6 col-md-offset-3">

                <form action="" method="POST">

                    {% if error %}

                        <p class=error><strong>Error:</strong> {{ error }}

                    {% endif %}

                    <div class="input-group">

                        <span class="input-group-addon" id="basic-addon3">Your Username</span>

                        <input type="text" class="form-control" name="username" id="user" aria-describedby="basic-addon3">

                    </div>

                    <br>

                    <div class="input-group">

                        <span class="input-group-addon" id="basic-addon3">Your Password</span>

                        <input type="text" class="form-control" id="pass" name="password" aria-describedby="basic-addon3">

                    </div>

                    <br>

                    <input type="Submit" value="Login" class="btn btn-default btn-sm">

                </form>

            </div>

        </div>

    </div>

</body>

</html>