# Hashing and key reference

Hasher

The `Hasher` class is a base class that is used to build a message hash. The hash result is stored in an array of bytes and can be retrieved byte by byte or as a formatted printable string. This class does not have a constructor.

hasher-p
Nameable
reset
none
none

The `reset` method reset the hasher object with its associated internal states.

hash-p
Boolean
String

The `hash-p` predicate returns true if the string argument is potentially a hash value. It is not possible, with our current technology, to reverse a hash value to one or several representations, nor it is possible to assert that such value exists.

get-byte
Byte
Integer

The `get-byte` method returns the hash byte value by index. The argument is the byte index which must be in the range of the hash result length.

format
String
none

The `format` method return a string representation of the hash value.

compute
String
Literal|Buffer|InputStream

The `compute` method computes the hash value from a string, a buffer or an input stream. The method returns a string representation of the result hash value. When the argument is a buffer object or an input stream, the characters are consumed from the object.

derive
String
String

The `derive` method computes the hash value from an octet string which is converted before the hash computation. The method returns a string representation of the result hash value.

get-hash-length
Integer
none

The `get-hash-length` method returns the hasher length in bytes.

get-result-length
Integer
none

The `get-result-length` method returns the hasher result length in bytes. The result length is less or equal to the hasher length and is set at construction.

Md2

The `Md2` class is a hashing class that implements the MD-2 algorithm.

md2-p
Hasher
Md2
none

The `Md2` constructor creates a default hashing object that implements the MD-2 algorithm.

Md2
Integer

The `Md2` constructor creates a MD-2 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length.

Md4

The `Md4` class is a hashing class that implements the MD-4 algorithm.

md4-p
Hasher
Md4
none

The `Md4` constructor creates a default hashing object that implements the MD-4 algorithm.

Md4
Integer

The `Md4` constructor creates a MD-4 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length.

Md5

The `Md5` class is a hashing class that implements the MD-5 algorithm.

md5-p
Hasher
Md5
none

The `Md5` constructor creates a default hashing object that implements the MD-5 algorithm.

Md5
Integer

The `Md5` constructor creates a MD-5 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length.

Sha1

The `Sha1` class is a hashing class that implements the SHA-1 algorithm.

sha1-p
Hasher
Sha1
none

The `Sha1` constructor creates a default hashing object that implements the SHA-1 algorithm.

Sha1
Integer

The `Sha1` constructor creates a SHA-1 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length.

Sha256

The `Sha256` class is a hashing class that implements the SHA-256 algorithm.

sha256-p
Hasher
Sha256
none

The `Sha256` constructor creates a default hashing object that implements the SHA-256 algorithm.

Sha256
Integer

The `Sha256` constructor creates a SHA-256 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length.

Sha384

The `Sha384` class is a hashing class that implements the SHA-384 algorithm.

sha384-p
Hasher
Sha384
none

The `Sha384` constructor creates a default hashing object that implements the SHA-384 algorithm.

Sha384
Integer

The `Sha384` constructor creates a SHA-384 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length.

Sha512

The `Sha512` class is a hashing class that implements the SHA-512 algorithm.

sha512-p
Hasher
Sha512
none

The `Sha512` constructor creates a default hashing object that implements the SHA-512 algorithm.

Sha512
Integer

The `Sha512` constructor creates a SHA-512 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length.

Key

The `Key` class is an original class used to store a particular key or to generate one. A key is designed to operate with a variety of cipher that can be either symmetric or asymmetric. In the symmetric case, the key is generally an array of bytes. Asymmetric key are generally stored in the form of number list that can be computed or loaded by value. By default, a random 128 bit symmetric key is created.

key-p
Object
Key
none

The `Key` constructor creates a default cipher key. The key is generated with random bytes and is 128 bits long.

Key
String

The `Key` constructor creates a symmetric key from an octet string. The octet string argument determines the size of the key. The octet string argument is compatible with the string obtained from the `format` method.

Key
Item

The `Key` constructor creates a key by type. If the key type is `KSYM`, a symmetric 128 bytes random key is generated. If the key type is `KRSA`, a 1024 bits RSA random key is generated.

