Skip to content

Crypt API

The crypt library provides a minimal set of crypto for connected devices. It provides Base64 encode/decode, MD5, SHA256, Bcrypt crypto and password utilities.

Function Index

boolcryptCheckPassword(cchar *plainTextPassword, cchar *passwordHash)
 Check a plain-text password against a password hash.
char *cryptDecode64(cchar *str)
 Decode a block that has been base64 encoded.
char *cryptDecode64Block(cchar *block, ssize *len, int flags)
 Decode a block that has been base64 encoded.
char *cryptEncode64(cchar *str)
 Encode a string using base64 encoding.
char *cryptEncode64Block(cuchar *block, ssize len)
 Encode a block using base64 encoding.
char *cryptGetFileMd5(cchar *path)
 Get an MD5 string hash for a file.
char *cryptGetFileSha1(cchar *path)
 Get a SHA1 hash for the contents of a file.
char *cryptGetFileSha256(cchar *path)
 Get a SHA256 hash for the contents of a file.
char *cryptGetMd5(uchar *block, ssize length)
 Get an MD5 hash for a block and return a string hash.
voidcryptGetMd5Block(uchar *block, ssize length, uchar hash[CRYPT_MD5_SIZE])
 Get an MD5 hash for a block and return a binary hash.
char *cryptGetPassword(cchar *prompt)
 Read a password from the console.
intcryptGetRandomBytes(uchar *buf, ssize length, bool block)
 Get random data.
char *cryptGetSha1(cuchar *block, ssize length)
 Get a SHA1 hash for a block and return a string hash.
char *cryptGetSha1Base64(cchar *s, ssize length)
 Get a SHA1 hash for a string and return a base-64 encoded string hash.
voidcryptGetSha1Block(cuchar *block, ssize length, uchar hash[CRYPT_SHA1_SIZE])
 Get a SHA1 hash for a block and return a binary hash.
char *cryptGetSha1WithPrefix(cuchar *buf, ssize length, cchar *prefix)
 Get an SHA1 checksum with optional prefix string and buffer length.
char *cryptGetSha256(cuchar *block, ssize length)
 Get a SHA256 hash for a block and return a string hash.
char *cryptGetSha256Base64(cchar *s, ssize length)
 Get a SHA256 hash for a string and return a base-64 encoded string hash.
voidcryptGetSha256Block(cuchar *block, ssize length, uchar hash[CRYPT_SHA256_SIZE])
 Get a SHA256 hash for a block and return a binary hash.
char *cryptMakePassword(cchar *password, int saltLength, int rounds)
 Make a password using the Blowfish cipher (Bcrypt).
voidcryptMd5Finalize(CryptMd5 *ctx, uchar digest[CRYPT_MD5_SIZE]),typedef,struct,CryptSha1
 Low level MD5 hashing API to finalize an MD5 hash compuation and return a binary hash result.
char *cryptMd5HashToString(uchar hash[CRYPT_MD5_SIZE])
 Convert an MD5 hash to a hex string.
voidcryptMd5Init(CryptMd5 *ctx)
 Low level MD5 hashing API to initialize an MD5 hash computation.
voidcryptMd5Update(CryptMd5 *ctx, uchar *block, uint length)
 Low level MD5 hashing API to update an MD5 hash computation with a block of data.
voidcryptSha1Finalize(CryptSha1 *ctx, uchar *hash)
 Low level SHA1 hashing API to finalize a SHA1 hash compuation and return a binary result.
char *cryptSha1HashToString(uchar hash[CRYPT_SHA1_SIZE])
 Convert a SHA1 hash to a string.
voidcryptSha1Init(CryptSha1 *ctx)
 Low level SHA1 hashing API to initialize a SHA1 hash computation.
voidcryptSha1Start(CryptSha1 *ctx)
 Low level SHA1 hashing API to start a SHA1 hash computation.
voidcryptSha1Term(CryptSha1 *ctx)
 Low level SHA1 hashing API to terminate a SHA1 hash compuation.
