decryptRsaOaep function

Future<Uint8List> decryptRsaOaep(
  1. String privateKey,
  2. dynamic message,
  3. {HashName digest = HashName.sha256,
  4. bool cleanupTempDirectory = true}
)

openssl pkeyutl -decrypt -pkeyopt rsa_padding_mode:oaep -in hello.encrypted -inkey myprivate.pem openssl rsautl -decrypt -oaep -in encrypted.dat -inkey privatekey.pem

Implementation

Future<Uint8List> decryptRsaOaep(String privateKey, message,
    {HashName digest = HashName.sha256,
      bool cleanupTempDirectory = true}) async {
  if(message is String) {
    message = base64Decode(message);
  }

  final tempDir = await Directory.systemTemp.createTemp();
  final privateKeyPath = path.join(tempDir.path, 'privatekey.pem');
  await File(privateKeyPath).writeAsString(privateKey, encoding: utf8);
  final messagePath = path.join(tempDir.path, 'encrypted.dat');
  await File(messagePath).writeAsBytes(message);

  // TODO digest
  // TODO MGF digest
  final res = await Process.run(
      'openssl',
      [
        'pkeyutl',
        '-decrypt',
        '-pkeyopt',
        'rsa_padding_mode:oaep',
        '-in',
        messagePath,
        '-inkey',
        '$privateKeyPath',
      ],
      stdoutEncoding: null,
      includeParentEnvironment: true,
      runInShell: true);

  if (res.exitCode != 0) {
    if(cleanupTempDirectory) {
      await tempDir.delete(recursive: true);
    }
    throw OpensslException(
        res.exitCode, systemEncoding.decode(res.stdout), res.stderr);
  }

  if(cleanupTempDirectory) {
    await tempDir.delete(recursive: true);
  }

  return Uint8List.fromList(res.stdout);
}