Search code examples
pythonsecuritysyntaxhashprogramming-languages

Simple programming language with a md5/sha1 hash command?


I'm looking to create a simple brute-force password-cracker which will hash an input from stdin and compare the result to an existing hash (for my securities class).

I've never done any actual programming involving hashes, though, since it's mostly been conceptual/mathematical parts so far. Is there a (relatively) easy-to-learn language which has a simple hash command? A friend of mine recommended Python but I'm not sure if there's anything else out there I could pick up within a day or two. I know a bit of C, but hashing seems relatively complicated and needs additional libraries.

Also, any syntax help with an actual hash command would be appreciated. The program itself needs to be able to support odd hash methods, like hashing a value twice or using a set salt that doesn't change, and be able to take input from stdin (for use with programs like johntheripper).


Solution

  • Assuming that you're only being asked to use the hash functions, and not to implement the hash yourself, Python's hashlib module has routines for md5 and sha1:

    [Updated for Python 3, in which hashlib wants bytes, not strings:]

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

    [Legacy Python 2 version:]

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