voidcryptSha1Update(CryptSha1 *ctx, cuchar *block, ssize length)
 Low level SHA1 hashing API to update a SHA1 hash computation with input data.
voidcryptSha256Finalize(CryptSha256 *ctx, uchar hash[CRYPT_SHA256_SIZE])
 Low level SHA256 hashing API to finalize a SHA256 hash compuation and return a binary result.
char *cryptSha256HashToString(uchar hash[CRYPT_SHA256_SIZE])
 Convert a SHA256 hash to a string.
voidcryptSha256Init(CryptSha256 *ctx)
 Low level SHA256 hashing API to initialize a SHA256 hash computation.
voidcryptSha256Start(CryptSha256 *ctx)
 Low level SHA256 hashing API to start a SHA256 hash computation.
voidcryptSha256Term(CryptSha256 *ctx)
 Low level SHA256 hashing API to terminate a SHA256 hash compuation.
voidcryptSha256Update(CryptSha256 *ctx, cuchar *block, ssize length)
 Low level SHA256 hashing API to update a SHA256 hash computation with input data.

Typedef Index

CryptMd5MD5 computation block.
CryptSha256SHA256 computation block.

Defines

#defineCRYPT_BLOWFISH   "BF1"
 Blowfish hash tag.
#defineCRYPT_BLOWFISH_ROUNDS   128
 Number of computation rounds.
#defineCRYPT_BLOWFISH_SALT_LENGTH   16
 Length of salt text.
#defineCRYPT_DECODE_TOKEQ   1
 Decode base64 blocks up to a NULL or equals.
#defineME_COM_CRYPT   1
 Minimal Crypto Library.

Typedefs

CryptMd5

MD5 computation block.

API Stability:
Internal.
Fields:
uintstate[4] MD5 hashing state.

CryptSha256

SHA256 computation block.

API Stability:
Internal.
Fields:
uint32state[8] SHA256 computation state.

Functions

bool cryptCheckPassword (cchar *plainTextPassword, cchar *passwordHash)

Check a plain-text password against a password hash.

Parameters:
plainTextPasswordInput plain-text password.
passwordHashHash previously computed via cryptMakePassword.
Returns:
True if the password matches.
API Stability:
Evolving.

char * cryptDecode64 (cchar *str)

Decode a block that has been base64 encoded.

Parameters:
strBase64 encoded string.
Returns:
Null terminated decoded string. Caller must free.
API Stability:
Evolving.

char * cryptDecode64Block (cchar *block, ssize *len, int flags)

Decode a block that has been base64 encoded.

Parameters:
blockBase64 encoded string.
lenPointer to receive the length of the decoded block.
flagsStop decoding at the end of the block or '=' if CRYPT_DECODE_TOKEQ is specified.
Returns:
Decoded block string. Caller must free. The length is described via *len.
API Stability:
Evolving.

char * cryptEncode64 (cchar *str)

Encode a string using base64 encoding.

Parameters:
strNull terminated string to encode.
Returns:
Base64 encoded string. Caller must free.
API Stability:
Evolving.

char * cryptEncode64Block (cuchar *block, ssize len)

Encode a block using base64 encoding.

Parameters:
blockBlock of data to encode.
lenLength of the block.
Returns:
Base64 encoded string. Caller must free.
API Stability:
Evolving.

char * cryptGetFileMd5 (cchar *path)

Get an MD5 string hash for a file.

Parameters:
pathFilename for the file for which to compute the hash.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

char * cryptGetFileSha1 (cchar *path)

Get a SHA1 hash for the contents of a file.

Parameters:
pathFilename of the file.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

char * cryptGetFileSha256 (cchar *path)

Get a SHA256 hash for the contents of a file.

Parameters:
pathFilename of the file.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

char * cryptGetMd5 (uchar *block, ssize length)

Get an MD5 hash for a block and return a string hash.

