0

I have the following code snippets

thetype = raw_input("Please enter hash type. md5 or sha1")
hash_type = hashlib.thetype(word).hexdigest()

This returns the error "AttributeError: 'module' object has no attribute 'thetype' " I kind of understand why but I guess what I am really asking is, how can I fix this?

1
  • Just use hashlib.new(thetype, word).hexdigest() Commented Jul 27, 2012 at 0:12

2 Answers 2

2

By using a dictionary (you can also use getattr, but that introduces the possibility of getting at other unrelated attributes).

d = {"md5" : hashlib.md5, "sha1" : hashlib.sha1}
hash_type = raw_input("Please enter hash type. md5 or sha1")
d[hash_type].hexdigest()

Also, raw_input already returns a str, so there's no need to call str again.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a ton! Worked perfectly.
Yeah I know about the str thing. I was screwing around haha. Didn't even realise I forgot to remove it
0
import hashlib
thetype = raw_input("Please enter hash type. %r"%(hashlib.algorithms,))
# 2.7 or later... just catch the exception from .new(thetype) for older versions.
if thetype in hashlib.algorithms:
    print hashlib.new(thetype)('some data').hexdigest()
else:
    print "No Way!"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.