All pastes #970197 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-04-03 21:43 UTC
rendered paste body
/**
 * Compute the SHA-1 hash of the given data as specified by the 'data' and
 * 'len' arguments, and place the result -- 160 bits (20 bytes) -- into the
 * specified output buffer 'buf'.
 *
 * The result is stored in a compact format in 'buf' -- each byte in 'buf'
 * yields two output characters as displayed by Unix tools such as 'sha1sum'
 * (i.e. each byte is displayed as hex-value).
 *
 * You can use '%02x' in printf() for printing the 20 bytes from 'buf', which
 * will result in 40 characters displayed on the screen.
 *
 * An example: 'echo -n "foo" | sha1sum' yields the hash
 *
 *   0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
 *
 * where 0x0b is actually the value of the first byte of the SHA-1
 * output, 0xee is the second byte, 0xc7 is the third byte, etc.
 *
 * @param data Pointer to the input data that shall be hashed.
 * @param len Length of the input data (in bytes).
 * @param buf Buffer which will hold the resulting hash (must be at
 *            least 20 bytes in size).
 * @return Pointer to the output buffer where the hash is stored.
 */
u8 *sha1(const u8 *data, size_t len, u8 *buf)
{
        SHA1_CTX ctx;

        SHA1Init(&ctx);
        SHA1Update(&ctx, data, len);
        SHA1Final(buf, &ctx);

        return buf;
}