Parameters:
blockBlock of data for which to compute the hash.
lengthLength of the block. If the length is -1, the block is assumed to be a string and its length is determined by strlen on the block.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

void cryptGetMd5Block (uchar *block, ssize length, uchar hash)

Get an MD5 hash for a block and return a binary hash.

Parameters:
blockBlock of data for which to compute the hash.
lengthLength of the block. If the length is -1, the block is assumed to be a string and its length is determined by strlen on the block.
hashArray to receive the hash.
API Stability:
Evolving.

char * cryptGetPassword (cchar *prompt)

Read a password from the console.

Description:
Used by utility programs to read passwords from the console.
Parameters:
promptPassword user prompt.
Returns:
The input password. Caller must free.
API Stability:
Evolving.

int cryptGetRandomBytes (uchar *buf, ssize length, bool block)

Get random data.

Parameters:
bufResult buffer to hold the random data.
lengthSize of the buffer.
blockSet to true to read from a blocking random generator that will guarantee the return of random data in the situation of insufficient entropy at the time the call was made.
Returns:
The input password. Caller must free.
API Stability:
Evolving.

char * cryptGetSha1 (cuchar *block, ssize length)

Get a SHA1 hash for a block and return a string hash.

Parameters:
blockBlock of data for which to compute the hash. If set to -1, the block is assumed to be a null terminated string.
lengthLength of the data block.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

char * cryptGetSha1Base64 (cchar *s, ssize length)

Get a SHA1 hash for a string and return a base-64 encoded string hash.

Parameters:
sString.
lengthLength of the data block. If set to <= 0, the block is assumed to be a null terminated string.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

void cryptGetSha1Block (cuchar *block, ssize length, uchar hash)

Get a SHA1 hash for a block and return a binary hash.

Parameters:
blockBlock of data for which to compute the hash.
lengthLength of the data block. If set to -1, the block is assumed to be a null terminated string.
hashArray to receive the hash result.
API Stability:
Evolving.

char * cryptGetSha1WithPrefix (cuchar *buf, ssize length, cchar *prefix)

Get an SHA1 checksum with optional prefix string and buffer length.

Parameters:
bufBuffer to checksum.
lengthSize of the buffer.
prefixString prefix to insert at the start of the result.
Returns:
An allocated string containing an SHA1 checksum.
API Stability:
Evolving.

char * cryptGetSha256 (cuchar *block, ssize length)

Get a SHA256 hash for a block and return a string hash.

Parameters:
blockBlock of data for which to compute the hash. If set to -1, the block is assumed to be a null terminated string.
lengthLength of the data block.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

char * cryptGetSha256Base64 (cchar *s, ssize length)

Get a SHA256 hash for a string and return a base-64 encoded string hash.

Parameters:
sString.
lengthLength of the data block. If set to <= 0, the block is assumed to be a null terminated string.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

void cryptGetSha256Block (cuchar *block, ssize length, uchar hash)

Get a SHA256 hash for a block and return a binary hash.

Parameters:
blockBlock of data for which to compute the hash.
lengthLength of the data block. If set to -1, the block is assumed to be a null terminated string.
hashArray to receive the hash result.
API Stability:
Evolving.

char * cryptMakePassword (cchar *password, int saltLength, int rounds)

Make a password using the Blowfish cipher (Bcrypt).

Parameters:
passwordInput plain-text password.
saltLengthLength of salt text to add.
roundsNumber of computation rounds. Default is 128. Longer is slower, but more secure.
Returns:
The computed password hash. Caller must free.
API Stability:
Evolving.

void cryptMd5Finalize (CryptMd5 *ctx, uchar digest)

Low level MD5 hashing API to finalize an MD5 hash compuation and return a binary hash result.

Description:
Finalize the hash computation.
Parameters:
ctxMD5 context.
digestMD5 array to receive the hash result.
API Stability:
Evolving.
API Stability:
Internal.

char * cryptMd5HashToString (uchar hash)

