0

I'm currently writing a program in C on OpenBSD. I need to compute a SHA256 hash of several strings. I know there is a tool sha256 that I could use on the console, but I need to use either that or a native Unix library to compute it.

Is there any .h file I have to include I may have missed?

I already tried to search the man pages and the internet, but ended every time with the console tool, but I need to call it via my C program. I know there is a library with crypt, which supports Blowfish; I'd need a library with sha256 to call it like the blowfish version:

printf("hash: %s" crypt(tohash, salt));

Edit: the proposed post with solution doesn't address this problem, since the post addresses C++ and OpenSSL; here we have only C and no external libraries.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Thoor
  • 137
  • 1
  • 8
  • If you don't want to use an external library, you have to either roll your own, crib from someone else's external library, or use the `sha256` command. Roll your own is hard; cribbing from someone else's llibrary isn't really different from using the external library (OpenSSL is merely one library that provides SHA256 support) and using the external tool is far from impossible — you probably can't use `popen()` and `pclose()` because you almost certainly need bidirectional communication, so you end up using `pipe()`, `fork()`, `dup2()`, `execvp()`, `read()`, `write()`, `close()`. – Jonathan Leffler May 09 '16 at 23:01
  • Your question is about OpenBSD. On OpenBSD libc by default contains SHA2 functions. the header is called `sha2.h` and you can find its documentation with the usual high quality `man SHA256Init`. It's not entirely clear what crypt-like functionality you need though. – Art May 10 '16 at 06:50

0 Answers0