Simple programming language with md5 / sha1 hash command?

I'm looking to create a simple password-cracked cracker that will hash the input from stdin and compare the result with an existing hash (for my security class).

I've never done any real programming with hashes, although it was mostly conceptual / math parts. Is there a (relatively) easy-to-learn language that has a simple hash command? A friend of mine recommended Python, but I'm not sure if there is anything else out there where I could pick up a day or two. I know a little C, but hashing seems to be relatively complex and needs additional libraries.

Also, any syntactic help with the actual hash command would be appreciated. The program itself must support odd hash methods, such as hashing a value twice or using a salt set that doesn't change, and be able to accept data from stdin (for use with programs like johntheripper).


source to share

5 answers

Assuming you're asked to use hash functions rather than implementing the hash yourself, the Python hashlib module has routines for md5 and sha1:

[Updated for Python 3, which hashlib

requires bytes, not strings:]

>>> import hashlib
>>> s = 'something to hash'
>>> sb = s.encode("utf8")
>>> hashlib.md5(sb)
<md5 HASH object @ 0x7f36f5ce3440>
>>> hashlib.md5(sb).hexdigest()
>>> hashlib.sha1(sb).hexdigest()


[Legacy Python 2 version:]

>>> import hashlib
>>> s = 'something to hash'
>>> hashlib.md5(s)
<md5 HASH object @ 0xb7714ca0>
>>> hashlib.md5(s).hexdigest()
>>> hashlib.sha1(s).hexdigest()




I think python is a great choice for something like this. It has a hashlib module , and if you need more power, the PyCrypto toolkit is easy to use and supports many cryptographic primitives including hashing. If you already know some C, then maybe just using this with openssl libraries will be easier for you. In any case, it is usually worth investing to learn a crypto library for your preferred language (especially a cryptoclass), because in the end you will want to use something that you do not want to code by hand and be sure it does it right.

As for the syntax with the actual hash command, here is an example in python with PyCrypto to get the SHA256 hash (using python 3.2):

import Crypto.Hash.SHA256

to_hash = "hello, how are you?"
sha256 =
dgst = sha256.hexdigest()


outputs output





A library hashlib

in python contains the following:

'md5', 'new', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'


for more details:

>>> import hashlib
>>> m = hashlib.md5()
>>> m.update(b"Nobody inspects")
>>> m.update(b" the spammish repetition")
>>> m.digest()
>>> m.digest_size
>>> m.block_size




This little python program is best for these small tasks

import hashlib
import sys
    hash_name = sys.argv[1]
except IndexError:
    print 'Specify the hash name as the first argument.'
        data = sys.argv[2]
    except IndexError:
        print 'Please enter the data to be hashed'
    h =
    print h.hexdigest()



$ python sha1 password1
$ python sha256 password1
$ python md5 password1




No programming language worthy of its salt can be learned in a few days. You might be able to get the syntax, but not much above that. I like this article:

There is probably no better programming language for this. I can recommend C # as it has simple cryptography classes in System.Security.Cryptography


To find the MD5 hash of a byte array, you can use something like this:

byte[] hash = System.Security.Cryptography.MD5.Create().ComputeHash(myByteArray);


To use SHA1, just replace MD5 with SHA1.

If you want to get the hash of an ASCII string, you can get the byte array like this:

byte[] myByteArray = System.Text.Encoding.ASCII.GetBytes(myString);


An example function for converting a string to a hash:

// At the top of the file:
// using System.Security.Cryptography;
// using System.Text;

byte[] GetHash(string message)
    return MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(message));




All Articles