PubEncKey

PubEncKey

Holds the public part of an encryption key pair

Constructor

new PubEncKey()

Source:

Methods

decrypt(cipher, secretEncKey) → {Promise.<Buffer>}

Decrypt the given cipher text using the sender's public encryption key and the recipient's secret encryption key. Read more about authenticated encryption.

Parameters:
Name Type Description
cipher Buffer

Encrypted data

secretEncKey SecEncKey

Recipient's secret encryption key

Source:
Example
// Assumes SAFEApp interface has been obtained
const asyncFn = async () => {
    const cipher = 'plain text to be encrypted';
    try {
      const encKeyPair = await app.crypto.generateEncKeyPair();
      const rawPubEncKey = Buffer.from(<sender's public encryption key>);
      const pubEncKey = await app.crypto.pubEncKeyFromRaw(rawPubEncKey.buffer);
      const secretEncKey = encKeyPair.secEncKey;
      const decryptedData = await pubEncKey.decrypt(cipher, secretEncKey)
    } catch(err) {
      throw err;
    }
};

encrypt(data, secretEncKey) → {Promise.<Buffer>}

Encrypt the input using recipient's public encryption key and sender's secret encryption key, such that each party can generate a shared secret key to verify the integrity of ciphers and to also decrypt them. Read more about authenticated encryption.

Parameters:
Name Type Description
data Buffer | String
secretEncKey SecEncKey

Sender's secret encryption key

Source:
Throws:
Example
// Assumes SAFEApp interface has been obtained
const asyncFn = async () => {
    const data = 'plain text to be encrypted';
    try {
      const encKeyPair = await app.crypto.generateEncKeyPair();
      const rawPubEncKey = Buffer.from(<recipient's public encryption key>);
      const pubEncKey = await app.crypto.pubEncKeyFromRaw(rawPubEncKey.buffer);
      const secretEncKey = encKeyPair.secEncKey;
      const encryptedBuffer = await pubEncKey.encrypt(data, secretEncKey)
    } catch(err) {
      throw err;
    }
};

encryptSealed(data) → {Promise.<Buffer>}

Encrypt the input using recipient's public key. Only recipient will be able to decrypt data. Read more about sealed boxes.

Parameters:
Name Type Description
data String | Buffer
Source:
Example
// Assumes SAFEApp interface has been obtained
const asyncFn = async () => {
    const stringOrBuffer = 'plain string to be encrypted';
    try {
      const rawPubEncKey = Buffer.from(<recipient's public encryption key>);
      const pubEncKey = await app.crypto.pubEncKeyFromRaw(rawPubEncKey.buffer);
      const encryptedData = await pubEncKey.encryptSealed(data);
    } catch(err) {
      throw err;
    }
};

getRaw() → {Promise.<Buffer>}

Generate raw buffer of public encryption key

Source:
Example
// Assumes SAFEApp interface has been obtained
const asyncFn = async () => {
  try {
    const encKeyPair = await app.crypto.generateEncKeyPair();
    const pubEncKey = encKeyPair.pubEncKey;
    const rawPubEncKey = await pubEncKey.getRaw();
  } catch (err) {
    throw err;
  }
};