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.

Extensions

Crypt Minimal Crypto Library.

Functions

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(cchar *block, ssize len)
 Encode a block using base64 encoding.
char*cryptGetFileMd5(cchar *path)
 Get an MD5 string hash for 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(char *buf, ssize length, bool block)
 Get random data.
char*cryptGetSha256(cuchar *block, ssize length)
 Get a SHA256 hash for a block and return a 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(RMd5 *ctx, uchar digest[CRYPT_MD5_SIZE]),typedef,struct,RSha256
 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(RMd5 *ctx)
 Low level MD5 hashing API to initialize an MD5 hash computation.
voidcryptMd5Update(RMd5 *ctx, uchar *block, uint length)
 Low level MD5 hashing API to update an MD5 hash computation with a block of data.
voidcryptSha256Finalize(RSha256 *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(RSha256 *ctx)
 Low level SHA256 hashing API to initialize a SHA256 hash computation.
voidcryptSha256Start(RSha256 *ctx)
 Low level SHA256 hashing API to start a SHA256 hash computation.
voidcryptSha256Term(RSha256 *ctx)
 Low level SHA256 hashing API to terminate a SHA256 hash compuation.
voidcryptSha256Update(RSha256 *ctx, cuchar *block, int length)
 Low level SHA256 hashing API to update a SHA256 hash computation with input data.

Typedefs

RMd5MD5 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.

Crypt

Crypt

Minimal Crypto Library.

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

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.
See Also:
cryptGetPassword, cryptMakePassword

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.
See Also:
cryptEncode64

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.
See Also:
cryptDecode64, cryptEncode64, cryptEncode64Block

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.
See Also:
cryptEncode64Block

char * cryptEncode64Block (cchar *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.
See Also:
cryptEncode64

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.
See Also:
cryptGetMd5, cryptGetMd5Block

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.
See Also:
cryptGetSha256, cryptGetSha256Block

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.
See Also:
cryptGetMd5Block

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.
See Also:
cryptGetMd5

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.
See Also:
cryptMakePassword

int cryptGetRandomBytes (char *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.
See Also:
cryptMakePassword

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.
See Also:

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.
See Also:
cryptGetFileSha256, cryptGetSha256

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.
See Also:
cryptCheckPassword, cryptGetPassword

void cryptMd5Finalize (RMd5 *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.
See Also:
cryptMd5Init, cryptMd5Update
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.
See Also:
cryptGetMd5, cryptGetMd5Block

void cryptMd5Init (RMd5 *ctx)

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

Description:
Initialize the hash computation.
Parameters:
ctxMD5 context.
API Stability:
Evolving.
See Also:
cryptMd5Finalize, cryptMd5Update

void cryptMd5Update (RMd5 *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.
See Also:
cryptMd5Finalize, cryptMd5Init

void cryptSha256Finalize (RSha256 *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.
See Also:
cryptSha256Init, cryptSha256Start, cryptSha256Term, cryptSha256Update

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.
See Also:
cryptGetSha256, cryptGetSha256Block

void cryptSha256Init (RSha256 *ctx)

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

Description:
Initialize the hash computation.
Parameters:
ctxSHA256 context.
API Stability:
Evolving.
See Also:
cryptSha256Finalize, cryptSha256Start, cryptSha256Update

void cryptSha256Start (RSha256 *ctx)

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

Description:
Start the hash computation.
Parameters:
ctxSHA256 context.
API Stability:
Evolving.
See Also:
cryptSha256Finalize, cryptSha256Init, cryptSha256Term, cryptSha256Update

void cryptSha256Term (RSha256 *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.
See Also:
cryptSha256Finalize, cryptSha256Init, cryptSha256Start, cryptSha256Update

void cryptSha256Update (RSha256 *ctx, cuchar *block, int 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.
See Also:
cryptSha256Finalize, cryptSha256Init, cryptSha256Start, cryptSha256Term

Typedefs

RMd5

MD5 computation block.

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