Reputation: 1224
Function crypt()
returns a string like TyVkFBglLfEGw
, when on my Ubuntu machine all passwords are in form like
$6$Ty8Ew9/O$V/ck4Apc7VOOqobhenO5.f6ccsVdCpQy5H6fyuNzCaDG.LxFnUWKHOobkpRQQtl.1cFG8BLAdfWjNbLiSxKXF/
I think my machine uses SHA512 algorithm. How can I create hashed strings like in /etc/shadow
file from a C program?
Upvotes: 2
Views: 2037
Reputation: 94779
If you read the man page for crypt; specifically the piece about Glibc notes, it mentions the following:
The glibc2 version of this function supports additional encryption
algorithms.
If salt is a character string starting with the characters "$id$"
followed by a string terminated by "$":
$id$salt$encrypted
then instead of using the DES machine, id identifies the encryption method used and this then determines how the rest of the password string is interpreted. The following values of id are supported:
ID | Method
─────────────────────────────────────────────────────────
1 | MD5
2a | Blowfish (not in mainline glibc; added in some
| Linux distributions)
5 | SHA-256 (since glibc 2.7)
6 | SHA-512 (since glibc 2.7)
So $5$salt$encrypted is an SHA-256 encoded password and $6$salt$encrypted is an SHA-512 encoded one.
this means that you should pass in a string as salt that contains the string $6$salt
to get it to generate a sha-512 crypt e.g.
char *salt = "$6$pt4wu5ns";
char *password = "muppet show";
printf("%s\n", crypt(password, salt));
Upvotes: 4
Reputation: 6404
Here's a website on how to implement the sha1 algorithm.
Normally I would describe the algorithm, but really there;s nothing to it, just mix up the bits with a complex series of adds and shifts. The tricky bit is getting it to exactly conform to everyone else;s sha1 code.
Upvotes: -3