john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

sha1 hash generator temp directory os stat last modified file chown chmod

import hashlib      # http://docs.python.org/2/library/hashlib.html

plaintext = 'Nobody inspects'

hashed_message = hashlib.sha1()
hashed_message.update( plaintext )
print  hashed_message.hexdigest()

same_message = hashlib.sha1( plaintext )
print same_message.hexdigest()

shared_secret = 'mysharedsecret'
print plaintext
print repr( plaintext )     # repr makes any object a string BUT also grabs the single quotes
signed_message = hashlib.sha1( repr( plaintext ) + shared_secret )

print signed_message.hexdigest()

# FYI git calculates sha1 using: sha1("blob " + filesize + "\0" + data)


# - - - - - - - - - - - - - - - - - - - - - - - -
import sys
import hashlib

BUFFER_SIZE = 1024 * 1024 * 64  # 1KB * 1024 = 1MB x 16 = 16MB

def hashfile( filepath , algorithm_instance ):
    with open( filepath, 'rb' ) as file:
        while True:
            buffer = file.read( BUFFER_SIZE )
            if not buffer:
                break
            algorithm_instance.update( buffer )
    return  algorithm_instance.hexdigest()


if __name__ == "__main__":
    CORRECTUSAGE = 'python ' + sys.arvg[0] + ' [sha1|md5] filepath'
    if len( sys.argv ) != 3:
        print 'ERROR: incorrect number of arguments, correct usage: %s' % CORRECTUSAGE
        sys.exit( 1 )

    hash_type = sys.argv[1]
    filepath = sys.argv[2]

    if hash_type.lower() == 'sha1':
        hash_result = hashfile( filepath , hashlib.sha1() ) # 1995
    elif hash_type.lower() == 'md5':
        hash_result = hashfile( filepath , hashlib.md5() )  # 1991
    else:
        print 'ERROR: hash type (%s) is not supported' % hash_type
        sys.exit( 1 )
    print '%s: \n%s\n' % ( hash_type , hash_result )


# - - - - - - - - - - - - - - - - - - - - - - - -

def hashfile( filepath ):
    sha1 = hashlib.sha1()
    file = open(filepath, 'rb')
    try:
        sha1.update( file.read() )
    finally:
        file.close()
    return sha1.hexdigest()


import os
import tempfile

string_list = list()
for i in range( 128 ):
    string_list.append( 'z' )
plaintext = ''.join( string_list )

local_file_path = os.path.join( tempfile.gettempdir() , 'data.txt' )
file = open( local_file_path , 'w' )
file.write( plaintext )
file.close()

file_hash = hashfile( local_file_path )
print '%s\n%s (sha1 of above)' % ( plaintext , file_hash )

string_hash = hashlib.sha1( plaintext )
print '%s\n%s (sha1 of above)' % ( plaintext , string_hash.hexdigest() )


print os.stat( local_file_path )   # WINDOWS = nt.stat_results , LINUX/MAC = posix.stat_results
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=128L, st_atime=1361408066L, st_mtime=1361409197L, st_ctime=1361408066
L)

# ino = windows does not return an inode number
# size = size in bytes (long)  ... special files = amount of data waiting
# atime = last access time , mtime = last modified time , ctime = creation time (windows) OR last metadata change time (linux)

# - - - - - - - - - - - - - - - - - - - - - - - -

import pwd
import grp
import os

try:
    uid = pwd.getpwnam( "nobody" ).pw_uid   # convert from string to id
    gid = grp.getgrnam( "nogroup" ).gr_gid
    os.chown( path, uid, gid )

    os.chown( path, uid, -1 )       # pass -1 in order to not modify the id value
except KeyError as error:
    print "key error when uid or gid not found"

mode = stat.S_IWRITE        # http://docs.python.org/2/library/os.html#os.chmod
os.chmod( path, mode )

  • « json write read decode parsing last error
  • exceptions try except Exception as error traceback stack trace »

Published

Oct 28, 2013

Category

python

~324 words

Tags

  • chmod 1
  • chown 1
  • directory 13
  • file 92
  • generator 3
  • hash 7
  • last 4
  • modified 4
  • os 7
  • python 180
  • sha1 6
  • stat 3
  • temp 3