RsaOaepPublicKey class abstract

RSAES-OAEP public key for decryption of messages.

An RsaOaepPublicKey instance holds a public RSA key for encrypting messages using the RSAES-OAEP scheme as specified in RFC 3447.

An RsaOaepPublicKey can be imported from:

A public-private KeyPair consisting of a RsaOaepPublicKey and a RsaOaepPrivateKey can be generated using RsaOaepPrivateKey.generateKey.

Example

import 'dart:typed_data' show Uint8List;
import 'dart:convert' show utf8;
import 'package:webcrypto/webcrypto.dart';

// Generate a public / private key-pair.
final keyPair = await RsaOaepPrivateKey.generateKey(
  4096,
  BigInt.from(65537),
  Hash.sha256,
);

// Generate a 256 bit symmetric key
final secretKeyToBeShared = await AesGcmSecretKey.generateKey(256);

// Using publicKey Bob can encrypt secretKeyToBeShared, such that it can
// only be decrypted with the private key.
final encryptedRawKey = await keyPair.publicKey.encryptBytes(
  await secretKeyToBeShared.exportRawKey(),
  label: 'shared-key',
);

// Given privateKey and encryptedRawKey Alice can decrypt the shared key.
final sharedRawSecretKey = await keypair.privateKey.decryptBytes(
  encryptedRawKey,
  label: 'shared-key',
);
final sharedSecretKey = await AesGcmSecretKey.importRaw(sharedRawSecretKey);
// Now both Alice and Bob share a secret key.

The size of the message to be encrypted is limited to message.length <= (modulusLength - 2 * hashLength - 2) / 8. Thus, RsaOaepPublicKey.encryptBytes is usually only used to encrypt the key for symmetric cipher like AesCbcSecretKey, AesCtrSecretKey or AesGcmSecretKey, after which the symmetric cipher can be used encrypt/decrypt larger messages.

Annotations
  • @sealed

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

encryptBytes(List<int> data, {List<int>? label}) Future<Uint8List>
Encrypt data such that it can only be decrypted with RsaOaepPrivateKey.decryptBytes from the matching private key.
exportJsonWebKey() Future<Map<String, dynamic>>
Export RSAES-OAEP public key in JSON Web Key format.
exportSpkiKey() Future<Uint8List>
Export this RSAES-OAEP public key in SPKI format.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

importJsonWebKey(Map<String, dynamic> jwk, Hash hash) Future<RsaOaepPublicKey>
Import RSAES-OAEP public key in JSON Web Key format.
importSpkiKey(List<int> keyData, Hash hash) Future<RsaOaepPublicKey>
Import RSAES-OAEP public key in SPKI format.