Saturday, June 11, 2016

Initializing a bare git repository for a remote server

# Creating a bare repository
# -------------------------------
# inside remote server
# -------------------------------
$ mkdir myproject
$ cd myproject/
$ mkdir gitdir.git
$ mkdir workingtree
$ cd gitdir.git/
$ git init --bare
Initialized empty Git repository in /absolute/path/to/myproject/gitdir.git/
$ touch hooks/post-receive
$ echo "git --git-dir=/absolute/path/to/myproject/gitdir.git --work-tree=/absolute/path/to/myproject/workingtree checkout -f" > hooks/post-receive 



# Pushing to a bare repository
# -------------------------------
# inside local machine
# -------------------------------
$ cd /path/to/myproject
$ git remote add remoteserver username@ipaddress:/absolute/path/to/myproject/gitdir.git
$ git push remoteserver master



# Looking at git branches and working tree files in bare repository
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ git branch
$ cd ../workingtree
$ ls



# Pushing multiple branches to a bare repository
# -------------------------------
# inside local machine
# -------------------------------
$ cd /path/to/myproject
$ git push remoteserver staging
$ git push remoteserver nightly
# etc
# -------------------------------
# inside remote server
# -------------------------------
cd /path/to/myproject/gitdir.git
git branch



# Checking out a different branch in bare repository
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ git checkout staging
fatal: This operation must be run in a work tree
$ cd ../workingtree
$ git checkout staging
fatal: Not a git repository (or any of the parent directories): .git



# So what do we do here?
# We shall edit the bare git repository's configuration file slightly
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ nano config 
# add a new entry 'worktree' inside core group
# set its value to 'absolute/path/to/myproject/workingtree' inside double-quotes
# like this:
    [core]
        repositoryformatversion = 0
        filemode = true
        bare = true
        worktree = "/absolute/path/to/myproject/workingtree"
# refer to this stackoverflow discussion here 



# Unfortunately, even setting the worktree explicitly didn't solve the problem.
# This time, I got a new fatal error
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ git checkout staging
fatal: core.bare and core.worktree do not make sense
# Hence, I edited the git repository's configuration file once again 
# There I changed the value of core.bare to False



# Checking out a different branch in bare repository
# -------------------------------
# inside remote server
# -------------------------------
$ cd /path/to/myproject/gitdir.git
$ git checkout staging
Switched to branch 'staging'
# I am not convinced this is a completely correct solution. 
# Maybe this is a hack approach. 
# But it worked for my case.



# Comments / criticisms / suggestions / improvements welcome below

Thursday, May 26, 2016

Cannot sympathize with her because she killed herself

I am left wondering why all the people are commenting "Allah apnake jannat nazil koruk (May Allah grant you paradise)" etc under a Facebook video of a Bangladeshi model attempting to commit suicide in her room. [P.S. It's a video of a model's failed attempt at committing suicide which she happened to publish on her own Facebook profile later on - she blamed her ex boyfriend for trying to commit suicide]

[BTW, soon afterwards, she took another attempt at taking her own life and she succeeded - there is no published video of that, duh!].

Is this how we think by default about death and about our lives? That after death it's all about just white clouds, skies, pearls and paradise?

Monday, March 21, 2016

Asymmetric encryption in Python using RSA from PyCrypto library

