class

RNS::Identity

Inherits Reference < Object

Manages cryptographic identities using X25519 encryption and Ed25519 signing key pairs. Provides encryption, decryption, signing, validation, and destination tracking for the Reticulum network.

Constants

AES128_BLOCKSIZE = 16
CURVE = "Curve25519"
DERIVED_KEY_LENGTH = 512 // 8
DERIVED_KEY_LENGTH_LEGACY = 256 // 8
HASHLENGTH = 256
KEYSIZE = 256 * 2
NAME_HASH_LENGTH = 80
RATCHET_EXPIRY = ((60 * 60) * 24) * 30
RATCHETSIZE = 256
SIGLENGTH = KEYSIZE
TOKEN_OVERHEAD = Cryptography::Token::TOKEN_OVERHEAD
TRUNCATED_HASHLENGTH = 128

Constructors

new(create_keys : Bool = true)

Creates a new Identity instance. When create_keys is true, generates fresh key pairs immediately.

Source

Class methods

clean_ratchets(storage_path : String | Nil = nil)
Source
current_ratchet_id(destination_hash : Bytes) : Bytes | Nil
Source
exit_handler(storage_path : String | Nil = nil)
Source
from_bytes(prv_bytes : Bytes) : Identity | Nil

Creates an Identity from raw private key bytes. Returns nil if the key is invalid.

Source
from_file(path : String) : Identity | Nil

Loads an Identity from a file containing private key bytes. Returns nil on failure.

Source
full_hash(data : Bytes) : Bytes

Computes a full SHA-256 hash of the given data.

Source
generate_ratchet
Source
get_random_hash

Generates a random truncated hash from secure random bytes.

Source
get_ratchet(destination_hash : Bytes) : Bytes | Nil
Source
get_ratchet_id(ratchet_pub_bytes : Bytes) : Bytes
Source
known_destinations
Source
known_ratchets
Source
load_known_destinations(storage_path : String | Nil = nil)
Source
persist_data(storage_path : String | Nil = nil)
Source
ratchet_public_bytes(ratchet : Bytes) : Bytes
Source
recall(target_hash : Bytes, from_identity_hash : Bool = false) : Identity | Nil

Recalls a known Identity by destination hash or identity hash. Returns nil if not found.

Source
recall_app_data(destination_hash : Bytes) : Bytes | Nil

Retrieves the stored app data for a known destination, or nil if not found.

Source
remember(packet_hash : Bytes, destination_hash : Bytes, public_key : Bytes, app_data : Bytes | Nil = nil)

Stores a destination's public key and app data in the known destinations table.

Source
remember_ratchet(destination_hash : Bytes, ratchet : Bytes)
Source
save_known_destinations(storage_path : String | Nil = nil)
Source
truncated_hash(data : Bytes) : Bytes

Computes a truncated hash (first 128 bits of the full SHA-256 hash).

Source
try_clean_ratchets

Clean expired ratchets from @@known_ratchets

Source
validate_announce(packet : Packet, only_validate_signature : Bool = false) : Bool

Validates an announce packet. Returns true if valid, false otherwise. When only_validate_signature is true, skips destination hash and known_destinations checks (used for quick signature validation).

Source

Instance methods

app_data
Source
app_data=(app_data : Bytes | Nil)
Source
create_keys

Generates new X25519 encryption and Ed25519 signing key pairs, then updates identity hashes.

Source
decrypt(ciphertext_token : Bytes, ratchets : Array(Bytes) | Nil = nil, enforce_ratchets : Bool = false) : Bytes | Nil

Decrypts ciphertext, optionally trying ratchet keys first. Returns nil on failure.

Source
encrypt(plaintext : Bytes, ratchet : Bytes | Nil = nil) : Bytes

Encrypts plaintext using ECDH key exchange with an ephemeral key and Token-based symmetric encryption.

Source
get_context
Source
get_private_key

Returns the concatenated private key bytes (encryption key + signing key).

Source
get_public_key

Returns the concatenated public key bytes (encryption key + signing key).

Source
get_salt
Source
hash

Generates an UInt64 hash value for this object.

This method must have the property that a == b implies a.hash == b.hash.

The hash value is used along with == by the Hash class to determine if two objects reference the same hash key.

Subclasses must not override this method. Instead, they must define hash(hasher), though usually the macro def_hash can be used to generate this method.

Source
hash=(hash : Bytes | Nil)
Source
hexhash
Source
hexhash=(hexhash : String | Nil)
Source
load(path : String) : Bool

Loads an identity's private key from a file and derives all key pairs from it.

Source
load_private_key(prv_bytes : Bytes) : Bool

Loads encryption and signing key pairs from raw private key bytes. Returns true on success.

Source
load_public_key(pub_bytes : Bytes)

Loads encryption and signing public keys from raw public key bytes.

Source
prv

─── Instance properties ───────────────────────────────────────────

Source
prv=(prv : Cryptography::X25519PrivateKey | Nil)

─── Instance properties ───────────────────────────────────────────

Source
prv_bytes
Source
prv_bytes=(prv_bytes : Bytes | Nil)
Source
pub=(pub : Cryptography::X25519PublicKey | Nil)
Source
pub_bytes
Source
pub_bytes=(pub_bytes : Bytes | Nil)
Source
sig_prv
Source
sig_prv=(sig_prv : Cryptography::Ed25519PrivateKey | Nil)
Source
sig_prv_bytes
Source
sig_prv_bytes=(sig_prv_bytes : Bytes | Nil)
Source
sig_pub
Source
sig_pub=(sig_pub : Cryptography::Ed25519PublicKey | Nil)
Source
sig_pub_bytes
Source
sig_pub_bytes=(sig_pub_bytes : Bytes | Nil)
Source
sign(message : Bytes) : Bytes

Signs a message using the Ed25519 private signing key.

Source
to_file(path : String) : Bool

Saves the identity's private key to a file at the given path.

Source
to_s(io : IO) : Nil

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>
Source
update_hashes
Source
validate(signature : Bytes, message : Bytes) : Bool

Validates an Ed25519 signature against a message. Returns true if the signature is valid.

Source