SIMPLE_HASH

API Reference

Class Overview

SIMPLE_HASH provides cryptographic hashing functions for Eiffel applications. All hash outputs are available as both raw bytes and hexadecimal strings.

Creation

create hasher.make

Initialize the hasher.

Contracts

ensure: buffer_ready: working_buffer.count = 64

SHA-256 Features

SHA-256 produces a 256-bit (32-byte) hash, output as 64 hexadecimal characters.

sha256 (a_input: STRING): STRING

digest := hasher.sha256 ("Hello, World!")

Compute SHA-256 hash and return as lowercase hex string (64 characters).

Contracts

require: input_not_void: a_input /= Void
ensure: result_not_void: Result /= Void
ensure: correct_length: Result.count = 64
ensure: lowercase_hex: across Result as c all c.item.is_lower or c.item.is_digit end

sha256_bytes (a_input: STRING): ARRAY [NATURAL_8]

bytes := hasher.sha256_bytes ("Hello, World!")

Compute SHA-256 hash and return as 32-byte array.

Contracts

require: input_not_void: a_input /= Void
ensure: result_not_void: Result /= Void
ensure: correct_length: Result.count = 32

HMAC-SHA256 Features

HMAC-SHA256 creates a keyed hash for message authentication. Essential for JWT HS256 signatures.

hmac_sha256 (a_key, a_message: STRING): STRING

signature := hasher.hmac_sha256 ("secret", "message")

Compute HMAC-SHA256 and return as lowercase hex string (64 characters).

Contracts

require: key_not_void: a_key /= Void
require: message_not_void: a_message /= Void
ensure: result_not_void: Result /= Void
ensure: correct_length: Result.count = 64

hmac_sha256_bytes (a_key, a_message: STRING): ARRAY [NATURAL_8]

bytes := hasher.hmac_sha256_bytes ("secret", "message")

Compute HMAC-SHA256 and return as 32-byte array.

Contracts

require: key_not_void: a_key /= Void
require: message_not_void: a_message /= Void
ensure: result_not_void: Result /= Void
ensure: correct_length: Result.count = 32

MD5 Features (Legacy)

Warning: MD5 is cryptographically broken. Use only for checksums, never for security.

md5 (a_input: STRING): STRING

checksum := hasher.md5 ("data")

Compute MD5 hash and return as lowercase hex string (32 characters).

Contracts

require: input_not_void: a_input /= Void
ensure: result_not_void: Result /= Void
ensure: correct_length: Result.count = 32

md5_bytes (a_input: STRING): ARRAY [NATURAL_8]

bytes := hasher.md5_bytes ("data")

Compute MD5 hash and return as 16-byte array.

Contracts

require: input_not_void: a_input /= Void
ensure: result_not_void: Result /= Void
ensure: correct_length: Result.count = 16

Utility Features

bytes_to_hex (a_bytes: ARRAY [NATURAL_8]): STRING

hex := hasher.bytes_to_hex (byte_array)

Convert byte array to lowercase hexadecimal string.

Contracts

require: bytes_not_void: a_bytes /= Void
ensure: result_not_void: Result /= Void
ensure: correct_length: Result.count = a_bytes.count * 2

hex_to_bytes (a_hex: STRING): ARRAY [NATURAL_8]

bytes := hasher.hex_to_bytes ("deadbeef")

Convert hexadecimal string to byte array.

Contracts

require: hex_not_void: a_hex /= Void
require: even_length: a_hex.count \\ 2 = 0
require: valid_hex: across a_hex as c all Hex_chars.has (c.item.as_lower) end
ensure: result_not_void: Result /= Void
ensure: correct_length: Result.count = a_hex.count // 2

Constants

Constant Value Description
Hex_chars 0123456789abcdef Hexadecimal characters for output formatting

Class Invariants

invariant: buffer_exists: working_buffer /= Void