Its been a long time so and theres no code posts so I think its time we had one
In this Post we will talk about

  • Config parser and %
  • Writing to files
  • pythons hashlib libary
ConfigParser

has two variants Safe and Raw

Safe supports interpolation which means % is treated as a special character
From python man page for ConfigParser

On top of the core functionality, SafeConfigParser supports interpolation. This means values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization.

Raw treats % as a normal character which is useful for API Keys

This is a sample block of code for Python

import configparser
import os

def Set():

	config = configparser.RawConfigParser()
	print("Please Enter Twitter Public Key ")
    TPK = input()
	print("Please Enter Twitter Private Key ")
    TPRK = input()
	print("Please note private key for twitter is not stored")
    config.add_section('Twitter')
	config.set('Twitter', 'Public', TPK)
	print("Please wait as your Access token is fetched")
	twitter = Twython(TPK, TPRK, oauth_version=2)
	ACCESS_TOKEN = twitter.obtain_access_token()
	config.set('Twitter','Access',ACCESS_TOKEN)
	with open(GetDir() + "/" + 'config.cfg', 'w') as configfile:
    	config.write(configfile)

Assuming that Twitter public key is asd678asd767887sa78987asd8%u9843901243
if you use a safe config parser it will parse % and complain of a interplation error

the config file will be like

[Twitter]
Public = 90234uj98iew892348423
Private = 9087234dx9u0%8rd39u08

and can be read very easily

def Fetch(service,key):
	
    config = configparser.RawConfigParser()
	config.read(GetDir() + "/" + 'config.cfg')

	key = config.get(service, key)
	return key

This function fetches the value associated with the selected service like

fetch("Last.fm",Public) to gets the public key for last.fm

Writing to files

in the previous example you seen this code

with open(GetDir() + "/" + 'config.cfg', 'w') as configfile:
    	config.write(configfile)

with open simply is while this is open do this config.write is the method which writes the data and as configfile means the type of data to be written is configfile formatted
the 'w' means open the file in write mode theres also 'r' which is read ther is append mode as well but I'll leave that as research up to you. if the file doesn't exist the file will be created and the GetDir method is a custom method I wrote to get the windows docs driectory and add a \ to the path

Hashlib fun

So python has a libaray called hashlib which allows access to cryptographic hashing algorithms such as

  • MD5
  • The SHA family of chiphers

and more

to use them this is the standard way of using it

import hashlib
import getpass

passw = getpass.getpass('Password: ')
md = hashlib.md5()   
md.update(passw.encode(encoding='utf_8', errors='strict'))
password_hash = md.hexdigest()

Getpass is a input libary that takes the imput but hides it on screen useful for passwords
so we take the value of passw which we will assume is password. md is simply setting up which hashlib function we want which in this case is MD5. the update call updates the contents of md with passw.encode which uses the current selected suite to encode it then hexdigest gives the final result in MD5 assigned to password_hash which you can prin the contents by doing:

print(password_hash)