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).

+3


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()
'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'

      

+12


source


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 = Crypto.Hash.SHA256.new()
sha256.update(to_hash.encode())
dgst = sha256.hexdigest()
print(dgst)

      



outputs output

5c1940d2a11cd8b1e256ea4183ba22cddfa7cc0d00610a159bd7f5886d01067a

      

+2


source


A library hashlib

in python contains the following:

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

      

for more details: http://docs.python.org/release/3.1.3/library/hashlib.html

>>> import hashlib
>>> m = hashlib.md5()
>>> m.update(b"Nobody inspects")
>>> m.update(b" the spammish repetition")
>>> m.digest()
b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
>>> m.digest_size
16
>>> m.block_size
64

      

+2


source


This little python program is best for these small tasks

hash.py

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

      

Output

$ python hash.py sha1 password1
e38ad214943daad1d64c102faec29de4afe9da3d
$ python hash.py sha256 password1
0b14d501a594442a01c6859541bcb3e8164d183d32937b851835442f69d5c94e
$ python hash.py md5 password1
7c6a180b36896a0a8c02787eeafb0e4c

      

0


source


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: http://norvig.com/21-days.html

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));
}

      

-1


source







All Articles