Key
Item Integer|String|Vector

The `Key` constructor creates a key by type. The first argument is the key type to generate. The second argument is either the key size, the key octet string or the key byte values. In the first form, an integer argument specifies the key size in bytes or bits depending on the key nature. In the second form, a string is used as octet string to represent the key. In the third form, a vector of byte values can be used to load the key.

KSYM

The `KSYM` constant indicates that the key is a symmetric key.

KRSA

The `KRSA` constant indicates that the key is a asymmetric RSA key.

KMAC

The `KMAC` constant indicates that the key is a message authentication (MAC) key.

RSA-MODULUS

The `RSA-MODULUS` constant corresponds to the RSA modulus value.

RSA-PUBLIC-EXPONENT

The `RSA-PUBLIC-EXPONENT` constant corresponds to the RSA public exponent value which is generally 65537.

RSA-SECRET-EXPONENT

The `RSA-SECRET-EXPONENT` constant corresponds to the RSA secret exponent value.

get-byte
Byte
Integer

The `get-byte` method returns a key byte value by index. The index must be in the key range or an exception is raised. This method is primarily used with symmetric key.

get-type
Item
none

The `get-type` method returns the key type in the form of an item object.

get-bits
Integer
none

The `get-bits` method returns the key size in bits.

get-size
Integer
none

The `get-size` method returns the key size in bytes.

format
String
none|Item

The `format` method returns a string representation of the key. In the first form, without argument, the key is returned as an octet string if possible. In the second form, the key value is returned as an octet string based on the key element to access.

get-relatif-key
Relatif
Item

The `get-relatif-key` method returns a relatif representation of a key element. This method is well suited for asymmetric key. The key value is returned as a relatif based on the key element to access.

Kdf

The `Kdf` class is an abstract class used to model key derivation function. The class provides only a byte buffer which can be accessed by index. In the key derivation functions land, there are numerous standards, such like PKCS 2.1, IEEE P1363-2000, ISO/IEC 18033-2. All of these standards have sometimes conflicting definitions.

kdf-p
Nameable
reset
none
none

The `reset` method reset the internal state of the kdf object.

get-size
Integer
none

The `get-size` method returns the kdf size in bytes.

get-byte
Byte
Integer

The `get-byte` method returns a kdf byte value by index. The index must be in the key range or an exception is raised.

format
String
none

The `format` method returns a string representation of the derived key.

derive
String
String

The `derive` method returns a string representation of a derived key computed from the octet string argument.

compute
String
String

The `compute` method returns a string representation of a derived key computed from the string argument.

Hkdf

The `Hkdf` class is an abstract class used to model key derivation function based on hash function. The class maintains a hasher object that is used to derive the key from an octet string.

hashed-kdf-p
Kdf
get-hasher
none
none

The `get-hasher` method returns the hasher object associated with the key derivation function object. object.

Kdf1

The `Kdf1` class is a hashed key derivation function class that implements the KDF1 specification as defined by ISO/IEC 18033-2. The class is strictly equivalent to the mask generation function (MGF1) defined in PKCS 2.1. On the other hand, this implementation does not conform to the KDF1 specification of IEEE 1363-2000 which is somehow rather bizarre. The class operates in theory with any type of hasher object as long as the octet string is not too long.

kdf1-p
Hkdf
Kdf1
Hasher Integer

The `Kdf1` constructor creates a KDF1 key derivation function object. The first argument is the hasher object to bind and the second argument is the kdf size.

Kdf2

The `Kdf2` class is a hashed key derivation function class that implements the KDF2 specification as defined by ISO/IEC 18033-2. The class is strictly equivalent to the key function derivation (KDF1) except that the internal counter runs from 1 to k instead of 0 to k-1. The class operates in theory with any type of hasher object as long as the octet string is not too long.

kdf2-p
Hkdf
Kdf2
Hasher Integer

The `Kdf2` constructor creates a KDF2 key derivation function object. The first argument is the hasher object to bind and the second argument is the kdf size.