SecEncKey

SecEncKey

Holds the secret part of an encryption key

Constructor

new SecEncKey()

Source:

Methods

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

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

An example use case for this method is if you have received messages from multiple senders, you may fetch your secret key once, then iterate over the messages passing each associated public encryption key to decrypt each message.

Parameters:
Name Type Description
cipher Buffer

Encrypted data

publicEncKey PubEncKey

Sender's public encryption key

Source:
Throws:
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, recipientPubKey) → {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
recipientPubKey PubEncKey

Recipient's public encryption key

Source:
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 recipientPubKey = await app.crypto.pubEncKeyFromRaw(rawPubEncKey.buffer);
      const secEncKey = encKeyPair.secEncKey;
      const encryptedBuffer = await secEncKey.encrypt(data, recipientPubKey)
    } catch(err) {
      throw err;
    }
};

getRaw() → {Promise.<Buffer>}

Generate raw buffer of secret encryption key

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