Convert an MD5 hash to a hex string.

Parameters:
hashPreviously computed MD5 hash.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

void cryptMd5Init (CryptMd5 *ctx)

Low level MD5 hashing API to initialize an MD5 hash computation.

Description:
Initialize the hash computation.
Parameters:
ctxMD5 context.
API Stability:
Evolving.

void cryptMd5Update (CryptMd5 *ctx, uchar *block, uint length)

Low level MD5 hashing API to update an MD5 hash computation with a block of data.

Description:
Update the hash computation with input.
Parameters:
ctxMD5 context.
blockInput block to add to the hash.
lengthLength of the input block.
API Stability:
Evolving.

void cryptSha1Finalize (CryptSha1 *ctx, uchar *hash)

Low level SHA1 hashing API to finalize a SHA1 hash compuation and return a binary result.

Description:
Finalize the hash computation and return a binary hash result.
Parameters:
ctxSHA1 context.
hashArray to receive the hash result.
API Stability:
Evolving.

char * cryptSha1HashToString (uchar hash)

Convert a SHA1 hash to a string.

Parameters:
hashHash result from cryptGetSha1Block.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

void cryptSha1Init (CryptSha1 *ctx)

Low level SHA1 hashing API to initialize a SHA1 hash computation.

Description:
Initialize the hash computation.
Parameters:
ctxSHA1 context.
API Stability:
Evolving.

void cryptSha1Start (CryptSha1 *ctx)

Low level SHA1 hashing API to start a SHA1 hash computation.

Description:
Start the hash computation.
Parameters:
ctxSHA1 context.
API Stability:
Evolving.

void cryptSha1Term (CryptSha1 *ctx)

Low level SHA1 hashing API to terminate a SHA1 hash compuation.

Description:
Terminate (conclude) the hash computation. This erases in-memory state and should be the final step in computing a hash.
Parameters:
ctxSHA1 context.
API Stability:
Evolving.

void cryptSha1Update (CryptSha1 *ctx, cuchar *block, ssize length)

Low level SHA1 hashing API to update a SHA1 hash computation with input data.

Description:
Update the hash computation with a block of data.
Parameters:
ctxSHA1 context.
blockBlock of data to hash.
lengthLength of the input block.
API Stability:
Evolving.

void cryptSha256Finalize (CryptSha256 *ctx, uchar hash)

Low level SHA256 hashing API to finalize a SHA256 hash compuation and return a binary result.

Description:
Finalize the hash computation and return a binary hash result.
Parameters:
ctxSHA256 context.
hashArray to receive the hash result.
API Stability:
Evolving.

char * cryptSha256HashToString (uchar hash)

Convert a SHA256 hash to a string.

Parameters:
hashHash result from cryptGetSha256Block.
Returns:
A hex string representation of the hash. Caller must free.
API Stability:
Evolving.

void cryptSha256Init (CryptSha256 *ctx)

Low level SHA256 hashing API to initialize a SHA256 hash computation.

Description:
Initialize the hash computation.
Parameters:
ctxSHA256 context.
API Stability:
Evolving.

void cryptSha256Start (CryptSha256 *ctx)

Low level SHA256 hashing API to start a SHA256 hash computation.

Description:
Start the hash computation.
Parameters:
ctxSHA256 context.
API Stability:
Evolving.

void cryptSha256Term (CryptSha256 *ctx)

Low level SHA256 hashing API to terminate a SHA256 hash compuation.

Description:
Terminate (conclude) the hash computation. This erases in-memory state and should be the final step in computing a hash.
Parameters:
ctxSHA256 context.
API Stability:
Evolving.

void cryptSha256Update (CryptSha256 *ctx, cuchar *block, ssize length)

Low level SHA256 hashing API to update a SHA256 hash computation with input data.

Description:
Update the hash computation with a block of data.
Parameters:
ctxSHA256 context.
blockBlock of data to hash.
lengthLength of the input block.
API Stability:
Evolving.