Skip to content

Instantly share code, notes, and snippets.

@deibit
Created March 9, 2013 14:57
Show Gist options
  • Save deibit/5124411 to your computer and use it in GitHub Desktop.
Save deibit/5124411 to your computer and use it in GitHub Desktop.
It reads the (OSX) clipboard and ouput multiple hash of the content. Also if you add a filename as an argument it open the file and hash its content. Windows and Linux port should be straightforward. It paste back the md5 (easy to change) to the clipboard.
#!/usr/bin/env python -B
#
# Clipboard operations from http://www.macdrifter.com/2011/12/python-and-the-mac-clipboard.html
#
# It will paste md5 by default
import sys
import hashlib
import subprocess
def get_clipboard():
p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
retcode = p.wait()
data = p.stdout.read()
return data
def set_clipboard(data):
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
p.stdin.write(data)
p.stdin.close()
retcode = p.wait()
def hash_this(data):
result = {}
hashes = ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']
for t_hash in hashes:
t = getattr(hashlib, t_hash)()
t.update(data)
result[t_hash] = t.hexdigest()
for n in result:
print("{0:<6} : {1}".format(n,result[n]))
return result['md5']
target = None
try:
with open(sys.argv[1]) as f:
target = "".join(f.readlines())
except:
target = get_clipboard()
if __name__ == '__main__':
set_clipboard(hash_this(target))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment