class

Arcana::Key

Inherits Reference < Object

An Ed25519 key pair: what a project proves its addresses with (see Signing). The bus holds only the public half.

key = Arcana::Key.load_or_create(".ai/arcana.key") key.public_key # => "ed25519:3q2+7w..." sig = key.sign("data") Arcana::Key.verify(key.public_key, sig, "data") # => true

The private key is kept as PKCS#8 PEM, as openssl genpkey -algorithm ed25519 writes it, so other languages' libraries read it as they are. A public key is written ed25519: and the raw 32 bytes in base64; a signature is the raw 64 bytes in base64.

Constants

FILE_ENV = "ARCANA_KEY_FILE"

The variable naming the key file of a process that runs apart from its project's .ai/, such as a sandboxed or deployed service. Such a process has a key of its own, kept with its state, which holds only its own addresses.

PKCS8_HEADER = Bytes[48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]

The DER of an Ed25519 PKCS#8 private key, up to the 32-byte seed that follows it.

PKEY_FREE = begin free = LibC.dlsym(LibC::RTLD_DEFAULT, "EVP_PKEY_free") if free.null? else Proc(LibArcanaCrypto::EVP_PKEY, Nil).new(free, Pointer(Void).null) end end

EVP_PKEY_free, found at run time (see LibArcanaCrypto for why it isn't bound). Nil only where libcrypto can't be looked up, as in a static build, and then keys aren't freed.

PREFIX = "ed25519:"

Constructors

from_pem(pem : String) : Key

Read a PKCS#8 PEM Ed25519 private key.

Source
generate
Source
load(path : String | Path) : Key
Source
load_or_create(path : String | Path) : Key

The key at path, made first if there is none. Two processes making it at once end up with the same key: the second finds the first's.

Source
new(seed : Bytes)

A key from its 32-byte seed (the private key itself).

Source

Class methods

clear_errors

Empty OpenSSL's error queue, so a failure here isn't reported later by someone else's call. :nodoc:

Source
from_env

The key in the file ARCANA_KEY_FILE names, made there on first use; nil when the variable isn't set.

ARCANA_KEY_FILE=/var/lib/mj/arcana.key

client = Arcana::Client.new(..., key: Arcana::Key.from_env)

Source
valid_public_key?(public_key : String) : Bool

Is public_key written as a public key should be?

Source
verify(public_key : String, signature : String, data : String | Bytes) : Bool

Is signature (base64) public_key's signature of data? False for anything malformed, never raises.

Source

Instance methods

finalize
Source
inspect(io : IO) : Nil

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

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

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
Source
public_key
Source
save(path : String | Path) : Nil

Write the key to path, readable only by its owner. Fails if path exists: a key is never overwritten.

Source
sign(data : String | Bytes) : String

Sign data, returning the signature in base64.

Source
to_pem
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