Skip to content

ARsa#

Stores public and/or private RSA key and encrypts/decrypts data with it.

Header:#include <AUI/Crypt/ARsa.h>
CMake:aui_link(my_target PUBLIC aui::crypt)

Detailed Description#

Instances are created with one of the static factories: ARsa::generate() creates a new key pair, fromPrivateKeyPEM and fromPublicKeyPEM load an existing key. ARsa is not copyable; factories return _<ARsa>.

Encryption uses PKCS#1 v1.5 padding. Input of any length is accepted: it is split to blocks of getKeyLength() - 11 bytes, each encrypted separately, and output blocks are getKeyLength() bytes long. Operations failing inside mbedTLS throw AException.

An instance loaded with fromPublicKeyPEM holds no private part, so it is meant for encryption only.

See RSA keys and encryption for examples.

Warning

Encryption does not authenticate data. There is no signing API in this class.

Public Methods#

decrypt#


AByteBuffer ARsa::decrypt(AByteBufferView in)

Decrypts data produced by encrypt(). Requires the private part of the key.

Arguments
in
concatenated encrypted blocks.
Returns
original data.

encrypt#


AByteBuffer ARsa::encrypt(AByteBufferView in)

Encrypts data with the public part of the key.

Arguments
in
data of arbitrary length.
Returns
concatenated encrypted blocks.

fromPrivateKeyPEM#


static _<ARsa> ARsa::fromPrivateKeyPEM(AByteBufferView buffer)

Loads a private key (and thus the public key) from PEM. Throws AException if the data cannot be parsed.

fromPublicKeyPEM#


static _<ARsa> ARsa::fromPublicKeyPEM(AByteBufferView buffer)

Loads a public key from PEM. Throws AException if the data cannot be parsed.

generate#


static _<ARsa> ARsa::generate(int bits = 0 x800)

Generates a new key pair.

Arguments
bits
key size in bits; 2048 by default.

Examples:

examples/app/game_of_life/src/main.cpp

Game of Life — Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

    [[nodiscard]] CellState& operator[](glm::ivec2 position) { return get(mStorage, position); }

    void randomize() {
        ranges::generate(mStorage, [&] {
            static std::default_random_engine re;
            static std::uniform_int_distribution d(0, 10);
            return d(re) > 7 ? CellState::ALIVE : CellState::DEAD;
        });
        emit frameComplete;
examples/ui/views/src/ExampleWindow.cpp

Views Example — All-in-one views building example.

                                   _new<AButton>("Four"), _new<AButton>("Five") })
                      .build() AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp }, Expanding{} },
              _new<ALabel>("Grid splitter"),
              AGridSplitter::Builder()
                      .withItems(AVector<AVector<_<AView>>>::generate(
                          5,
                          [](size_t y) {
                              return AVector<_<AView>>::generate(5, [&](size_t x) {
                                  return _new<AButton>("{}x{}"_format(x, y));
                              });
examples/ui/infinite_lazy_list/src/main.cpp

Infinite Lazy List — Usage of AUI_DECLARATIVE_FOR to make an infinite lazy list.

            AThread::sleep(500ms); // imitate hard work here

            // aka "loaded" from backend storage of some kind
            auto loadedItems = AVector<_<Item>>::generate(20, [&](size_t i) {
                return aui::ptr::manage_shared(new Item { .value = "Item {}"_format(loadFrom + i) });
            });

            AUI_UI_THREAD { // back to main thread.
                state->items.writeScope()->insertAll(loadedItems);

getKeyLength#


size_t ARsa::getKeyLength()

Returns the key length in bytes (modulus size).

getPrivateKeyPEM#


AByteBuffer ARsa::getPrivateKeyPEM()

Exports the private key in PEM format. Requires the private part of the key.

getPublicKeyPEM#


AByteBuffer ARsa::getPublicKeyPEM()

Exports the public key in PEM format.