0

Is there any C or C++ function which can produce the same output of this python code (SHA512 with salt)?

import crypt;
crypt.crypt('test', '$6$Salte2Ck$')
m13r
  • 2,458
  • 2
  • 29
  • 39
user2521791
  • 1,621
  • 2
  • 14
  • 13
  • 1
    There's no such function in standard C or C++. You have to use an external library. – Some programmer dude Aug 04 '14 at 11:51
  • 1
    Related: [How to calculate a SHA-512 hash in C++ on Linux?](http://stackoverflow.com/q/5125041/279627) – Sven Marnach Aug 04 '14 at 11:55
  • any idea where to find portable external library which can do the same job? or do I need to write my own SHA512+ salt? – user2521791 Aug 04 '14 at 11:58
  • 3
    Here's how `crypt.crypt()` is implemented: http://hg.python.org/cpython/file/50722d2f08c7/Modules/cryptmodule.c It basically simply forwards to the C library function `crypt()`. – Sven Marnach Aug 04 '14 at 12:01
  • 1
    Why do you think Python's crypt uses SHA512? It doesn't seem to do so, both according to the implementation and the [documentation](https://docs.python.org/2/library/crypt.html). – Sven Marnach Aug 04 '14 at 12:03
  • because when I use this python function it produce the same hash value as SHA512 – user2521791 Aug 04 '14 at 12:12
  • @Sven: Unfortunately Python's crypt documentation is inadequate and incomplete. That's probably somewhat intentional because the underlying `crypt` function is [underspecified](http://pubs.opengroup.org/onlinepubs/009695399/functions/crypt.html) and its implementation depends on your platform and even OS version. In fact, on my (older) Linux system, it's impossible to get `crypt` to use SHA-512, but that's not true on all versions of Linux. – indiv Aug 04 '14 at 17:29
  • @indiv: I think the Python documentation is quite clear that this function simply forwards to `crypt(3)`. It even refers to the man page and mentions that details are different from platform to platform. What else would you hope for? – Sven Marnach Aug 05 '14 at 13:53
  • @sven I don't know, you are the one who said the documentation didn't say it used SHA-512 even though it does in this case and I was explaining why. But if you think the documentation is fine then that's ok too. – indiv Aug 06 '14 at 04:07
  • @indiv: I wasn't aware that there are systems where `crypt` is based on SHA512. It certainly isn't on any system I ever used. – Sven Marnach Aug 06 '14 at 13:56
  • I believe it deepen on the second parameter sent to this function.For example, calHash= crypt.crypt(input,'$6$m90MgTxl$') the $6$ in the second parameter is for SHA512 and I can verify this by checking the /etc/shadow file in my Ubuntu system which contain the passwords hash for all the users. when I call this function with the same salt and password the result is the same as the hash saved in the /etc/shadow file. – user2521791 Aug 10 '14 at 10:16

1 Answers1

3

Here are some explanations to Python's crypt function.
So I think what you are looking for is the C function crypt from unistd.h.

Community
  • 1
  • 1
m13r
  • 2,458
  • 2
  • 29
  • 39