### Install nimcrypto Source: https://github.com/cheatfate/nimcrypto/blob/master/README.md Use nimble to install the nimcrypto library. ```bash nimble install nimcrypto # installation ``` -------------------------------- ### EGETU64 Template Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/utils.html Template for getting a 64-bit unsigned integer from memory with an offset. ```nim template EGETU64(p, o): uint64 ``` -------------------------------- ### EGETU32 Template Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/utils.html Template for getting a 32-bit unsigned integer from memory with an offset. ```nim template EGETU32(p, o): uint32 ``` -------------------------------- ### GET_QWORD Template Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/utils.html Template for getting a 64-bit unsigned integer from a byte pointer at a specific index. ```nim template GET_QWORD(p: ptr byte; i: int): uint64 ``` -------------------------------- ### GETU8 Template Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/utils.html Template for getting a single byte from memory with an offset. ```nim template GETU8(p, o): byte ``` -------------------------------- ### Template Size Functions Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Functions to get the block and key sizes for various cipher modes. ```APIDOC ## Template Size Functions ### sizeBlockECB Gets the block size for ECB mode. - **Parameters**: - `ctx` (ECB[T]): The ECB mode context. - **Returns**: - `int`: The block size in bytes. ### sizeKeyECB Gets the key size for ECB mode. - **Parameters**: - `ctx` (ECB[T]): The ECB mode context. - **Returns**: - `int`: The key size in bytes. ### sizeBlockCBC Gets the block size for CBC mode. - **Parameters**: - `ctx` (CBC[T]): The CBC mode context. - **Returns**: - `int`: The block size in bytes. ### sizeKeyCBC Gets the key size for CBC mode. - **Parameters**: - `ctx` (CBC[T]): The CBC mode context. - **Returns**: - `int`: The key size in bytes. ### sizeBlockCTR Gets the block size for CTR mode. - **Parameters**: - `ctx` (CTR[T]): The CTR mode context. - **Returns**: - `int`: The block size in bytes. ### sizeKeyCTR Gets the key size for CTR mode. - **Parameters**: - `ctx` (CTR[T]): The CTR mode context. - **Returns**: - `int`: The key size in bytes. ``` -------------------------------- ### Get Key Size from Rijndael Context Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/rijndael.html Returns the key size in bytes for the given Rijndael context. ```APIDOC ## sizeKey ### Description Returns the key size in bytes for the given Rijndael context. ### Signature `template sizeKey(ctx: RijndaelContext): int` ### Parameters * `ctx` (RijndaelContext) - The Rijndael context. ### Returns (int) The key size in bytes. ``` -------------------------------- ### BLAKE2 Helper Templates Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/blake2.html Provides templates for extracting 32-bit and 64-bit unsigned integers from byte arrays, and for getting digest and block sizes. ```APIDOC ## BLAKE2 Helper Templates ### Description These templates provide utility functions for working with BLAKE2 hashes. ### Templates * `BLGETU64(p, o): uint64` - Extracts a 64-bit unsigned integer from a byte array `p` at offset `o`. * `BLGETU32(p, o): uint32` - Extracts a 32-bit unsigned integer from a byte array `p` at offset `o`. * `sizeDigest(ctx: Blake2Context): uint` - Returns the digest size in bytes for a given BLAKE2 context. * `sizeBlock(ctx: Blake2Context): uint` - Returns the block size in bytes for a given BLAKE2 context. * `sizeDigest(r: typedesc[blake2]): int` - Returns the digest size in bytes for a BLAKE2 type descriptor. * `sizeBlock(r: typedesc[blake2]): int` - Returns the block size in bytes for a BLAKE2 type descriptor. ``` -------------------------------- ### init (openArray[byte]) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/blowfish.html Initializes the Blowfish context with the provided key as an open array of bytes. ```APIDOC ## init(ctx: var BlowfishContext; key: openArray[byte]) ### Description Initializes the Blowfish context with the provided key as an open array of bytes. This procedure sets up the necessary internal state for encryption and decryption operations. ### Parameters * **ctx** (var BlowfishContext) - The Blowfish context to initialize. * **key** (openArray[byte]) - An open array of bytes representing the encryption key. ``` -------------------------------- ### Basic Hashing with keccak_256 Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/index.html Demonstrates the most basic usage of nimcrypto by hashing a string using the keccak_256 algorithm. Ensure the nimcrypto library is imported before use. ```nim import nimcrypto echo keccak_256.digest("Alice makes a hash") ``` -------------------------------- ### Blake2 Context Initialization (with key) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/blake2.html Initializes a Blake2 hashing context with an optional key. ```APIDOC ## proc init(ctx: var Blake2Context; key: ptr byte = nil; keylen: uint = 0'u) ### Description Initializes a Blake2 hashing context. An optional key and its length can be provided. ### Parameters - **ctx** (var Blake2Context) - The Blake2 hashing context to initialize. - **key** (ptr byte, optional) - A pointer to the key. Defaults to nil. - **keylen** (uint, optional) - The length of the key. Defaults to 0. ``` -------------------------------- ### Initialize Rijndael Context with Key Size and Key Pointer Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/rijndael.html Initializes the Rijndael context with a specified key size and a pointer to the key material. ```APIDOC ## initRijndaelContext ### Description Initializes the Rijndael context with a specified key size and a pointer to the key material. ### Signature `proc initRijndaelContext(ctx: var RijndaelContext; N: int; key: ptr byte)` ### Parameters * `ctx` (var RijndaelContext) - The Rijndael context to initialize. * `N` (int) - The key size in bits (e.g., 128, 192, 256). * `key` (ptr byte) - A pointer to the key material. ``` -------------------------------- ### Blake2 Context Initialization (with array key) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/blake2.html Initializes a Blake2 hashing context with a key provided as an open array. ```APIDOC ## proc init[T: bchar](ctx: var Blake2Context; key: openArray[T]) ### Description Initializes a Blake2 hashing context using a key provided as an open array. This is an inline procedure for efficiency. ### Parameters - **ctx** (var Blake2Context) - The Blake2 hashing context to initialize. - **key** (openArray[T]) - The key to use for initialization. ``` -------------------------------- ### init (ptr byte) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/blowfish.html Initializes the Blowfish context with the provided key and key length. This is an alias for initBlowfishContext. ```APIDOC ## init(ctx: var BlowfishContext; key: ptr byte; nkey: int) ### Description Initializes the Blowfish context with the provided key and key length. This procedure sets up the necessary internal state for encryption and decryption operations. This is an alias for initBlowfishContext. ### Parameters * **ctx** (var BlowfishContext) - The Blowfish context to initialize. * **key** (ptr byte) - A pointer to the byte array representing the encryption key. * **nkey** (int) - The length of the key in bytes. ``` -------------------------------- ### GET_DWORD Template Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/utils.html Template for getting a 32-bit unsigned integer from a byte pointer at a specific index. ```nim template GET_DWORD(p: ptr byte; i: int): uint32 ``` -------------------------------- ### GCM Get Tag (array) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Obtains the authentication tag from the GCM context and returns it as a 16-byte array. ```APIDOC proc getTag[T](ctx: var GCM[T]): array[16, byte] ``` -------------------------------- ### Initialize Rijndael Context with Key Pointer and Key Length Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/rijndael.html Initializes the Rijndael context with a pointer to the key material and its length. Defaults key length to 0 if not provided. ```APIDOC ## init ### Description Initializes the Rijndael context with a pointer to the key material and its length. ### Signature `proc init(ctx: var RijndaelContext; key: ptr byte; nkey: int = 0)` ### Parameters * `ctx` (var RijndaelContext) - The Rijndael context to initialize. * `key` (ptr byte) - A pointer to the key material. * `nkey` (int, optional) - The length of the key in bytes. Defaults to 0. ``` -------------------------------- ### Twofish Initialization Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/twofish.html Initializes the Twofish context with a provided key. Supports keys of varying lengths. ```APIDOC ## init(ctx: var TwofishContext; key: ptr byte; nkey: int = 0) ### Description Initializes the Twofish context with a byte array key. ### Parameters * **ctx** (var TwofishContext) - The Twofish context to initialize. * **key** (ptr byte) - A pointer to the key data. * **nkey** (int) - The length of the key in bytes. Defaults to 0, which might imply auto-detection or a default size. ### Method proc init(ctx: var TwofishContext; key: ptr byte; nkey: int = 0) ## init(ctx: var TwofishContext; key: openArray[byte]) ### Description Initializes the Twofish context with a key provided as an open array of bytes. ### Parameters * **ctx** (var TwofishContext) - The Twofish context to initialize. * **key** (openArray[byte]) - The key data as a byte array. ### Method proc init(ctx: var TwofishContext; key: openArray[byte]) ``` -------------------------------- ### Get Block Size from Rijndael Context Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/rijndael.html Returns the block size in bytes for the given Rijndael context. ```APIDOC ## sizeBlock ### Description Returns the block size in bytes for the given Rijndael context. ### Signature `template sizeBlock(ctx: RijndaelContext): int` ### Parameters * `ctx` (RijndaelContext) - The Rijndael context. ### Returns (int) The block size in bytes. ``` -------------------------------- ### Initialize Rijndael Context with Key Array Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/rijndael.html Initializes the Rijndael context using an open array of bytes as the key. ```APIDOC ## init ### Description Initializes the Rijndael context using an open array of bytes as the key. ### Signature `proc init(ctx: var RijndaelContext; key: openArray[byte])` ### Parameters * `ctx` (var RijndaelContext) - The Rijndael context to initialize. * `key` (openArray[byte]) - An open array of bytes representing the key. ``` -------------------------------- ### HMAC Blake2 Context Digest Size Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Template to get the size of the HMAC digest in octets (bytes) using BLAKE2b/BLAKE2s algorithms. ```nim template sizeDigest(h: HMAC[Blake2Context]): uint Size of HMAC digest in octets (bytes) using BLAKE2b/BLAKE2s algorithms. ``` -------------------------------- ### Basic keccak_256 Hashing in Nim Source: https://github.com/cheatfate/nimcrypto/blob/master/README.md Demonstrates the basic usage of the keccak_256 digest function from the nimcrypto library. Ensure the nimcrypto library is imported. ```nim import nimcrypto echo keccak_256.digest("Alice makes a hash") # outputs F8AE86DA35CF3D9F0816BAA6015A6AFFD20BA5D6A533FEA94D89D6164264326F ``` -------------------------------- ### HMAC Keccak Context Digest Size Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Template to get the size of the HMAC digest in octets (bytes) using KECCAK/SHA3/SHAKE algorithms. ```nim template sizeDigest(h: HMAC[KeccakContext]): uint Size of HMAC digest in octets (bytes) using KECCAK/SHA3/SHAKE algorithms. ``` -------------------------------- ### BLAKE2 Context Initialization Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/blake2.html Initializes a BLAKE2 context, optionally with a key. Supports both raw byte pointers and open arrays for the key. ```APIDOC ## initBlake2Context ### Description Initializes a BLAKE2 hashing context. An optional key can be provided. ### Signature `proc init(ctx: var Blake2Context; key: ptr byte = nil; keylen: uint = 0'u)` `proc init[T: bchar](ctx: var Blake2Context; key: openArray[T])` ### Parameters * `ctx` (var Blake2Context) - The BLAKE2 context to initialize. * `key` (ptr byte, optional) - A pointer to the key buffer. Defaults to nil. * `keylen` (uint, optional) - The length of the key in bytes. Defaults to 0. * `key` (openArray[T], optional) - An open array containing the key. ``` -------------------------------- ### HMAC RIPEMD Context Digest Size Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Template to get the size of the HMAC digest in octets (bytes) using RIPEMD algorithms. ```nim template sizeDigest(h: HMAC[RipemdContext]): uint Size of HMAC digest in octets (bytes) using RIPEMD algorithms. ``` -------------------------------- ### OFB Initialize (byte array) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the OFB mode context with a key and an initial vector (IV) provided as byte arrays. No additional padding is performed. ```APIDOC ## init OFB[T](ctx: var OFB[T]; key: openArray[byte]; iv: openArray[byte]) ### Description Initialize OFB[T] with encryption key key and initial vector (IV) iv. ### Parameters #### Path Parameters - **ctx** (OFB[T]) - The OFB mode context to initialize. - **key** (openArray[byte]) - The encryption key as a byte array. - **iv** (openArray[byte]) - The initial vector as a byte array. ### Notes - This procedure will not perform any additional padding for encryption key key and initial vector iv. - Length of key array must be at least ctx.sizeKey() octets (bytes). - Length of iv array must be at least ctx.sizeBlock() octets (bytes). - You can see examples of usage OFB mode here examples/ofb.nim. ``` -------------------------------- ### Create and Inspect a Hash Digest in Nim Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hash.html Demonstrates how to create a hash digest from a hexadecimal string and retrieve its bit length. Ensure the nimcrypto module is imported. ```nim import nimcrypto const SomeDigest = "7F83B1657FF1FC53B92DC18148A1D65DFC2D4B1FA3D677284ADDD200126D9069".toDigest echo $SomeDigest ## Get number of bits used by ``SomeDigest``. echo SomeDigest.bits ``` -------------------------------- ### HMAC SHA2 Context Digest Size Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Template to get the size of the HMAC digest in octets (bytes) using SHA2 algorithms. ```nim template sizeDigest(h: HMAC[Sha2Context]): uint Size of HMAC digest in octets (bytes) using SHA2 algorithms. ``` -------------------------------- ### Get Block Size for Rijndael Type Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/rijndael.html Returns the block size in bytes for a given Rijndael type (e.g., rijndael128, aes256). ```APIDOC ## sizeBlock ### Description Returns the block size in bytes for a given Rijndael type. ### Signature `template sizeBlock(r: typedesc[rijndael]): int` ### Parameters * `r` (typedesc[rijndael]) - The type descriptor for a Rijndael variant (e.g., `rijndael128`). ### Returns (int) The block size in bytes. ``` -------------------------------- ### CBC Initialization (chars) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the CBC context with a key and an initial vector (IV) using character arrays. ```APIDOC ## init CBC (chars) ### Description Initialize CBC[T] with encryption key key and initial vector (IV) iv. ### Parameters - `ctx` (CBC[T]) - The CBC context. - `key` (openArray[char]) - The encryption key. - `iv` (openArray[char]) - The initial vector. ### Notes - This procedure will not perform any additional padding for encryption key key and initial vector iv. - Length of key must be at least ctx.sizeKey() octets (bytes). - Length of iv must be at least ctx.sizeBlock() octets (bytes). ``` -------------------------------- ### Get Key Size for Rijndael Type Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/rijndael.html Returns the key size in bytes for a given Rijndael type (e.g., rijndael128, aes256). ```APIDOC ## sizeKey ### Description Returns the key size in bytes for a given Rijndael type. ### Signature `template sizeKey(r: typedesc[rijndael]): int` ### Parameters * `r` (typedesc[rijndael]) - The type descriptor for a Rijndael variant (e.g., `rijndael128`). ### Returns (int) The key size in bytes. ``` -------------------------------- ### OFB Initialize (char array) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the OFB mode context with a key and an initial vector (IV) provided as character arrays. No additional padding is performed. ```APIDOC ## init OFB[T](ctx: var OFB[T]; key: openArray[char]; iv: openArray[char]) ### Description Initialize OFB[T] with encryption key key and initial vector (IV) iv. ### Parameters #### Path Parameters - **ctx** (OFB[T]) - The OFB mode context to initialize. - **key** (openArray[char]) - The encryption key as a character array. - **iv** (openArray[char]) - The initial vector as a character array. ### Notes - This procedure will not perform any additional padding for encryption key key and initial vector iv. - Length of key array must be at least ctx.sizeKey() octets (bytes). - Length of iv array must be at least ctx.sizeBlock() octets (bytes). - You can see examples of usage OFB mode here examples/ofb.nim. ``` -------------------------------- ### CBC Initialization (bytes) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the CBC context with a key and an initial vector (IV) using byte arrays. ```APIDOC ## init CBC (bytes) ### Description Initialize CBC[T] with encryption key key and initial vector (IV) iv. ### Parameters - `ctx` (CBC[T]) - The CBC context. - `key` (openArray[byte]) - The encryption key. - `iv` (openArray[byte]) - The initial vector. ### Notes - This procedure will not perform any additional padding for encryption key key and initial vector iv. - Length of key must be at least ctx.sizeKey() octets (bytes). - Length of iv must be at least ctx.sizeBlock() octets (bytes). ``` -------------------------------- ### finishSha2Context (MDigest) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/sha2.html Finalizes the SHA2 hash computation and returns the digest as an MDigest type. This is a convenient way to get the final hash value. ```APIDOC ## finishSha2Context (MDigest) ### Description Finalizes the SHA2 hash computation and returns the digest as an MDigest type. This is a convenient way to get the final hash value. ### Signature `proc finish(ctx: var Sha2Context): MDigest[ctx.bits]` ### Parameters * `ctx` (var Sha2Context) - The SHA2 context. ### Returns * `MDigest[ctx.bits]` - The calculated hash digest. ``` -------------------------------- ### Classic HMAC Calculation with SHA256 Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Demonstrates initializing, updating, and finishing an HMAC context using SHA256. Supports key initialization with both string/array and pointer/length. ```nim import nimcrypto var stringToHmac = "Hello World!" var stringHmacKey = "AliceKey" let ptrToHmac = cast[ptr byte](addr stringToHmac[0]) let ptrHmacKey = cast[ptr byte](addr stringHmacKey[0]) let toHmacLen = uint(len(stringToHmac)) let hmacKeyLen = uint(len(stringHmacKey)) # Declare context objects var hctx1, hctx2: HMAC[sha256] # Initalize HMAC[SHA256] contexts with key `AliceKey`. hctx1.init(stringHmacKey) hctx2.init(ptrHmacKey, hmacKeyLen) # Update HMAC[SHA256] context using data `Hello World!` twice. hctx1.update(stringToHmac) hctx1.update(stringToHmac) # Update HMAC[SHA256] context using data `Hello World!` twice. hctx2.update(ptrToHmac, toHmacLen) hctx2.update(ptrToHmac, toHmacLen) # Print HMAC[SHA256] digest. echo $hctx1.finish() echo $hctx2.finish() # Do not forget to clear contexts. hctx1.clear() hctx2.clear() ``` -------------------------------- ### HMAC Blake2 Context Block Size Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Template to get the size of the processing block in octets (bytes) for HMAC operations using BLAKE2b/BLAKE2s algorithms. ```nim template sizeBlock(h: HMAC[Blake2Context]): uint Size of processing block in octets (bytes), while perform HMAC operation using BLAKE2b/BLAKE2s algorithms. ``` -------------------------------- ### initBlowfishContext Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/blowfish.html Initializes the Blowfish context with the provided key and key length. ```APIDOC ## initBlowfishContext(ctx: var BlowfishContext; key: ptr byte; nkey: int) ### Description Initializes the Blowfish context with the provided key and key length. This procedure sets up the necessary internal state for encryption and decryption operations. ### Parameters * **ctx** (var BlowfishContext) - The Blowfish context to initialize. * **key** (ptr byte) - A pointer to the byte array representing the encryption key. * **nkey** (int) - The length of the key in bytes. ``` -------------------------------- ### HMAC Keccak Context Block Size Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Template to get the size of the processing block in octets (bytes) for HMAC operations using KECCAK/SHA3/SHAKE algorithms. ```nim template sizeBlock(h: HMAC[KeccakContext]): uint Size of processing block in octets (bytes), while perform HMAC operation using KECCAK/SHA3/SHAKE algorithms. ``` -------------------------------- ### Classic HMAC Calculation Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Demonstrates the classic method of HMAC calculation using context objects. This involves initializing, updating, and finishing the HMAC context. ```APIDOC ## Classic HMAC Calculation ### Description This section illustrates the traditional approach to calculating HMAC digests using a context object. It involves initializing the HMAC context with a key, updating it with the data to be authenticated, and finally finishing the calculation to obtain the digest. ### Usage ```nim import nimcrypto var stringToHmac = "Hello World!" var stringHmacKey = "AliceKey" let ptrToHmac = cast[ptr byte](addr stringToHmac[0]) let ptrHmacKey = cast[ptr byte](addr stringHmacKey[0]) let toHmacLen = uint(len(stringToHmac)) let hmacKeyLen = uint(len(stringHmacKey)) # Declare context objects var hctx1, hctx2: HMAC[sha256] # Initialize HMAC[SHA256] contexts with key `AliceKey`. hctx1.init(stringHmacKey) hctx2.init(ptrHmacKey, hmacKeyLen) # Update HMAC[SHA256] context using data `Hello World!` twice. hctx1.update(stringToHmac) hctx1.update(stringToHmac) # Update HMAC[SHA256] context using data `Hello World!` twice. hctx2.update(ptrToHmac, toHmacLen) hctx2.update(ptrToHmac, toHmacLen) # Print HMAC[SHA256] digest. echo $hctx1.finish() echo $hctx2.finish() # Do not forget to clear contexts. hctx1.clear() hctx2.clear() ``` ### Key Concepts - **HMAC Context**: An object (`HMAC[HashType]`) that holds the state of the HMAC calculation. - **`init`**: Initializes the HMAC context with a secret key. - **`update`**: Processes a chunk of data to be included in the HMAC calculation. - **`finish`**: Completes the HMAC calculation and returns the digest. - **`clear`**: Resets the HMAC context, releasing any sensitive information. ``` -------------------------------- ### HMAC RIPEMD Context Block Size Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Template to get the size of the processing block in octets (bytes) for HMAC operations using RIPEMD algorithms. ```nim template sizeBlock(h: HMAC[RipemdContext]): uint Size of processing block in octets (bytes), while perform HMAC operation using RIPEMD algorithms. ``` -------------------------------- ### OFB Initialize (ptr byte) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the OFB mode context with a key and an initial vector (IV) provided as pointers to bytes. ```APIDOC ## init OFB[T](ctx: var OFB[T]; key: ptr byte; iv: ptr byte) ### Description Initialize OFB[T] with encryption key key and initial vector (IV) iv. ### Parameters #### Path Parameters - **ctx** (OFB[T]) - The OFB mode context to initialize. - **key** (ptr byte) - A pointer to the encryption key. - **iv** (ptr byte) - A pointer to the initial vector. ### Notes - Size of encryption key pointed by key must be at least ctx.sizeKey octets (bytes). - Size of initial vector iv must be at least ctx.sizeBlock octets (bytes). ``` -------------------------------- ### HMAC SHA2 Context Block Size Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Template to get the size of the processing block in octets (bytes) for HMAC operations using SHA2 algorithms. ```nim template sizeBlock(h: HMAC[Sha2Context]): uint Size of processing block in octets (bytes), while perform HMAC operation using SHA2 algorithms. ``` -------------------------------- ### HMAC Calculation with Slices Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Illustrates how to perform HMAC calculations on a specific slice of data using the `hmac` proc with `ostart` and `ofinish` parameters. ```APIDOC ## HMAC Calculation with Slices ### Description This example demonstrates how to compute an HMAC digest for a specific portion (slice) of a larger data buffer. The `hmac` procedure provides `ostart` and `ofinish` parameters to define the inclusive range of the data to be processed, offering finer control over the authentication process. ### Usage ```nim import nimcrypto # Assume 'myKey' is your secret key and 'largeDataBuffer' is the data # For demonstration, we use strings: var myKey = "SecretKey" var largeDataBuffer = "This is a long string that contains the data we want to authenticate." # Calculate HMAC for the slice from index 5 to 20 (inclusive) # The data slice will be "is a long strin" let digest = sha256.hmac(myKey, largeDataBuffer, ostart = 5, ofinish = 20) echo $digest ``` ### Parameters - **`ostart`** (int): The starting index (inclusive) of the data slice for HMAC calculation. Defaults to 0. - **`ofinish`** (int): The ending index (inclusive) of the data slice for HMAC calculation. Defaults to -1 (meaning the end of the data). ### Note Ensure that `ostart` and `ofinish` are valid indices within the bounds of the `data` array. If `ofinish` is -1, the slice extends to the end of the data. ``` -------------------------------- ### Initialize CFB Mode (ptr byte) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the CFB mode context with a given key and initialization vector (IV) using raw byte pointers. Ensure the key and IV meet the minimum size requirements. ```APIDOC ## init[T](ctx: var CFB[T]; key: ptr byte; iv: ptr byte) ### Description Initializes the CFB[T] context with the provided encryption key and initialization vector (IV). ### Parameters * `ctx` (var CFB[T]): The CFB context to initialize. * `key` (ptr byte): A pointer to the encryption key. The size must be at least ctx.sizeKey octets. * `iv` (ptr byte): A pointer to the initialization vector. The size must be at least ctx.sizeBlock octets. ### Notes * The size of the encryption key pointed to by `key` must be at least `ctx.sizeKey` octets (bytes). * The size of the initial vector `iv` must be at least `ctx.sizeBlock` octets (bytes). * Refer to `examples/cfb.nim` for usage examples. ``` -------------------------------- ### GCM Get Tag (var) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Obtains the authentication tag from the GCM context and stores it in the provided tag byte array. The maximum tag size is 128 bits (16 bytes). ```APIDOC proc getTag[T](ctx: var GCM[T]; tag: var openArray[byte]) ``` -------------------------------- ### CBC Initialization (ptr byte) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the CBC context with a key and an initial vector (IV) using pointers. ```APIDOC ## init CBC (ptr byte) ### Description Initialize CBC[T] with encryption key key and initial vector (IV) iv. ### Parameters - `ctx` (CBC[T]) - The CBC context. - `key` (ptr byte) - Pointer to the encryption key. - `iv` (ptr byte) - Pointer to the initial vector. ### Notes - Size of encryption key pointed by key must be at least ctx.sizeKey octets (bytes). - Size of initial vector iv must be at least ctx.sizeBlock octets (bytes). ``` -------------------------------- ### Calculate Digest from OpenArray Slice Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hash.html Calculates a digest from an open array (like a string or sequence) using a specified hash type. Allows specifying start and end indices to hash a sub-section of the data. ```nim import nimcrypto var stringToHash = "Hello World!" ## Calculate digest of whole string `Hello World!`. echo sha256.digest(stringToHash) ## Calcualte digest of `Hello`. echo sha256.digest(stringToHash, ofinish = 4) ## Calculate digest of `World!`. echo sha256.digest(stringToHash, ostart = 6) ## Calculate digest of constant `Hello`. echo sha256.digest("Hello") ## Calculate digest of constant `World!`. echo sha256.digest("World!") ``` -------------------------------- ### KeccakContext Procedures Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/keccak.html Procedures for initializing, updating, and finalizing Keccak hashing operations. ```APIDOC ## Procedures for KeccakContext ### `init` - **Description**: Initializes a KeccakContext. - **Signature**: `proc init(ctx: var KeccakContext)` ### `clear` - **Description**: Clears a KeccakContext. - **Signature**: `proc clear(ctx: var KeccakContext){.inline.}` ### `update` (byte pointer) - **Description**: Updates the KeccakContext with data from a byte pointer. - **Signature**: `proc update(ctx: var KeccakContext; data: ptr byte; ulen: uint)` ### `update` (openArray) - **Description**: Updates the KeccakContext with data from an openArray. - **Signature**: `proc update[T: bchar](ctx: var KeccakContext; data: openArray[T])` ### `xof` - **Description**: Initiates an extendable output function (XOF) operation. - **Signature**: `proc xof(ctx: var KeccakContext)` ### `output` - **Description**: Retrieves the current output from the KeccakContext. - **Signature**: `proc output(ctx: var KeccakContext; data: ptr byte; ulen: uint): uint` ### `finish` (byte pointer) - **Description**: Finalizes the KeccakContext and writes the output to a byte pointer. - **Signature**: `proc finish(ctx: var KeccakContext; data: ptr byte; ulen: uint): uint` ### `finish` (MDigest) - **Description**: Finalizes the KeccakContext and returns the hash as an MDigest. - **Signature**: `proc finish(ctx: var KeccakContext): MDigest[ctx.bits]` ### `finish` (openArray) - **Description**: Finalizes the KeccakContext and writes the output to an openArray. - **Signature**: `proc finish[T: bchar](ctx: var KeccakContext; data: var openArray[T])` ``` -------------------------------- ### HMAC Initialization Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Initializes the HMAC context with a secret key. Supports byte pointers and open arrays. ```APIDOC ## initHMAC ### Description Initializes the HMAC context with a secret key. ### Signatures - `init[T](hmctx: var HMAC[T]; key: ptr byte; ulen: uint)` - `init[T](hmctx: var HMAC[T]; key: openArray[byte])` - `init[T](hmctx: var HMAC[T]; key: openArray[char])` ``` -------------------------------- ### Initialize CFB Mode (openArray byte) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the CFB mode context with a given key and initialization vector (IV) using byte arrays. No additional padding is performed on the key or IV. ```APIDOC ## init[T](ctx: var CFB[T]; key: openArray[byte]; iv: openArray[byte]) ### Description Initializes the CFB[T] context with the provided encryption key and initialization vector (IV) using byte arrays. ### Parameters * `ctx` (var CFB[T]): The CFB context to initialize. * `key` (openArray[byte]): The encryption key as a byte array. Its length must be at least ctx.sizeKey() octets. * `iv` (openArray[byte]): The initialization vector as a byte array. Its length must be at least ctx.sizeBlock() octets. ### Notes * This procedure does not perform any additional padding for the encryption key or initialization vector. * Refer to `examples/cfb.nim` for usage examples. ``` -------------------------------- ### CTR Initialization (openArray byte) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the CTR context with a given key and initialization vector (IV) using byte arrays. ```APIDOC ## proc init[T](ctx: var CTR[T]; key: openArray[byte]; iv: openArray[byte]) ### Description Initializes the CTR context with the provided encryption key and initial vector (IV) as byte arrays. ### Parameters - **ctx** (var CTR[T]) - The CTR context to initialize. - **key** (openArray[byte]) - The encryption key as a byte array. Its length must be at least `ctx.sizeKey()` bytes. - **iv** (openArray[byte]) - The initialization vector (IV) as a byte array. Its length must be at least `ctx.sizeBlock()` bytes. ### Notes - This procedure does not perform any additional padding for the key or IV. - Refer to `examples/ctr.nim` for usage examples. ``` -------------------------------- ### initSha2Context Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/sha2.html Initializes a SHA2 context structure. This prepares the context for hashing operations. ```APIDOC ## initSha2Context ### Description Initializes a SHA2 context structure. This prepares the context for hashing operations. ### Signature `proc init(ctx: var Sha2Context)` ### Parameters * `ctx` (var Sha2Context) - The SHA2 context to initialize. ``` -------------------------------- ### EPUTU64 Template Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/utils.html Template for putting a 64-bit unsigned integer into memory with an offset. ```nim template EPUTU64(p, o, v) ``` -------------------------------- ### CTR Initialization (openArray char) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the CTR context with a given key and initialization vector (IV) using character arrays. ```APIDOC ## proc init[T](ctx: var CTR[T]; key: openArray[char]; iv: openArray[char]) ### Description Initializes the CTR context with the provided encryption key and initial vector (IV) as character arrays. ### Parameters - **ctx** (var CTR[T]) - The CTR context to initialize. - **key** (openArray[char]) - The encryption key as a character array. Its length must be at least `ctx.sizeKey()` bytes. - **iv** (openArray[char]) - The initialization vector (IV) as a character array. Its length must be at least `ctx.sizeBlock()` bytes. ### Notes - This procedure does not perform any additional padding for the key or IV. - Refer to `examples/ctr.nim` for usage examples. ``` -------------------------------- ### RIPEMD Context Initialization Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/ripemd.html Initializes a RIPEMD context structure. This should be called before any other operations on the context. ```APIDOC ## initRipemdContext ### Description Initializes a RIPEMD context structure. ### Signature `proc init(ctx: var RipemdContext)` ``` -------------------------------- ### EPUTU32 Template Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/utils.html Template for putting a 32-bit unsigned integer into memory with an offset. ```nim template EPUTU32(p, o, v) ``` -------------------------------- ### Initialize CFB Mode (openArray char) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the CFB mode context with a given key and initialization vector (IV) using character arrays. No additional padding is performed on the key or IV. ```APIDOC ## init[T](ctx: var CFB[T]; key: openArray[char]; iv: openArray[char]) ### Description Initializes the CFB[T] context with the provided encryption key and initialization vector (IV) using character arrays. ### Parameters * `ctx` (var CFB[T]): The CFB context to initialize. * `key` (openArray[char]): The encryption key as a character array. Its length must be at least ctx.sizeKey() octets. * `iv` (openArray[char]): The initialization vector as a character array. Its length must be at least ctx.sizeBlock() octets. ### Notes * This procedure does not perform any additional padding for the encryption key or initialization vector. * Refer to `examples/cfb.nim` for usage examples. ``` -------------------------------- ### HMAC Calculation with Various Hash Algorithms Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Demonstrates performing HMAC calculations using different hash algorithms (SHA256, SHA512, KECCAK256, RIPEMD160) with byte pointers and lengths. ```nim import nimcrypto var stringToHmac = "Hello World!" var stringHmacKey = "AliceKey" let data = cast[ptr byte](addr stringToHmac[0]) let datalen = uint(len(stringToHmac)) let key = cast[ptr byte](addr stringHmacKey[0]) let keylen = uint(len(stringHmacKey)) # Print HMAC[SHA256](key = "AliceKey", data = "Hello World!") echo sha256.hmac(key, keylen, data, datalen) # Print HMAC[SHA512](key = "AliceKey", data = "Hello World!") echo sha512.hmac(key, keylen, data, datalen) # Print HMAC[KECCAK256](key = "AliceKey", data = "Hello World!") echo keccak256.hmac(key, keylen, data, datalen) # Print HMAC[RIPEMD160](key = "AliceKey", data = "Hello World!") echo ripemd160.hmac(key, keylen, data, datalen) ``` -------------------------------- ### CTR Initialization (ptr byte) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the CTR context with a given key and initialization vector (IV) using pointer types. ```APIDOC ## proc init[T](ctx: var CTR[T]; key: ptr byte; iv: ptr byte) ### Description Initializes the CTR context with the provided encryption key and initial vector (IV). ### Parameters - **ctx** (var CTR[T]) - The CTR context to initialize. - **key** (ptr byte) - A pointer to the encryption key. The size of the key must be at least `ctx.sizeKey` bytes. - **iv** (ptr byte) - A pointer to the initialization vector. The size of the IV must be at least `ctx.sizeBlock` bytes. ### Notes - Ensure the key and IV are of sufficient size as specified by `ctx.sizeKey` and `ctx.sizeBlock` respectively. ``` -------------------------------- ### HMAC Calculation with Slicing Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Shows how to compute HMAC on a specific slice of data using various hash algorithms and array inputs. ```nim import nimcrypto # Print HMAC[SHA256](key = "AliceKey", data = "Hello World!") echo sha256.hmac(stringHmacKey, stringToHmac) # Print HMAC[SHA512](key = "AliceKey", data = "Hello World!") echo sha512.hmac(stringHmacKey, stringToHmac) # Print HMAC[KECCAK256](key = "AliceKey", data = "Hello World!") echo keccak256.hmac(stringHmacKey, stringToHmac) # Print HMAC[RIPEMD160](key = "AliceKey", data = "Hello World!") echo ripemd160.hmac(stringHmacKey, stringToHmac) ``` -------------------------------- ### Keccak Context Initialization and Clearing Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/keccak.html Initializes or clears the state of a Keccak context for hashing operations. ```APIDOC ## initKeccakContext ### Description Initializes the Keccak context structure. ### Signature `init(ctx: var KeccakContext)` ## clearKeccakContext ### Description Clears the Keccak context structure, resetting its state. ### Signature `clear(ctx: var KeccakContext)` ``` -------------------------------- ### PUTU64 Template Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/utils.html Template for putting a 64-bit unsigned integer into memory. ```nim template PUTU64(p, o, v) ``` -------------------------------- ### SET_QWORD Template Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/utils.html Template for setting a 64-bit unsigned integer into a byte pointer at a specific index. ```nim template SET_QWORD(p: ptr byte; i: int; v: uint64) ``` -------------------------------- ### encrypt (ptr byte) Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/blowfish.html Encrypts input bytes using the Blowfish algorithm with the provided context. The output is written to the output buffer. ```APIDOC ## encrypt(ctx: var BlowfishContext; inbytes: ptr byte; outbytes: ptr byte) ### Description Encrypts input bytes using the Blowfish algorithm with the provided context. The output is written to the output buffer. ### Parameters * **ctx** (var BlowfishContext) - The Blowfish context to use for encryption. * **inbytes** (ptr byte) - A pointer to the input byte buffer to be encrypted. * **outbytes** (ptr byte) - A pointer to the output byte buffer where the encrypted data will be stored. ``` -------------------------------- ### One-line HMAC Calculation Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/hmac.html Demonstrates a concise, one-line method for HMAC calculation using the `hmac` proc. ```APIDOC ## One-line HMAC Calculation ### Description This section shows a more direct way to compute HMAC digests using a single procedure call. This method is convenient for simple cases where the entire data and key are readily available. ### Usage ```nim import nimcrypto # Print HMAC[SHA256] digest of `Hello World!Hello World!` using key `AliceKey`. echo $sha256.hmac(stringHmacKey, stringToHmac & stringToHmac) # Output to stdout must be 3 equal digests: # 18AF7C8586141A47EAAD416C2B356431D001FAFF3B8C98C80AA108DC971B230D # 18AF7C8586141A47EAAD416C2B356431D001FAFF3B8C98C80AA108DC971B230D # 18AF7C8586141A47EAAD416C2B356431D001FAFF3B8C98C80AA108DC971B230D ``` ### Key Concepts - **`hmac` proc**: A convenience function that performs the entire HMAC calculation (initialization, update, and finish) in one step. - **Overloads**: The `hmac` proc is available for various hash types (e.g., `sha256`, `sha512`) and accepts keys and data as strings, byte arrays, or pointers. ``` -------------------------------- ### ECB Mode Initialization Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/bcmode.html Initializes the ECB (Electronic Code Book) mode context with a given encryption key. The key size must be at least the size returned by `sizeKey()` for the specific cipher. ```APIDOC ## proc init[T](ctx: var ECB[T]; key: ptr byte) ### Description Initializes ECB[T] with encryption key `key`. Note! Size of data pointed by `key` must be at least `ctx.sizeKey` octets (bytes). ## proc init[T](ctx: var ECB[T]; key: openArray[byte]) ### Description Initializes ECB[T] with encryption key `key`. This procedure will not perform any additional padding for encryption key `key`. Length of `key` must be at least `ECB[T].sizeKey()` octets (bytes). You can see examples of usage ECB mode here examples/ecb.nim. ## proc init[T](ctx: var ECB[T]; key: openArray[char]) ### Description Initializes ECB[T] with encryption key `key`. This procedure will not perform any additional padding for encryption key `key`. Length of `key` must be at least `ECB[T].sizeKey()` octets (bytes). You can see examples of usage ECB mode here examples/ecb.nim. ``` -------------------------------- ### Encrypt Open Array using Rijndael Context Source: https://github.com/cheatfate/nimcrypto/blob/master/docs/nimcrypto/rijndael.html Encrypts an input open array of bytes to an output open array using the initialized Rijndael context. ```APIDOC ## encrypt ### Description Encrypts an input open array of bytes to an output open array using the initialized Rijndael context. ### Signature `proc encrypt(ctx: var RijndaelContext; input: openArray[byte]; output: var openArray[byte])` ### Parameters * `ctx` (var RijndaelContext) - The initialized Rijndael context. * `input` (openArray[byte]) - The input data as an open array of bytes. * `output` (var openArray[byte]) - The output buffer (will be modified) for encrypted data. ```