1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-09-28 19:05:55 +08:00
ULib/tests/base/crypto_dgst.c
stefanocasazza b6ee1e71fd fix
2017-07-25 12:41:44 +02:00

68 lines
1.3 KiB
C

// crypto_dgst.c
#include <ulib/base/utility.h>
#include <ulib/base/ssl/dgst.h>
#include <stdlib.h>
#include <openssl/ssl.h>
#define U_BUFLEN (4096 * 1)
static const char* usage = "Usage: crypto_dgst algo key keylen\n";
static void u_do_cipher(int alg, const char* key, uint32_t keylen)
{
static unsigned char buf[U_BUFLEN];
U_INTERNAL_TRACE("u_do_cipher(%d,%.*s,%u)", alg, keylen, key, keylen)
u_dgst_init(alg, key, keylen);
while (true)
{
int readlen = read(STDIN_FILENO, buf, sizeof(buf));
U_INTERNAL_PRINT("readlen = %d", readlen)
if (readlen <= 0)
{
if (!readlen) break;
else
{
perror("read");
exit(1);
}
}
U_INTERNAL_PRINT("buf = %.*s", readlen, buf)
u_dgst_hash(buf, readlen);
}
# ifdef __MINGW32__
(void) setmode(1, O_BINARY);
# endif
write(STDOUT_FILENO, buf, u_dgst_finish(buf, 0));
}
int main(int argc, char** argv)
{
u_init_ulib(argv);
U_INTERNAL_TRACE("main(%d,%p)", argc, argv)
U_INTERNAL_PRINT("argv[0] = %s\nargv[1] = %s", argv[0], argv[1])
if (argc == 4) u_do_cipher(u_atoi(argv[1]), argv[2], u_atoi(argv[3]));
else
{
fprintf(stderr, "%s", usage);
exit(1);
}
return 0;
}