libfossil
fossil-hash.h File Reference
#include "fossil-util.h"

Go to the source code of this file.

Data Structures

struct  fsl_md5_cx
 Holds state for MD5 calculations. More...
 
struct  fsl_sha1_cx
 Holds state for SHA1 calculations. More...
 

Macros

#define fsl_md5_cx_empty_m
 
#define FSL_MD5_INITIAL_HASH   "d41d8cd98f00b204e9800998ecf8427e"
 The hash string of the initial MD5 state. More...
 
#define fsl_sha1_cx_empty_m
 fsl_sha1_cx instance intended for in-struct copy initialization. More...
 

Typedefs

typedef struct fsl_md5_cx fsl_md5_cx
 
typedef struct fsl_sha1_cx fsl_sha1_cx
 

Enumerations

enum  fsl_hash_constants {
  FSL_UUID_STRLEN = 40,
  FSL_MD5_STRLEN = 32
}
 Various set-in-stone constants used by the API. More...
 

Functions

FSL_EXPORT void fsl_md5_digest_to_base16 (unsigned char *digest, char *zBuf)
 Converts an md5 digest value (from fsl_md5_final()'s 2nd parameter) to a 32-byte (FSL_MD5_STRLEN) CRC string plus a terminating NUL byte. More...
 
FSL_EXPORT void fsl_md5_final (fsl_md5_cx *cx, unsigned char *digest)
 Finishes up the calculation of the md5 for the given context and writes a 16-byte digest value to the 2nd parameter. More...
 
FSL_EXPORT void fsl_md5_init (fsl_md5_cx *cx)
 Initializes the given context pointer. More...
 
FSL_EXPORT void fsl_md5_update (fsl_md5_cx *cx, void const *buf, fsl_size_t len)
 Updates cx's state to reflect the addition of the data specified by the range (buf, buf+len]. More...
 
FSL_EXPORT void fsl_md5_update_buffer (fsl_md5_cx *cx, fsl_buffer const *b)
 Equivalent to fsl_md5_update(cx, b->mem, b->used). More...
 
FSL_EXPORT void fsl_md5_update_cstr (fsl_md5_cx *cx, char const *str, fsl_int_t len)
 Passes the first len bytes of str to fsl_md5_update(cx). More...
 
FSL_EXPORT int fsl_md5_update_filename (fsl_md5_cx *cx, char const *fname)
 A fsl_md5_update_stream() proxy which updates cx to include the contents of the given file. More...
 
FSL_EXPORT int fsl_md5_update_stream (fsl_md5_cx *cx, fsl_input_f src, void *srcState)
 Reads all input from src() and passes it through fsl_md5_update(cx,...). More...
 
FSL_EXPORT int fsl_md5sum_buffer (fsl_buffer const *pIn, fsl_buffer *pCksum)
 The md5 counterpart of fsl_sha1sum_buffer(), identical in semantics except that its result is an MD5 hash instead of an SHA1 hash and the resulting hex string is FSL_MD5_STRLEN bytes long plus a terminating NUL. More...
 
FSL_EXPORT char * fsl_md5sum_cstr (const char *zIn, fsl_int_t len)
 The md5 counterpart of fsl_sha1sum_cstr(), identical in semantics except that its result is an MD5 hash instead of an SHA1 hash and the resulting string is FSL_MD5_STRLEN bytes long plus a terminating NUL. More...
 
FSL_EXPORT int fsl_md5sum_filename (const char *zFilename, fsl_buffer *pCksum)
 The MD5 counter part to fsl_sha1sum_filename(), with identical semantics except that the generated hash is an MD5 string instead of SHA1. More...
 
FSL_EXPORT int fsl_md5sum_stream (fsl_input_f src, void *srcState, fsl_buffer *pCksum)
 The MD5 counter part to fsl_sha1sum_stream(), with identical semantics except that the generated hash is an MD5 string instead of SHA1. More...
 
FSL_EXPORT void fsl_sha1_digest_to_base16 (unsigned char *digest, char *zBuf)
 Convert a digest into base-16. More...
 
FSL_EXPORT void fsl_sha1_final (fsl_sha1_cx *context, unsigned char *digest)
 Add padding and finalizes the message digest. More...
 
FSL_EXPORT void fsl_sha1_init (fsl_sha1_cx *context)
 Initializes the given context with the initial SHA1 state. More...
 
FSL_EXPORT void fsl_sha1_update (fsl_sha1_cx *context, void const *data, fsl_size_t len)
 Updates the given context to include the hash of the first len bytes of the given data. More...
 
FSL_EXPORT int fsl_sha1sum_buffer (fsl_buffer const *pIn, fsl_buffer *pCksum)
 Computes the SHA1 checksum of pIn and stores the resulting checksum in the buffer pCksum. More...
 
FSL_EXPORT char * fsl_sha1sum_cstr (const char *zIn, fsl_int_t len)
 Computes the SHA1 checksum of the first len bytes of the given string. More...
 
FSL_EXPORT int fsl_sha1sum_filename (const char *zFilename, fsl_buffer *pCksum)
 A fsl_sha1sum_stream() wrapper which calculates the SHA1 of given file. More...
 
FSL_EXPORT int fsl_sha1sum_stream (fsl_input_f src, void *srcState, fsl_buffer *pCksum)
 Consumes all input from src and calculates its SHA1 hash. More...
 

Variables

FSL_EXPORT const fsl_md5_cx fsl_md5_cx_empty
 A fsl_md5_cx instance which holds the initial state used for md5 calculations. More...
 
FSL_EXPORT const fsl_sha1_cx fsl_sha1_cx_empty
 fsl_sha1_cx instance intended for copy initialization. More...
 

Macro Definition Documentation

#define fsl_md5_cx_empty_m
Value:
{ \
1/*isInit*/, \
{/*buf*/0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 }, \
{/*bits*/0,0}, \
{0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, \
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, \
0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, \
0,0,0,0}}

Definition at line 86 of file fossil-hash.h.

#define FSL_MD5_INITIAL_HASH   "d41d8cd98f00b204e9800998ecf8427e"

The hash string of the initial MD5 state.

Used as an optimization for some places where we need an MD5 but know it will not hash any data.

Equivalent to what the md5sum command outputs for empty input:

1 # md5sum < /dev/null
2 d41d8cd98f00b204e9800998ecf8427e -

Definition at line 61 of file fossil-hash.h.

#define fsl_sha1_cx_empty_m
Value:
{ \
{0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 }, \
{0,0}, \
{0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, \
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 \
} \
}

fsl_sha1_cx instance intended for in-struct copy initialization.

Definition at line 229 of file fossil-hash.h.

Typedef Documentation

typedef struct fsl_md5_cx fsl_md5_cx

Definition at line 46 of file fossil-hash.h.

typedef struct fsl_sha1_cx fsl_sha1_cx

Definition at line 47 of file fossil-hash.h.

Enumeration Type Documentation

Various set-in-stone constants used by the API.

Enumerator
FSL_UUID_STRLEN 

The length, in bytes, of fossil's hex-form UUID strings.

FSL_MD5_STRLEN 

The length, in bytes, of a hex-form MD5 hash.

Definition at line 34 of file fossil-hash.h.

Function Documentation

FSL_EXPORT void fsl_md5_digest_to_base16 ( unsigned char *  digest,
char *  zBuf 
)

Converts an md5 digest value (from fsl_md5_final()'s 2nd parameter) to a 32-byte (FSL_MD5_STRLEN) CRC string plus a terminating NUL byte.

i.e. zBuf must be at least (FSL_MD5_STRLEN+1) bytes long.

See also
fsl_md5_final()
FSL_EXPORT void fsl_md5_final ( fsl_md5_cx cx,
unsigned char *  digest 
)

Finishes up the calculation of the md5 for the given context and writes a 16-byte digest value to the 2nd parameter.

Use fsl_md5_digest_to_base16() to convert the digest output value to hexidecimal form.

See also
fsl_md5_init()
fsl_md5_update()
fsl_md5_digest_to_base16()
FSL_EXPORT void fsl_md5_init ( fsl_md5_cx cx)

Initializes the given context pointer.

It must not be NULL. This must be the first routine called on any fsl_md5_cx instances. Alternately, copy-constructing fsl_md5_cx_empty has the same effect.

See also
fsl_md5_update()
fsl_md5_final()
FSL_EXPORT void fsl_md5_update ( fsl_md5_cx cx,
void const *  buf,
fsl_size_t  len 
)

Updates cx's state to reflect the addition of the data specified by the range (buf, buf+len].

Neither cx nor buf may be NULL. This may be called an arbitrary number of times between fsl_md5_init() and fsl_md5_final().

See also
fsl_md5_init()
fsl_md5_final()
FSL_EXPORT void fsl_md5_update_buffer ( fsl_md5_cx cx,
fsl_buffer const *  b 
)

Equivalent to fsl_md5_update(cx, b->mem, b->used).

Results are undefined if either pointer is invalid or NULL.

FSL_EXPORT void fsl_md5_update_cstr ( fsl_md5_cx cx,
char const *  str,
fsl_int_t  len 
)

Passes the first len bytes of str to fsl_md5_update(cx).

If len is less than 0 then fsl_strlen() is used to calculate the length. Results are undefined if either pointer is invalid or NULL. This is a no-op if !len or (len<0 && !*str).

FSL_EXPORT int fsl_md5_update_filename ( fsl_md5_cx cx,
char const *  fname 
)

A fsl_md5_update_stream() proxy which updates cx to include the contents of the given file.

FSL_EXPORT int fsl_md5_update_stream ( fsl_md5_cx cx,
fsl_input_f  src,
void *  srcState 
)

Reads all input from src() and passes it through fsl_md5_update(cx,...).

Returns 0 on success, FSL_RC_MISUSE if !cx or !src. If src returns a non-0 code, that code is returned from here.

FSL_EXPORT int fsl_md5sum_buffer ( fsl_buffer const *  pIn,
fsl_buffer pCksum 
)

The md5 counterpart of fsl_sha1sum_buffer(), identical in semantics except that its result is an MD5 hash instead of an SHA1 hash and the resulting hex string is FSL_MD5_STRLEN bytes long plus a terminating NUL.

FSL_EXPORT char* fsl_md5sum_cstr ( const char *  zIn,
fsl_int_t  len 
)

The md5 counterpart of fsl_sha1sum_cstr(), identical in semantics except that its result is an MD5 hash instead of an SHA1 hash and the resulting string is FSL_MD5_STRLEN bytes long plus a terminating NUL.

FSL_EXPORT int fsl_md5sum_filename ( const char *  zFilename,
fsl_buffer pCksum 
)

The MD5 counter part to fsl_sha1sum_filename(), with identical semantics except that the generated hash is an MD5 string instead of SHA1.

FSL_EXPORT int fsl_md5sum_stream ( fsl_input_f  src,
void *  srcState,
fsl_buffer pCksum 
)

The MD5 counter part to fsl_sha1sum_stream(), with identical semantics except that the generated hash is an MD5 string instead of SHA1.

FSL_EXPORT void fsl_sha1_digest_to_base16 ( unsigned char *  digest,
char *  zBuf 
)

Convert a digest into base-16.

digest must be at least 20 bytes long and hold an SHA1 digest. zBuf must be at least (FSL_UUID_STRLEN + 1) bytes long, for FSL_UUID_STRLEN characters of hexidecimal-form SHA1 hash and 1 NUL byte.

See also
fsl_sha1_final()
FSL_EXPORT void fsl_sha1_final ( fsl_sha1_cx context,
unsigned char *  digest 
)

Add padding and finalizes the message digest.

If digest is not NULL then it writes 20 bytes of digest to the 2nd parameter.

See also
fsl_sha1_update()
fsl_sha1_digest_to_base16()
FSL_EXPORT void fsl_sha1_init ( fsl_sha1_cx context)

Initializes the given context with the initial SHA1 state.

This must be the first routine called on an SHA1 context, and passing this context to other SHA1 routines without first having passed it to this will lead to undefined results.

See also
fsl_sha1_update()
fsl_sha1_final()
FSL_EXPORT void fsl_sha1_update ( fsl_sha1_cx context,
void const *  data,
fsl_size_t  len 
)

Updates the given context to include the hash of the first len bytes of the given data.

See also
fsl_sha1_init()
fsl_sha1_final()
FSL_EXPORT int fsl_sha1sum_buffer ( fsl_buffer const *  pIn,
fsl_buffer pCksum 
)

Computes the SHA1 checksum of pIn and stores the resulting checksum in the buffer pCksum.

pCksum's memory is re-used if is has any allocated to it. pCksum may == pIn, in which case this is a destructive operation (replacing the hashed data with its hash code).

Return 0 on success, FSL_RC_OOM if (re)allocating pCksum fails.

FSL_EXPORT char* fsl_sha1sum_cstr ( const char *  zIn,
fsl_int_t  len 
)

Computes the SHA1 checksum of the first len bytes of the given string.

If len is negative then zInt must be NUL-terminated and fsl_strlen() is used to find its length. The result is a FSL_UUID_STRLEN-byte string (+NUL byte) returned in memory obtained from fsl_malloc(), so it must be passed to fsl_free() to free it. If NULL==zIn or !len then NULL is returned.

FSL_EXPORT int fsl_sha1sum_filename ( const char *  zFilename,
fsl_buffer pCksum 
)

A fsl_sha1sum_stream() wrapper which calculates the SHA1 of given file.

Returns FSL_RC_IO if the file cannot be opened, FSL_RC_MISUSE if !zFilename or !pCksum, else as per fsl_sha1sum_stream().

TODO: the v1 impl has special behaviour for symlinks which this function lacks. For that support we need a variant of this function which takes a fsl_cx parameter (for the allow-symlinks setting).

FSL_EXPORT int fsl_sha1sum_stream ( fsl_input_f  src,
void *  srcState,
fsl_buffer pCksum 
)

Consumes all input from src and calculates its SHA1 hash.

The result is set in pCksum (its contents, if any, are overwritten, not appended to). Returns 0 on success. Returns FSL_RC_MISUSE if !src or !pCksum. It keeps consuming input from src() until that function reads fewer bytes than requested, at which point EOF is assumed. If src() returns non-0, that code is returned from this function.

Variable Documentation

FSL_EXPORT const fsl_md5_cx fsl_md5_cx_empty

A fsl_md5_cx instance which holds the initial state used for md5 calculations.

Instances must either be copy-initialized from this instance or they must be passed to fsl_md5_init() before they are used.

Definition at line 101 of file fossil-hash.h.

FSL_EXPORT const fsl_sha1_cx fsl_sha1_cx_empty

fsl_sha1_cx instance intended for copy initialization.

Its contents are equivalent to those set by fsl_sha1_init().

Definition at line 241 of file fossil-hash.h.