2010/07/01

Torrent Files: Begone! (And Be Recoverable!)

Whenever I download a torrent file, the file ends-up in my web browser's download directory. Then I invariably click on the .torrent file to start the download. Since I usually download a few .torrent files a day, my download directory becomes cluttered with files I do not really want. But I do not want to delete the files since I might want to forward them to someone else later or download the rest of a partially downloaded torrent at a later time.

As with any difficulty in live, it can be solved by a small Python script. The best way I found to handle the files is to just move the files in a subdirectory. So I just "double-click" the script whenever I think too many .torrent files are lying around.

import os
import fnmatch

def main():
  try:
    _clear_torrent_files()
  except BaseException as e:
    print(e)
  input("Press a key to exit")

def _clear_torrent_files():
  files = os.listdir('./')

  files_move = []
  for file in files:
    if fnmatch.fnmatch(file, '*.torrent'):
      files_move.append(file)
  
  x=0
  for file in files_move:
    x +=1
    folder_dest = './TorrRep/__Cleared/'
    _createDirIfNotExist(folder_dest)
    file_dest = folder_dest + file
    str = '[{2}] - Moving {0} to {1}'.format(file, file_dest, x)
    if len(str) > 70:
      str = str[0:69]
    print (str)
    os.rename(file, file_dest)

def _createDirIfNotExist(in_dir):
  if not os.path.exists(in_dir):
    os.makedirs(in_dir)

if __name__ == '__main__':
  main()

Then the files are 'gone', but recoverable!

2010/06/27

Securely Erase the Content of A Drive... The Easy Way!

Do you sometimes have somewhat sensitive data that has been deleted on a drive but you would feel better if it were unrecoverable? And you don't want to wipe-out the whole drive (keep the data that is already in). Such a situation occurs a lot when changing PC at work. You know the next guy will likely be a computer expert; if that person has a penchant for evil, you definitely do not what that person to have access to the data that used to be on that computer.


Leave this script running overnight and you can be (kind of) confident nothing will be recoverable.





#python 3

import random
import os

def _getRandStr(rand_str_len):
  lib = "abcdefghjiklmnopqrstuvwxyz123456789"
  
  randomStr = []
  
  while len(randomStr) < rand_str_len:
    randomStr += lib[ random.randint(0,len(lib)-1) ]
  
  assert len(randomStr) == rand_str_len
  return "".join(randomStr)

def writeToHDUntilException():
  randomfolder = None
  nfilesInFolder = 0
  while True:
    if randomfolder is None or nfilesInFolder > 9:
      randomfolder = _getRandStr(16)
      if not os.path.isdir(randomfolder):
        os.mkdir(randomfolder)
      nfilesInFolder = 0
    fh = open( randomfolder + '/' + _getRandStr(32) + '.jpg', 'wb' )
    fh.write( _getRandStr(128).encode()*1024*8 )
    fh.close()
    nfilesInFolder += 1

if __name__ == '__main__':
  writeToHDUntilException()


If you leave that script running for long enough then you will eventually have overwritten all the free nodes of the HD. Delete the files created and the content that was there beforehand will be unrecoverable. Quite handy for USB keys.

2010/06/19

da_crypt.py: The smallest encryption library you will ever find!

Here is a small library that I wrote just for fun. If you provide a key that is the same length as the file/bytes to be encrypted, this algorithm is unbreakable (see XOR_cipher and One Time Pad).


#!/usr/bin/python3.0

class Key:

def __init__(self, bytesKey):
self.__key = bytesKey

def getAt(self, x):
x = x % len(self.__key)
return self.__key[x]

class Crypt_Xor:

def __init__(self, key):
self.__key = key

def encrypt(self, bytesToCrypt):
bytesCrypt = bytearray(len(bytesToCrypt))
x = 0
for byte in bytesToCrypt:
key_node = self.__key.getAt(x)
bytesCrypt[x] = byte ^ key_node
x += 1
return bytesCrypt

def decrypt(self, bytesToDecrypt):
bytesDeCrypt = bytearray(len(bytesToDecrypt))
x = 0
lstDecrypted = []
for char in bytesToDecrypt:
key_node = self.__key.getAt(x)
bytesDeCrypt[x] = bytesToDecrypt[x] ^ key_node
x += 1

return bytesDeCrypt



The amazing thing is that it can be fully implemented with very little code (31 lines in this case).
I got the idea reading the excellent book 'Computer Networks by Andrew S.Tanenbaum -- 8.1.4'.

Here is a small test function:


def _test_crypt_object( crypt ):
data_orig = b'I am a simple string to be encrypted.'
print( 'data_orig: %s' % data_orig )

testKey = b'My secret key... \x123\x456\x798'

crypt = crypt( Key(testKey) )
data_encoded = crypt.encrypt(data_orig)
print( 'data_encoded: %s' % data_encoded )
data_decoded = crypt.decrypt(data_encoded)
print( 'data_decoded: %s' % data_decoded )

assert data_decoded == data_orig

def test_module():
print( 'da_crypt test start' )
_test_crypt_object(Crypt_Xor)
print( 'da_crypt test end' )

if __name__ == '__main__':
test_module()


You can check the updated code at:
http://code.google.com/p/miscdev/source/browse/da_crypt/
It also contains a small extension that allows file encryption.

Be careful though: the implementation uses a circular key (for simplicity's sake). If you use a key that is not random or a key that is less than the size of the data to be encrypted then it becomes relatively easy to crack.

Enjoy!

2009/06/01

Picasa - Delete Annoying ".picasaoriginals" Space Consuming Folders

Did you ever notice that Picasa creates a backup of any picture that has been modified? I could never find an option to suppress that behavior. I mean, I know what I am doing; I do not want to have extra copies of my pictures laying around and taking up all my disk space.

So I built a small applicaiton that lists the ".picasaoriginals" folders location under the folder where the Python script is run from. It lists the content of the ".picasaoriginals" and prompts the user for deletion. You can view the code here. It is only a few dozens lines long so just copy/paste to a .py file if you want to use it. It is written in a Python 3.0.

Here is an example of the command-prompt interation with the program.
python DelPicasaOriginalBackups.py

Folder:
C:\pics\09-01-03 - Montreal\.picasaoriginals
Content:
{IMG_7432.jpg,IMG_7433.jpg,IMG_7434.jpg}

Do you want to delete the following folder and everything in sub-dirs?
C:\pics\09-01-03 - Montreal\.picasaoriginals
N/Y?
Hope it is useful. Feel free to post any comment/suggestion.

2007/09/02

pyWeightWatch: Monitor your Weight Efficiently

pyWeightWatch: Monitor your Weight Efficiently

Note:

The project page has moved: http://david-web.appspot.com/cnt/WeightWatch/

Consider the information on this page deprecated.

Installation:



Just unpack the .exe in a directory, then run pyWeight.exe.

The program is going to ask you how much you weight now. Jump on a scale and enter your weight.

The program will automatically generate a graph of your weight over time.




This is what the program generates for me:


Usage:

Enter today's weight and see the graph/save to .png:
pyWeight.exe
Just update the .png file with the data contained in Weight.xml:
pyWeight.exe -u
pyWeight.exe --update

Download:

pyWeightWatch v1.0 (source here)


Future features:

-Interpolation and prediction
-Better labelling
-XML indexed by date and weight, should extract all weightelement and then extract the weight and date.
-Leave some space in graph (not just the extremum of the data)
-Build Weight.xml if not present/valid

[Generated at: 2007_09_02_14h21m16s]