# Inspired from http://coding4streetcred.com/blog/post/Asymmetric-Encryption-Revisited-(in-PyCrypto)
# PyCrypto docs available at https://www.dlitz.net/software/pycrypto/api/2.6/
from Crypto import Random
from Crypto.PublicKey import RSA
import base64
def generate_keys():
# RSA modulus length must be a multiple of 256 and >= 1024
modulus_length = 256*4 # use larger value in production
privatekey = RSA.generate(modulus_length, Random.new().read)
publickey = privatekey.publickey()
return privatekey, publickey
def encrypt_message(a_message , publickey):
encrypted_msg = publickey.encrypt(a_message, 32)[0]
encoded_encrypted_msg = base64.b64encode(encrypted_msg) # base64 encoded strings are database friendly
return encoded_encrypted_msg
def decrypt_message(encoded_encrypted_msg, privatekey):
decoded_encrypted_msg = base64.b64decode(encoded_encrypted_msg)
decoded_decrypted_msg = privatekey.decrypt(decoded_encrypted_msg)
return decoded_decrypted_msg
########## BEGIN ##########
a_message = "The quick brown fox jumped over the lazy dog"
privatekey , publickey = generate_keys()
encrypted_msg = encrypt_message(a_message , publickey)
decrypted_msg = decrypt_message(encrypted_msg, privatekey)
print "%s - (%d)" % (privatekey.exportKey() , len(privatekey.exportKey()))
print "%s - (%d)" % (publickey.exportKey() , len(publickey.exportKey()))
print " Original content: %s - (%d)" % (a_message, len(a_message))
print "Encrypted message: %s - (%d)" % (encrypted_msg, len(encrypted_msg))
print "Decrypted message: %s - (%d)" % (decrypted_msg, len(decrypted_msg))
view raw RSA_example.py hosted with ❤ by GitHub

Symmetric encryption in python using AES from PyCrypto library

# Inspired from https://pythonprogramming.net/encryption-and-decryption-in-python-code-example-with-explanation/
# PyCrypto docs available at https://www.dlitz.net/software/pycrypto/api/2.6/
from Crypto.Cipher import AES
import base64, os
def generate_secret_key_for_AES_cipher():
# AES key length must be either 16, 24, or 32 bytes long
AES_key_length = 16 # use larger value in production
# generate a random secret key with the decided key length
# this secret key will be used to create AES cipher for encryption/decryption
secret_key = os.urandom(AES_key_length)
# encode this secret key for storing safely in database
encoded_secret_key = base64.b64encode(secret_key)
return encoded_secret_key
def encrypt_message(private_msg, encoded_secret_key, padding_character):
# decode the encoded secret key
secret_key = base64.b64decode(encoded_secret_key)
# use the decoded secret key to create a AES cipher
cipher = AES.new(secret_key)
# pad the private_msg
# because AES encryption requires the length of the msg to be a multiple of 16
padded_private_msg = private_msg + (padding_character * ((16-len(private_msg)) % 16))
# use the cipher to encrypt the padded message
encrypted_msg = cipher.encrypt(padded_private_msg)
# encode the encrypted msg for storing safely in the database
encoded_encrypted_msg = base64.b64encode(encrypted_msg)
# return encoded encrypted message
return encoded_encrypted_msg
def decrypt_message(encoded_encrypted_msg, encoded_secret_key, padding_character):
# decode the encoded encrypted message and encoded secret key
secret_key = base64.b64decode(encoded_secret_key)
encrypted_msg = base64.b64decode(encoded_encrypted_msg)
# use the decoded secret key to create a AES cipher
cipher = AES.new(secret_key)
# use the cipher to decrypt the encrypted message
decrypted_msg = cipher.decrypt(encrypted_msg)
# unpad the encrypted message
unpadded_private_msg = decrypted_msg.rstrip(padding_character)
# return a decrypted original private message
return unpadded_private_msg
####### BEGIN HERE #######
private_msg = """
Lorem ipsum dolor sit amet, malis recteque posidonium ea sit, te vis meliore verterem. Duis movet comprehensam eam ex, te mea possim luptatum gloriatur. Modus summo epicuri eu nec. Ex placerat complectitur eos.
"""
padding_character = "{"
secret_key = generate_secret_key_for_AES_cipher()
encrypted_msg = encrypt_message(private_msg, secret_key, padding_character)
decrypted_msg = decrypt_message(encrypted_msg, secret_key, padding_character)
print " Secret Key: %s - (%d)" % (secret_key, len(secret_key))
print "Encrypted Msg: %s - (%d)" % (encrypted_msg, len(encrypted_msg))
print "Decrypted Msg: %s - (%d)" % (decrypted_msg, len(decrypted_msg))
view raw AES_example.py hosted with ❤ by GitHub