Python implementation of sha512_crypt.c - python

I'm looking for a 100% pure Python implementation of sha512_crypt.c as taken from http://www.akkadia.org/drepper/SHA-crypt.txt.
I'm learning Python. I want to understand the code (and compare it to the C code, etc.). I don't know enough Python to write it myself -- even if I did, I wouldn't trust myself to get it right. I don't have that expertise. So please don't answer this question by telling me to go write the code myself. That's not my question. I am looking for an existing 100% Python implementation that gives the same output as the original sha512_crypt.c written by Ulrich Drepper.
Just to be sure my question is clear, answers I'm looking for are probably either:
"I know for sure that a 100% Python implementation of that C code doesn't exist."
"Here is the link to the Python code that you can download."
(Even though I'm not asking for help writing specific code, I was told to post here as a result of a meta discussion. This question is, after all, about studying Python code.)
BTW, I know there is a Java implementation here: ftp://ftp.arlut.utexas.edu/java_hashes/
I'm looking for the Python equivalent.
Thank you.
UPDATE: james-mills answered the question for me. But today I just learned about Nullege: A Search Engine for Python source code
http://www.nullege.com/
That could come in handy in the future.
I also learned that startpage.com will accept "filetype:py" as a search term and it returns some good results. Unfortunately, I tried the same with duckduckgo and it didn't return any results.

Check out passlib it seems to have a pure Python implemtnation of SHA512 crypt that it falls back to:
This class will use the first available of two possible backends:
stdlib crypt(), if the host OS supports SHA512-Crypt (most Linux systems).
a pure python implementation of SHA512-Crypt built into passlib.
You can see which backend is in use by calling the get_backend() method.
Found the source code: https://code.google.com/p/passlib/source/browse/passlib/handlers/sha2_crypt.py

Its a bit subjective because you have so many choices, but try this: http://google.com/search?q="sha512_crypt"+filetype:py. Notice the quotes around the filename to ensure you get an exact match (Google will treat the underscore as a space otherwise).
I'm not familiar with Drepper's work or Python, so I could not say if any are any exact matches.

Related

Debugging: how to view behind the scenes calls? - Python, PyQt

This seems like a somewhat simple question that I'm having a lot of trouble finding an answer for, perhaps I haven't found the words programmers use to talk about this.
I am a relatively inexperienced programmer, and have run into some difficulties with troubleshooting an application I have made. The question I have could be general, because I haven't found an answer to this for any language, but for me I'm specifically using python and PyQt4:
Is there any way to view the behind-the-scenes calls made when I execute a method/call a command? I can use debugging software (in my case winpdb) to track which lines in my code are called, but, aside from traceback info given after an error, haven't figured out how to follow the program's steps "behind the scenes". This would be helpful for cases where there hasn't been a programming error from the compiler's point of view, but the behavior is unexpected because the programmer doesn't have a complete understanding of a module's inner workings.
To put it another way: I want to know about the code that I didn't write. I want to know what is triggered after a line of my code is called. If I call a method like len(), python calls its own methods that will eventually return an integer to me. I'm hoping there's a way we can see what python does in between my lines of code.
If this has been asked before, please let me know, and accept my apologies for repeating the question, but I haven't been able to find an answer to that question, or at least the best way of asking it. Thank you very much for your help.
I'm not sure what your requirements are, but using cProfile with Gprof2Dot will generate a png of your code's call graphs and the amount of time spent in each function/method.
Just:
grab gprof2dot.py
Use the given sample code

How to convert python source code to C++ source code [duplicate]

This question already has answers here:
Convert Python program to C/C++ code? [closed]
(8 answers)
Closed 3 years ago.
I have been trying to find a way to convert .py source file to .cpp source (as a time saver from doing it manually). I've never used python before and was hoping for a quick way to convert it, and cleanup any code the converter might not do well.
So far, some of the options that I have found while googling seem to be: nuitka, cython, and pypy/rpython.
However, the documentation I have read only seem to produce executables, and not actual source code.
At this point, I have found py2c, but cannot seem to find any documentation on how to use it. Also, judging by the posted roadmap on the wiki, it does not seem to be a finished product, and so I'm doubtful as to its reliability.
If you can provide other sources on how this can be accomplished, or shed some light on something I may have missed on the above-mentioned possibilities, it would be appreciated. Otherwise, I will simply convert it manually.
Programming languages cannot be easily converted like this. For example, Python has a large standard library, C++ doesn't have the same library. How do you translate those calls?
More fundamentally, the semantics of the language are very different, even a statement as simple as x = 1 means something different in Python and C++.
You are going to have to write C++ while reading the Python.
Have a look at shedskin, if it won't do the whole job,it still might be helpfull.

Are there any examples on python-purple floating around?

I want to learn it but I have no idea where to start. Everything out there suggests reading the libpurple source but I don't think I understand enough c to really get a grasp of it.
There isn't much about it yet... the intro, the howto, and the sources (here browsing them online but of course you can git clone them) are about it. In particular, the tiny example client you can get from here does have some miniscule example of use of purple's facilities (definitely not enough, but maybe it can get you started with the help of some 'dir', 'help' and the like...?)
Not sure how much help this will be but based on information from here, it seems like you just install python-purple and import and call the functions as normal Python functions.
Can't help you with a concrete example as I decided to use something else. However, one of the first things I wanted to do after I cloned the repo was remove the ecore dependency. Here's a patch submitted to the mailing list to do just that: https://garage.maemo.org/pipermail/python-purple-devel/2009-March/000000.html
Incidentally, if you're looking for AIM take a look at twisted.words. For Yahoo, trying getting the source for curphoo or zinc (both are console YMSG clients). For GTalk/Jabber, I've had good experiences with xmpppy.

Can I encrypt email and decrypt it back using python default library set?

Of course similar questions have been asked in stackoverflow but I don't want to use any third party library like Crypto or something. So I need to generate a ciphertext from a user email and decrypt it back to plaintext. How can I do this in python?
A third-party system is your best bet.
If you really can't/don't want to use a third-party, maybe something simple would suffice.
One of the simpler algorithms is the Tiny Encryption Algorithm (TEA). Here's an example of a Python implementation that you could start with.
Yes, you can.
Read http://www.amk.ca/python/code/crypto.html
You'll find an answer there ;)
You're question is not concrete enough to say more. You may want to read http://en.wikipedia.org/wiki/Cryptography#Modern_cryptography
Cheers,
Tuergeist
Update:
No, you cannot. (with build in functionality due to export restrictions, see http://docs.python.org/library/crypto.html)
But you can, if you're implementing you own algorithm (bad idea).
So, the BEST solution is, to use the extension recommended by python core developers. See post above.
Cheers again.
If what you mean is that you want to roll your own encryption system, you could try using the built-in hmac and hashlib modules. (hashlib is new for 2.5, so if you must use an earlier Python, your hash choices are the older md5 and sha modules.)
If you are opposed to (or are prevented from) installing a third-party library but are OK with using third-party algorithms or even "lightweight" third-party implementations of algorithms (e.g. published Python source code which resides in a single .py file that you can incorporate or import yourself without using setup.py or any other formal installation), then I highly recommend you do so, because these are likely to be better than what you can come up with on your own.
The smallest and user-friendliest of these that I am aware of is known as p3, written by cryptographer Paul Rubin. The original link is no longer active but you can search for it. Googling currently yields a near-exact copy as well as an adaptation for Python 3.
You could also try one of several single-module, pure-Python Rijndael (AES) implementations such as this or this. (Again, links are not guaranteed to be permanent so you may have to do some searching.)

Is there a library that will detect the source code language of a block of code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Writing a python script and it needs to find out what language a block of code is written in. I could easily write this myself, but I'd like to know if a solution already exists.
Pygments is insufficient and unreliable.
Pygments can guess too. Here is an example from the documentation:
>>> from pygments.lexers import guess_lexer, guess_lexer_for_filename
>>> guess_lexer('#!/usr/bin/python\nprint "Hello World!"')
<pygments.lexers.PythonLexer>
>>> guess_lexer_for_filename('test.py', 'print "Hello World!"')
<pygments.lexers.PythonLexer>
I guess you should try what this very site uses: google-code-prettify (from this question)
[EDIT]J.F. Sebastian pointed me to Pygments (see this answer)
This can be a little difficult to do reliably. For example, what language is the following:
print("blah");
The most reliable way (aside from having the user select the correct language, of course) is to check if the first line is starts with #! ("hashbang") - whatever is after this is the intepreter for the scripting language.
That will work reliably for a lot of scripting languages (including python, shell scripting, perl, ruby etc etc..), but not for compiled languages..
You could look for unique syntax stylings, or specific keywords and weight each one towards a specific language. For example $#somevar is probably Perl. somevar.each do |another| ..... end is probably ruby.. but this would end up being a lot of work, and will not always work (especially with short code blocks)
The other obvious way is to use the file-extension. If it's *.pl it's probably Perl code..
What are you trying to achieve? If you want to syntax highlight, look at what google-code-prettify does - basically a reasonably intelligent, generic syntax highlighter..
In the above above ambiguous example, print is probably a statement or function name, "blah" is probably a string. If you highlight those two differently, you've successfully highlighted a lot of different languages, without having to detect what one it actually is.. but that may not always work, depending on the task..
Ohcount has been developed for this exactly:
http://labs.ohloh.net/ohcount
They are using it at www.ohloh.net to count the contribution of people in languages.
The bad news is that it is coded in ruby, but I am sure that you can integrate it one way or the other in python.
Since you asked this question, GitHub have released the code they use to detect programming languages, Linguist. In my experience, GitHub is very accurate.
Language detection
Linguist defines the list of all languages known to GitHub in a yaml file. In order for a file to be highlighted, a language and lexer must be defined there.
Most languages are detected by their file extension. This is the fastest and most common situation.
For disambiguating between files with common extensions, we use a bayesian classifier. For an example, this helps us tell the difference between .h files which could be either C, C++, or Obj-C.
Ruby gem: http://rubygems.org/gems/github-linguist
If you can't use Ruby for whatever reason, the logic is simple enough to port https://github.com/github/linguist/blob/master/lib/linguist/language.rb
Vim uses a bunch of interesting tests and regular expressions to look for certain file formats. You can look at the vim instruction file at vim/vim71/filetype.vim, or here online.
what language a block of code is written in
What are your alternatives, among what languages? There is no way to determine this universally. But if you narrow your focus there is probably a tool somewhere
You can check highlight.js which automatically highlights the code block, they say they are using some kind of heuristic methods to accomplish this http://softwaremaniacs.org/soft/highlight/en/
As other have said Pygments will be your best bet.

Categories