CRYPTO_SHA512(3MONOCYPHER) | 3MONOCYPHER | CRYPTO_SHA512(3MONOCYPHER) |
crypto_sha512
,
crypto_sha512_init
,
crypto_sha512_update
,
crypto_sha512_final
—
#include <monocypher-ed25519.h>
void
crypto_sha512
(uint8_t hash[64],
const uint8_t *message, size_t
message_size);
void
crypto_sha512_init
(crypto_sha512_ctx
*ctx);
void
crypto_sha512_update
(crypto_sha512_ctx
*ctx, const uint8_t *message,
size_t message_size);
void
crypto_sha512_final
(crypto_sha512_ctx
*ctx, uint8_t hash[64]);
Note that SHA-512 itself is not suitable for hashing passwords and deriving keys from them; use the crypto_argon2i(3monocypher) family of functions for that purpose instead.
SHA-512 is vulnerable to length extension attacks; using it as a message authentication code (MAC) algorithm or keyed hash requires precautions. The crypto_hmac_sha512(3monocypher) family of functions provides HMAC with SHA-512. Use crypto_verify64(3monocypher) to compare MACs created this way.
The arguments are:
NULL
if message_size is
0.An incremental interface is provided. It is useful for handling streams of data or large files without using too much memory. This interface uses three steps:
crypto_sha512_init
(), which
sets up a context with the hashing parameters;crypto_sha512_update
(), which hashes
the message chunk by chunk and keeps the intermediary result in the
context;crypto_sha512_final
(), which
produces the final hash. The crypto_sha512_ctx is
automatically wiped upon finalisation.crypto_sha512
() is a convenience function
that performs crypto_sha512_init
(),
crypto_sha512_update
(), and
crypto_sha512_final
().
uint8_t hash [64]; /* Output hash (64 bytes) */ uint8_t message[12] = "Lorem ipsum"; /* Message to hash */ crypto_sha512(hash, message, 12);
Hashing a message incrementally:
uint8_t hash [ 64]; /* Output hash (64 bytes) */ uint8_t message[500] = {1}; /* Message to hash */ crypto_sha512_ctx ctx; crypto_sha512_init(&ctx); for (size_t i = 0; i < 500; i += 100) { crypto_sha512_update(&ctx, message + i, 100); } crypto_sha512_final(&ctx, hash);
crypto_sha512
(),
crypto_sha512_init
(),
crypto_sha512_update
(), and
crypto_sha512_final
() functions first appeared in
Monocypher 0.3, but were not intended for use outside Monocypher itself and
thus undocumented. They became part of the official API in Monocypher 3.0.0.
February 5, 2020 | Debian |